webpack.config.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) Jupyter Development Team.
  3. | Distributed under the terms of the Modified BSD License.
  4. |----------------------------------------------------------------------------*/
  5. var path = 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 package_data = require('./package.json');
  14. // Handle the extensions.
  15. var jlab = package_data.jupyterlab;
  16. var extensions = jlab.extensions;
  17. var mimeExtensions = jlab.mimeExtensions;
  18. var extraConfig = Build.ensureAssets({
  19. packageNames: Object.keys(mimeExtensions).concat(Object.keys(extensions)),
  20. output: jlab.outputDir
  21. });
  22. // Create the entry point file.
  23. var source = fs.readFileSync('index.js').toString();
  24. var template = Handlebars.compile(source);
  25. var data = {
  26. jupyterlab_extensions: extensions,
  27. jupyterlab_mime_extensions: mimeExtensions
  28. };
  29. var result = template(data);
  30. // Ensure a clear build directory.
  31. var buildDir = path.resolve(jlab.buildDir);
  32. if (fs.existsSync(buildDir)) {
  33. fs.removeSync(buildDir);
  34. }
  35. fs.ensureDirSync(buildDir);
  36. fs.writeFileSync(path.join(buildDir, 'index.out.js'), result);
  37. fs.copySync('./package.json', path.join(buildDir, 'package.json'));
  38. // Set up variables for watch mode.
  39. var localLinked = {};
  40. var ignoreCache = Object.create(null);
  41. Object.keys(jlab.linkedPackages).forEach(function(name) {
  42. var localPath = require.resolve(path.join(name, 'package.json'));
  43. localLinked[name] = path.dirname(localPath);
  44. });
  45. var ignorePatterns = [/^\.\#/]; // eslint-disable-line
  46. /**
  47. * Sync a local path to a linked package path if they are files and differ.
  48. */
  49. function maybeSync(localPath, name, rest) {
  50. var stats = fs.statSync(localPath);
  51. if (!stats.isFile(localPath)) {
  52. return;
  53. }
  54. var source = fs.realpathSync(path.join(jlab.linkedPackages[name], rest));
  55. if (source === fs.realpathSync(localPath)) {
  56. return;
  57. }
  58. fs.watchFile(source, { interval: 500 }, function(curr) {
  59. if (!curr || curr.nlink === 0) {
  60. return;
  61. }
  62. try {
  63. fs.copySync(source, localPath);
  64. } catch (err) {
  65. console.error(err);
  66. }
  67. });
  68. }
  69. /**
  70. * A WebPack Plugin that copies the assets to the static directory and
  71. * fixes the output of the HTMLWebpackPlugin
  72. */
  73. function JupyterFrontEndPlugin() {}
  74. JupyterFrontEndPlugin.prototype.apply = function(compiler) {
  75. compiler.hooks.afterEmit.tap(
  76. 'JupyterFrontEndPlugin',
  77. function() {
  78. // Copy the static assets.
  79. var staticDir = jlab.staticDir;
  80. if (!staticDir) {
  81. return;
  82. }
  83. // Ensure a clean static directory on the first emit.
  84. if (this._first && fs.existsSync(staticDir)) {
  85. fs.removeSync(staticDir);
  86. }
  87. this._first = false;
  88. fs.copySync(buildDir, staticDir);
  89. }.bind(this)
  90. );
  91. };
  92. JupyterFrontEndPlugin.prototype._first = true;
  93. const plugins = [
  94. new DuplicatePackageCheckerPlugin({
  95. verbose: true,
  96. exclude(instance) {
  97. // ignore known duplicates
  98. return ['domelementtype', 'hash-base', 'inherits'].includes(
  99. instance.name
  100. );
  101. }
  102. }),
  103. new HtmlWebpackPlugin({
  104. chunksSortMode: 'none',
  105. template: path.join('templates', 'template.html'),
  106. title: jlab.name || 'JupyterLab'
  107. }),
  108. new webpack.HashedModuleIdsPlugin(),
  109. new JupyterFrontEndPlugin({})
  110. ];
  111. if (process.argv.includes('--analyze')) {
  112. plugins.push(new Visualizer());
  113. }
  114. module.exports = [
  115. {
  116. mode: 'development',
  117. entry: {
  118. main: ['whatwg-fetch', path.resolve(buildDir, 'index.out.js')]
  119. },
  120. output: {
  121. path: path.resolve(buildDir),
  122. publicPath: '{{page_config.fullStaticUrl}}/',
  123. filename: '[name].[chunkhash].js'
  124. },
  125. optimization: {
  126. splitChunks: {
  127. chunks: 'all'
  128. }
  129. },
  130. module: {
  131. rules: [
  132. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  133. { test: /\.md$/, use: 'raw-loader' },
  134. { test: /\.txt$/, use: 'raw-loader' },
  135. {
  136. test: /\.js$/,
  137. use: ['source-map-loader'],
  138. enforce: 'pre',
  139. // eslint-disable-next-line no-undef
  140. exclude: /node_modules/
  141. },
  142. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  143. { test: /\.js.map$/, use: 'file-loader' },
  144. {
  145. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  146. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  147. },
  148. {
  149. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  150. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  151. },
  152. {
  153. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  154. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  155. },
  156. {
  157. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  158. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  159. },
  160. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  161. {
  162. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  163. use: 'url-loader?limit=10000&mimetype=image/svg+xml'
  164. }
  165. ]
  166. },
  167. watchOptions: {
  168. ignored: function(localPath) {
  169. localPath = path.resolve(localPath);
  170. if (localPath in ignoreCache) {
  171. return ignoreCache[localPath];
  172. }
  173. // Ignore files with certain patterns
  174. var baseName = localPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line
  175. if (
  176. ignorePatterns.some(function(rexp) {
  177. return baseName.match(rexp);
  178. })
  179. ) {
  180. return true;
  181. }
  182. // Limit the watched files to those in our local linked package dirs.
  183. var ignore = true;
  184. Object.keys(localLinked).some(function(name) {
  185. // Bail if already found.
  186. var rootPath = localLinked[name];
  187. var contained = localPath.indexOf(rootPath + path.sep) !== -1;
  188. if (localPath !== rootPath && !contained) {
  189. return false;
  190. }
  191. var rest = localPath.slice(rootPath.length);
  192. if (rest.indexOf('node_modules') === -1) {
  193. ignore = false;
  194. maybeSync(localPath, name, rest);
  195. }
  196. return true;
  197. });
  198. ignoreCache[localPath] = ignore;
  199. return ignore;
  200. }
  201. },
  202. node: {
  203. fs: 'empty'
  204. },
  205. bail: true,
  206. devtool: 'source-map',
  207. externals: ['node-fetch', 'ws'],
  208. plugins,
  209. stats: {
  210. chunkModules: true
  211. }
  212. }
  213. ].concat(extraConfig);