webpack.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. var plib = 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 BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
  11. .BundleAnalyzerPlugin;
  12. var Build = require('@jupyterlab/buildutils').Build;
  13. var WPPlugin = require('@jupyterlab/buildutils').WPPlugin;
  14. var package_data = require('./package.json');
  15. // Handle the extensions.
  16. var jlab = package_data.jupyterlab;
  17. var extensions = jlab.extensions;
  18. var mimeExtensions = jlab.mimeExtensions;
  19. var packageNames = Object.keys(mimeExtensions).concat(Object.keys(extensions));
  20. // Ensure a clear build directory.
  21. var buildDir = plib.resolve(jlab.buildDir);
  22. if (fs.existsSync(buildDir)) {
  23. fs.removeSync(buildDir);
  24. }
  25. fs.ensureDirSync(buildDir);
  26. // Build the assets
  27. var extraConfig = Build.ensureAssets({
  28. packageNames: packageNames,
  29. output: jlab.outputDir
  30. });
  31. // Create the entry point file.
  32. var source = fs.readFileSync('index.js').toString();
  33. var template = Handlebars.compile(source);
  34. var data = {
  35. jupyterlab_extensions: extensions,
  36. jupyterlab_mime_extensions: mimeExtensions
  37. };
  38. var result = template(data);
  39. fs.writeFileSync(plib.join(buildDir, 'index.out.js'), result);
  40. fs.copySync('./package.json', plib.join(buildDir, 'package.json'));
  41. fs.copySync(
  42. plib.join(jlab.outputDir, 'imports.css'),
  43. plib.join(buildDir, 'imports.css')
  44. );
  45. // Set up variables for the watch mode ignore plugins
  46. let watched = {};
  47. let ignoreCache = Object.create(null);
  48. Object.keys(jlab.linkedPackages).forEach(function(name) {
  49. if (name in watched) return;
  50. const localPkgPath = require.resolve(plib.join(name, 'package.json'));
  51. watched[name] = plib.dirname(localPkgPath);
  52. });
  53. // Set up source-map-loader to look in watched lib dirs
  54. let sourceMapRes = Object.values(watched).reduce((res, name) => {
  55. res.push(new RegExp(name + '/lib'));
  56. return res;
  57. }, []);
  58. /**
  59. * A filter function set up to exclude all files that are not
  60. * in a package contained by the Jupyterlab repo. Used to ignore
  61. * files during a `--watch` build.
  62. */
  63. function ignored(path) {
  64. path = plib.resolve(path);
  65. if (path in ignoreCache) {
  66. // Bail if already found.
  67. return ignoreCache[path];
  68. }
  69. // Limit the watched files to those in our local linked package dirs.
  70. let ignore = true;
  71. Object.keys(watched).some(name => {
  72. const rootPath = watched[name];
  73. const contained = path.indexOf(rootPath + plib.sep) !== -1;
  74. if (path !== rootPath && !contained) {
  75. return false;
  76. }
  77. const rest = path.slice(rootPath.length);
  78. if (rest.indexOf('node_modules') === -1) {
  79. ignore = false;
  80. }
  81. return true;
  82. });
  83. ignoreCache[path] = ignore;
  84. return ignore;
  85. }
  86. const plugins = [
  87. new WPPlugin.NowatchDuplicatePackageCheckerPlugin({
  88. verbose: true,
  89. exclude(instance) {
  90. // ignore known duplicates
  91. return ['domelementtype', 'hash-base', 'inherits'].includes(
  92. instance.name
  93. );
  94. }
  95. }),
  96. new HtmlWebpackPlugin({
  97. chunksSortMode: 'none',
  98. template: plib.join('templates', 'template.html'),
  99. title: jlab.name || 'JupyterLab'
  100. }),
  101. new webpack.HashedModuleIdsPlugin(),
  102. // custom plugin for ignoring files during a `--watch` build
  103. new WPPlugin.FilterWatchIgnorePlugin(ignored),
  104. // custom plugin that copies the assets to the static directory
  105. new WPPlugin.FrontEndPlugin(buildDir, jlab.staticDir)
  106. ];
  107. if (process.argv.includes('--analyze')) {
  108. plugins.push(new BundleAnalyzerPlugin());
  109. }
  110. module.exports = [
  111. {
  112. mode: 'development',
  113. entry: {
  114. main: ['whatwg-fetch', plib.resolve(buildDir, 'index.out.js')]
  115. },
  116. // Map Phosphor files to Lumino files.
  117. resolve: {
  118. alias: {
  119. '@phosphor/algorithm$': plib.resolve(
  120. __dirname,
  121. 'node_modules/@lumino/algorithm/lib/index.js'
  122. ),
  123. '@phosphor/application$': plib.resolve(
  124. __dirname,
  125. 'node_modules/@lumino/application/lib/index.js'
  126. ),
  127. '@phosphor/commands$': plib.resolve(
  128. __dirname,
  129. 'node_modules/@lumino/commands/lib/index.js'
  130. ),
  131. '@phosphor/coreutils$': plib.resolve(
  132. __dirname,
  133. 'node_modules/@lumino/coreutils/lib/index.js'
  134. ),
  135. '@phosphor/disposable$': plib.resolve(
  136. __dirname,
  137. 'node_modules/@lumino/disposable/lib/index.js'
  138. ),
  139. '@phosphor/domutils$': plib.resolve(
  140. __dirname,
  141. 'node_modules/@lumino/domutils/lib/index.js'
  142. ),
  143. '@phosphor/dragdrop$': plib.resolve(
  144. __dirname,
  145. 'node_modules/@lumino/dragdrop/lib/index.js'
  146. ),
  147. '@phosphor/dragdrop/style': plib.resolve(
  148. __dirname,
  149. 'node_modules/@lumino/widgets/style'
  150. ),
  151. '@phosphor/messaging$': plib.resolve(
  152. __dirname,
  153. 'node_modules/@lumino/messaging/lib/index.js'
  154. ),
  155. '@phosphor/properties$': plib.resolve(
  156. __dirname,
  157. 'node_modules/@lumino/properties/lib'
  158. ),
  159. '@phosphor/signaling': plib.resolve(
  160. __dirname,
  161. 'node_modules/@lumino/signaling/lib/index.js'
  162. ),
  163. '@phosphor/widgets/style': plib.resolve(
  164. __dirname,
  165. 'node_modules/@lumino/widgets/style'
  166. ),
  167. '@phosphor/virtualdom$': plib.resolve(
  168. __dirname,
  169. 'node_modules/@lumino/virtualdom/lib/index.js'
  170. ),
  171. '@phosphor/widgets$': plib.resolve(
  172. __dirname,
  173. 'node_modules/@lumino/widgets/lib/index.js'
  174. )
  175. }
  176. },
  177. output: {
  178. path: plib.resolve(buildDir),
  179. publicPath: '{{page_config.fullStaticUrl}}/',
  180. filename: '[name].[chunkhash].js'
  181. },
  182. optimization: {
  183. splitChunks: {
  184. chunks: 'all'
  185. }
  186. },
  187. module: {
  188. rules: [
  189. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  190. { test: /\.md$/, use: 'raw-loader' },
  191. { test: /\.txt$/, use: 'raw-loader' },
  192. {
  193. test: /\.js$/,
  194. include: sourceMapRes,
  195. use: ['source-map-loader'],
  196. enforce: 'pre'
  197. },
  198. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  199. { test: /\.js.map$/, use: 'file-loader' },
  200. {
  201. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  202. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  203. },
  204. {
  205. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  206. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  207. },
  208. {
  209. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  210. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  211. },
  212. {
  213. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  214. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  215. },
  216. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  217. {
  218. // In .css files, svg is loaded as a data URI.
  219. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  220. issuer: { test: /\.css$/ },
  221. use: {
  222. loader: 'svg-url-loader',
  223. options: { encoding: 'none', limit: 10000 }
  224. }
  225. },
  226. {
  227. // In .ts and .tsx files (both of which compile to .js), svg files
  228. // must be loaded as a raw string instead of data URIs.
  229. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  230. issuer: { test: /\.js$/ },
  231. use: {
  232. loader: 'raw-loader'
  233. }
  234. }
  235. ]
  236. },
  237. watchOptions: {
  238. poll: 500,
  239. aggregateTimeout: 1000
  240. },
  241. node: {
  242. fs: 'empty'
  243. },
  244. bail: true,
  245. devtool: 'inline-source-map',
  246. externals: ['node-fetch', 'ws'],
  247. plugins
  248. }
  249. ].concat(extraConfig);