webpack.config.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * Sync a local path to a linked package path if they are files and differ.
  80. * This is used by `jupyter lab --watch` to synchronize linked packages
  81. * and has no effect in `jupyter lab --dev-mode --watch`.
  82. */
  83. function maybeSync(localPath, name, rest) {
  84. const stats = fs.statSync(localPath);
  85. if (!stats.isFile(localPath)) {
  86. return;
  87. }
  88. const source = fs.realpathSync(plib.join(jlab.linkedPackages[name], rest));
  89. if (source === fs.realpathSync(localPath)) {
  90. return;
  91. }
  92. fs.watchFile(source, { interval: 500 }, function(curr) {
  93. if (!curr || curr.nlink === 0) {
  94. return;
  95. }
  96. try {
  97. fs.copySync(source, localPath);
  98. } catch (err) {
  99. console.error(err);
  100. }
  101. });
  102. }
  103. /**
  104. * A filter function set up to exclude all files that are not
  105. * in a package contained by the Jupyterlab repo. Used to ignore
  106. * files during a `--watch` build.
  107. */
  108. function ignored(path) {
  109. path = plib.resolve(path);
  110. if (path in ignoreCache) {
  111. // Bail if already found.
  112. return ignoreCache[path];
  113. }
  114. // Limit the watched files to those in our local linked package dirs.
  115. let ignore = true;
  116. Object.keys(watched).some(name => {
  117. const rootPath = watched[name];
  118. const contained = path.indexOf(rootPath + plib.sep) !== -1;
  119. if (path !== rootPath && !contained) {
  120. return false;
  121. }
  122. const rest = path.slice(rootPath.length);
  123. if (rest.indexOf('node_modules') === -1) {
  124. ignore = false;
  125. maybeSync(path, name, rest);
  126. }
  127. return true;
  128. });
  129. ignoreCache[path] = ignore;
  130. return ignore;
  131. }
  132. const plugins = [
  133. new WPPlugin.NowatchDuplicatePackageCheckerPlugin({
  134. verbose: true,
  135. exclude(instance) {
  136. // ignore known duplicates
  137. return ['domelementtype', 'hash-base', 'inherits'].includes(
  138. instance.name
  139. );
  140. }
  141. }),
  142. new HtmlWebpackPlugin({
  143. chunksSortMode: 'none',
  144. template: plib.join('templates', 'template.html'),
  145. title: jlab.name || 'JupyterLab'
  146. }),
  147. new webpack.ids.HashedModuleIdsPlugin(),
  148. // custom plugin for ignoring files during a `--watch` build
  149. new WPPlugin.FilterWatchIgnorePlugin(ignored),
  150. // custom plugin that copies the assets to the static directory
  151. new WPPlugin.FrontEndPlugin(buildDir, jlab.staticDir),
  152. new webpack.DefinePlugin({
  153. 'process.env': '{}',
  154. process: {}
  155. })
  156. ];
  157. if (process.argv.includes('--analyze')) {
  158. plugins.push(new BundleAnalyzerPlugin());
  159. }
  160. module.exports = [
  161. {
  162. mode: 'development',
  163. entry: {
  164. main: ['whatwg-fetch', plib.resolve(buildDir, 'index.out.js')]
  165. },
  166. // Map Phosphor files to Lumino files.
  167. resolve: {
  168. alias: {
  169. '@phosphor/algorithm$': plib.resolve(
  170. __dirname,
  171. 'node_modules/@lumino/algorithm/dist/index.js'
  172. ),
  173. '@phosphor/application$': plib.resolve(
  174. __dirname,
  175. 'node_modules/@lumino/application/dist/index.js'
  176. ),
  177. '@phosphor/commands$': plib.resolve(
  178. __dirname,
  179. 'node_modules/@lumino/commands/dist/index.js'
  180. ),
  181. '@phosphor/coreutils$': plib.resolve(
  182. __dirname,
  183. 'node_modules/@lumino/coreutils/dist/index.js'
  184. ),
  185. '@phosphor/disposable$': plib.resolve(
  186. __dirname,
  187. 'node_modules/@lumino/disposable/dist/index.js'
  188. ),
  189. '@phosphor/domutils$': plib.resolve(
  190. __dirname,
  191. 'node_modules/@lumino/domutils/dist/index.js'
  192. ),
  193. '@phosphor/dragdrop$': plib.resolve(
  194. __dirname,
  195. 'node_modules/@lumino/dragdrop/dist/index.js'
  196. ),
  197. '@phosphor/dragdrop/style': plib.resolve(
  198. __dirname,
  199. 'node_modules/@lumino/widgets/style'
  200. ),
  201. '@phosphor/messaging$': plib.resolve(
  202. __dirname,
  203. 'node_modules/@lumino/messaging/dist/index.js'
  204. ),
  205. '@phosphor/properties$': plib.resolve(
  206. __dirname,
  207. 'node_modules/@lumino/properties/lib'
  208. ),
  209. '@phosphor/signaling': plib.resolve(
  210. __dirname,
  211. 'node_modules/@lumino/signaling/dist/index.js'
  212. ),
  213. '@phosphor/widgets/style': plib.resolve(
  214. __dirname,
  215. 'node_modules/@lumino/widgets/style'
  216. ),
  217. '@phosphor/virtualdom$': plib.resolve(
  218. __dirname,
  219. 'node_modules/@lumino/virtualdom/dist/index.js'
  220. ),
  221. '@phosphor/widgets$': plib.resolve(
  222. __dirname,
  223. 'node_modules/@lumino/widgets/dist/index.js'
  224. )
  225. }
  226. },
  227. output: {
  228. path: plib.resolve(buildDir),
  229. publicPath: '{{page_config.fullStaticUrl}}/',
  230. filename: '[name].[chunkhash].js'
  231. },
  232. optimization: {
  233. splitChunks: {
  234. chunks: 'all'
  235. }
  236. },
  237. module: {
  238. rules: [
  239. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  240. { test: /\.md$/, use: 'raw-loader' },
  241. { test: /\.txt$/, use: 'raw-loader' },
  242. {
  243. test: /\.js$/,
  244. include: sourceMapRes,
  245. use: ['source-map-loader'],
  246. enforce: 'pre'
  247. },
  248. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  249. { test: /\.js.map$/, use: 'file-loader' },
  250. {
  251. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  252. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  253. },
  254. {
  255. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  256. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  257. },
  258. {
  259. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  260. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  261. },
  262. {
  263. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  264. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  265. },
  266. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  267. {
  268. // In .css files, svg is loaded as a data URI.
  269. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  270. issuer: /\.css$/,
  271. use: {
  272. loader: 'svg-url-loader',
  273. options: { encoding: 'none', limit: 10000 }
  274. }
  275. },
  276. {
  277. // In .ts and .tsx files (both of which compile to .js), svg files
  278. // must be loaded as a raw string instead of data URIs.
  279. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  280. issuer: /\.js$/,
  281. use: {
  282. loader: 'raw-loader'
  283. }
  284. }
  285. ]
  286. },
  287. watchOptions: {
  288. poll: 500,
  289. aggregateTimeout: 1000
  290. },
  291. // node: {
  292. // fs: 'empty'
  293. // },
  294. bail: true,
  295. devtool: 'inline-source-map',
  296. externals: ['node-fetch', 'ws'],
  297. plugins
  298. }
  299. ].concat(extraConfig);