webpack.config.js 8.4 KB

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