webpack.config.js 7.9 KB

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