webpack.config.js 8.4 KB

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