webpack.config.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 DuplicatePackageCheckerPlugin = require('duplicate-package-checker-webpack-plugin');
  11. var Visualizer = require('webpack-visualizer-plugin');
  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. /**
  54. * Sync a local path to a linked package path if they are files and differ.
  55. */
  56. function maybeSync(localPath, name, rest) {
  57. const stats = fs.statSync(localPath);
  58. if (!stats.isFile(localPath)) {
  59. return;
  60. }
  61. const source = fs.realpathSync(plib.join(jlab.linkedPackages[name], rest));
  62. if (source === fs.realpathSync(localPath)) {
  63. return;
  64. }
  65. fs.watchFile(source, { interval: 500 }, function(curr) {
  66. if (!curr || curr.nlink === 0) {
  67. return;
  68. }
  69. try {
  70. fs.copySync(source, localPath);
  71. } catch (err) {
  72. console.error(err);
  73. }
  74. });
  75. }
  76. /**
  77. * A filter function set up to exclude all files that are not
  78. * in a package contained by the Jupyterlab repo. Used to ignore
  79. * files during a `--watch` build.
  80. */
  81. function ignored(path) {
  82. path = plib.resolve(path);
  83. if (path in ignoreCache) {
  84. // Bail if already found.
  85. return ignoreCache[path];
  86. }
  87. // Limit the watched files to those in our local linked package dirs.
  88. let ignore = true;
  89. Object.keys(watched).some(name => {
  90. const rootPath = watched[name];
  91. const contained = path.indexOf(rootPath + plib.sep) !== -1;
  92. if (path !== rootPath && !contained) {
  93. return false;
  94. }
  95. const rest = path.slice(rootPath.length);
  96. if (rest.indexOf('node_modules') === -1) {
  97. ignore = false;
  98. maybeSync(path, name, rest);
  99. }
  100. return true;
  101. });
  102. ignoreCache[path] = ignore;
  103. return ignore;
  104. }
  105. const plugins = [
  106. new DuplicatePackageCheckerPlugin({
  107. verbose: true,
  108. exclude(instance) {
  109. // ignore known duplicates
  110. return ['domelementtype', 'hash-base', 'inherits'].includes(
  111. instance.name
  112. );
  113. }
  114. }),
  115. new HtmlWebpackPlugin({
  116. chunksSortMode: 'none',
  117. template: plib.join('templates', 'template.html'),
  118. title: jlab.name || 'JupyterLab'
  119. }),
  120. new webpack.HashedModuleIdsPlugin(),
  121. // custom plugin for ignoring files during a `--watch` build
  122. new WPPlugin.FilterWatchIgnorePlugin(ignored),
  123. // custom plugin that copies the assets to the static directory
  124. new WPPlugin.FrontEndPlugin(buildDir, jlab.staticDir)
  125. ];
  126. if (process.argv.includes('--analyze')) {
  127. plugins.push(new Visualizer());
  128. }
  129. module.exports = [
  130. {
  131. mode: 'development',
  132. entry: {
  133. main: ['whatwg-fetch', plib.resolve(buildDir, 'index.out.js')]
  134. },
  135. output: {
  136. path: plib.resolve(buildDir),
  137. publicPath: '{{page_config.fullStaticUrl}}/',
  138. filename: '[name].[chunkhash].js'
  139. },
  140. optimization: {
  141. splitChunks: {
  142. chunks: 'all'
  143. }
  144. },
  145. module: {
  146. rules: [
  147. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  148. { test: /\.md$/, use: 'raw-loader' },
  149. { test: /\.txt$/, use: 'raw-loader' },
  150. {
  151. test: /\.js$/,
  152. use: ['source-map-loader'],
  153. enforce: 'pre',
  154. // eslint-disable-next-line no-undef
  155. exclude: /node_modules/
  156. },
  157. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  158. { test: /\.js.map$/, use: 'file-loader' },
  159. {
  160. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  161. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  162. },
  163. {
  164. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  165. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  166. },
  167. {
  168. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  169. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  170. },
  171. {
  172. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  173. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  174. },
  175. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  176. {
  177. // in css files, svg is loaded as a url formatted string
  178. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  179. issuer: { test: /\.css$/ },
  180. use: {
  181. loader: 'svg-url-loader',
  182. options: { encoding: 'none', limit: 10000 }
  183. }
  184. },
  185. {
  186. // in ts and tsx files (both of which compile to js),
  187. // svg is loaded as a raw string
  188. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  189. issuer: { test: /\.js$/ },
  190. use: {
  191. loader: 'raw-loader'
  192. }
  193. }
  194. ]
  195. },
  196. watchOptions: {
  197. poll: 333
  198. },
  199. node: {
  200. fs: 'empty'
  201. },
  202. bail: true,
  203. devtool: 'inline-source-map',
  204. externals: ['node-fetch', 'ws'],
  205. plugins,
  206. stats: {
  207. chunkModules: true
  208. }
  209. }
  210. ].concat(extraConfig);