webpack.config.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. var path = require('path');
  6. var fs = require('fs-extra');
  7. var Handlebars = require('handlebars');
  8. var HtmlWebpackPlugin = require('html-webpack-plugin');
  9. var webpack = require('webpack');
  10. var DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
  11. var Visualizer = require('webpack-visualizer-plugin');
  12. var Build = require('@jupyterlab/buildutils').Build;
  13. var package_data = require('./package.json');
  14. // Handle the extensions.
  15. var jlab = package_data.jupyterlab;
  16. var extensions = jlab.extensions;
  17. var mimeExtensions = jlab.mimeExtensions;
  18. var packageNames = Object.keys(mimeExtensions).concat(Object.keys(extensions));
  19. // Ensure a clear build directory.
  20. var buildDir = path.resolve(jlab.buildDir);
  21. if (fs.existsSync(buildDir)) {
  22. fs.removeSync(buildDir);
  23. }
  24. fs.ensureDirSync(buildDir);
  25. // Build the assets
  26. var extraConfig = Build.ensureAssets({
  27. packageNames: packageNames,
  28. output: jlab.outputDir
  29. });
  30. // Create the entry point file.
  31. var source = fs.readFileSync('index.js').toString();
  32. var template = Handlebars.compile(source);
  33. var data = {
  34. jupyterlab_extensions: extensions,
  35. jupyterlab_mime_extensions: mimeExtensions
  36. };
  37. var result = template(data);
  38. fs.writeFileSync(path.join(buildDir, 'index.out.js'), result);
  39. fs.copySync('./package.json', path.join(buildDir, 'package.json'));
  40. fs.copySync(
  41. path.join(jlab.outputDir, 'imports.css'),
  42. path.join(buildDir, 'imports.css')
  43. );
  44. // Set up variables for watch mode.
  45. var localLinked = {};
  46. var ignoreCache = Object.create(null);
  47. Object.keys(jlab.linkedPackages).forEach(function(name) {
  48. var localPath = require.resolve(path.join(name, 'package.json'));
  49. localLinked[name] = path.dirname(localPath);
  50. });
  51. var ignorePatterns = [/^\.\#/]; // eslint-disable-line
  52. /**
  53. * Sync a local path to a linked package path if they are files and differ.
  54. */
  55. function maybeSync(localPath, name, rest) {
  56. var stats = fs.statSync(localPath);
  57. if (!stats.isFile(localPath)) {
  58. return;
  59. }
  60. var source = fs.realpathSync(path.join(jlab.linkedPackages[name], rest));
  61. if (source === fs.realpathSync(localPath)) {
  62. return;
  63. }
  64. fs.watchFile(source, { interval: 500 }, function(curr) {
  65. if (!curr || curr.nlink === 0) {
  66. return;
  67. }
  68. try {
  69. fs.copySync(source, localPath);
  70. } catch (err) {
  71. console.error(err);
  72. }
  73. });
  74. }
  75. /**
  76. * A WebPack Plugin that copies the assets to the static directory and
  77. * fixes the output of the HTMLWebpackPlugin
  78. */
  79. function JupyterFrontEndPlugin() {}
  80. JupyterFrontEndPlugin.prototype.apply = function(compiler) {
  81. compiler.hooks.afterEmit.tap(
  82. 'JupyterFrontEndPlugin',
  83. function() {
  84. // Copy the static assets.
  85. var staticDir = jlab.staticDir;
  86. if (!staticDir) {
  87. return;
  88. }
  89. // Ensure a clean static directory on the first emit.
  90. if (this._first && fs.existsSync(staticDir)) {
  91. fs.removeSync(staticDir);
  92. }
  93. this._first = false;
  94. fs.copySync(buildDir, staticDir);
  95. }.bind(this)
  96. );
  97. };
  98. JupyterFrontEndPlugin.prototype._first = true;
  99. const plugins = [
  100. new DuplicatePackageCheckerPlugin({
  101. verbose: true,
  102. exclude(instance) {
  103. // ignore known duplicates
  104. return ['domelementtype', 'hash-base', 'inherits'].includes(
  105. instance.name
  106. );
  107. }
  108. }),
  109. new HtmlWebpackPlugin({
  110. chunksSortMode: 'none',
  111. template: path.join('templates', 'template.html'),
  112. title: jlab.name || 'JupyterLab'
  113. }),
  114. new webpack.HashedModuleIdsPlugin(),
  115. new JupyterFrontEndPlugin({})
  116. ];
  117. if (process.argv.includes('--analyze')) {
  118. plugins.push(new Visualizer());
  119. }
  120. module.exports = [
  121. {
  122. mode: 'development',
  123. entry: {
  124. main: ['whatwg-fetch', path.resolve(buildDir, 'index.out.js')]
  125. },
  126. output: {
  127. path: path.resolve(buildDir),
  128. publicPath: '{{page_config.fullStaticUrl}}/',
  129. filename: '[name].[chunkhash].js'
  130. },
  131. optimization: {
  132. splitChunks: {
  133. chunks: 'all'
  134. }
  135. },
  136. module: {
  137. rules: [
  138. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  139. { test: /\.md$/, use: 'raw-loader' },
  140. { test: /\.txt$/, use: 'raw-loader' },
  141. {
  142. test: /\.js$/,
  143. use: ['source-map-loader'],
  144. enforce: 'pre',
  145. // eslint-disable-next-line no-undef
  146. exclude: /node_modules/
  147. },
  148. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  149. { test: /\.js.map$/, use: 'file-loader' },
  150. {
  151. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  152. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  153. },
  154. {
  155. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  156. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  157. },
  158. {
  159. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  160. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  161. },
  162. {
  163. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  164. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  165. },
  166. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  167. {
  168. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  169. use: 'url-loader?limit=10000&mimetype=image/svg+xml'
  170. }
  171. ]
  172. },
  173. watchOptions: {
  174. ignored: function(localPath) {
  175. localPath = path.resolve(localPath);
  176. if (localPath in ignoreCache) {
  177. return ignoreCache[localPath];
  178. }
  179. // Ignore files with certain patterns
  180. var baseName = localPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line
  181. if (
  182. ignorePatterns.some(function(rexp) {
  183. return baseName.match(rexp);
  184. })
  185. ) {
  186. return true;
  187. }
  188. // Limit the watched files to those in our local linked package dirs.
  189. var ignore = true;
  190. Object.keys(localLinked).some(function(name) {
  191. // Bail if already found.
  192. var rootPath = localLinked[name];
  193. var contained = localPath.indexOf(rootPath + path.sep) !== -1;
  194. if (localPath !== rootPath && !contained) {
  195. return false;
  196. }
  197. var rest = localPath.slice(rootPath.length);
  198. if (rest.indexOf('node_modules') === -1) {
  199. ignore = false;
  200. maybeSync(localPath, name, rest);
  201. }
  202. return true;
  203. });
  204. ignoreCache[localPath] = ignore;
  205. return ignore;
  206. },
  207. poll: 100
  208. },
  209. node: {
  210. fs: 'empty'
  211. },
  212. bail: true,
  213. devtool: 'source-map',
  214. externals: ['node-fetch', 'ws'],
  215. plugins,
  216. stats: {
  217. chunkModules: true
  218. }
  219. }
  220. ].concat(extraConfig);