webpack.config.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // Fix the template output.
  79. var indexPath = path.join(buildDir, 'index.html');
  80. var indexData = fs.readFileSync(indexPath, 'utf8');
  81. indexData = indexData
  82. .split('{{page_config.frontendUrl}}/')
  83. .join('{{page_config.frontendUrl}}');
  84. fs.writeFileSync(indexPath, indexData, 'utf8');
  85. // Copy the static assets.
  86. var staticDir = jlab.staticDir;
  87. if (!staticDir) {
  88. return;
  89. }
  90. // Ensure a clean static directory on the first emit.
  91. if (this._first && fs.existsSync(staticDir)) {
  92. fs.removeSync(staticDir);
  93. }
  94. this._first = false;
  95. fs.copySync(buildDir, staticDir);
  96. }.bind(this)
  97. );
  98. };
  99. JupyterFrontEndPlugin.prototype._first = true;
  100. const plugins = [
  101. new DuplicatePackageCheckerPlugin({
  102. verbose: true,
  103. exclude(instance) {
  104. // ignore known duplicates
  105. return ['domelementtype', 'hash-base', 'inherits'].includes(
  106. instance.name
  107. );
  108. }
  109. }),
  110. new HtmlWebpackPlugin({
  111. template: path.join('templates', 'template.html'),
  112. title: jlab.name || 'JupyterLab'
  113. }),
  114. new webpack.HashedModuleIdsPlugin(),
  115. new JupyterFrontEndPlugin({})
  116. ];
  117. if (process.argv.includes('--analyze')) {
  118. plugins.push(new Visualizer());
  119. }
  120. module.exports = [
  121. {
  122. mode: 'development',
  123. entry: {
  124. main: ['whatwg-fetch', path.resolve(buildDir, 'index.out.js')]
  125. },
  126. output: {
  127. path: path.resolve(buildDir),
  128. publicPath: '{{page_config.frontendUrl}}',
  129. filename: '[name].[chunkhash].js'
  130. },
  131. optimization: {
  132. splitChunks: {
  133. chunks: 'all'
  134. }
  135. },
  136. module: {
  137. rules: [
  138. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  139. { test: /\.md$/, use: 'raw-loader' },
  140. { test: /\.txt$/, use: 'raw-loader' },
  141. {
  142. test: /\.js$/,
  143. use: ['source-map-loader'],
  144. enforce: 'pre',
  145. // eslint-disable-next-line no-undef
  146. exclude: /node_modules/
  147. },
  148. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  149. { test: /\.js.map$/, use: 'file-loader' },
  150. {
  151. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  152. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  153. },
  154. {
  155. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  156. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  157. },
  158. {
  159. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  160. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  161. },
  162. {
  163. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  164. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  165. },
  166. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  167. {
  168. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  169. use: 'url-loader?limit=10000&mimetype=image/svg+xml'
  170. }
  171. ]
  172. },
  173. watchOptions: {
  174. ignored: function(localPath) {
  175. localPath = path.resolve(localPath);
  176. if (localPath in ignoreCache) {
  177. return ignoreCache[localPath];
  178. }
  179. // Ignore files with certain patterns
  180. var baseName = localPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line
  181. if (
  182. ignorePatterns.some(function(rexp) {
  183. return baseName.match(rexp);
  184. })
  185. ) {
  186. return true;
  187. }
  188. // Limit the watched files to those in our local linked package dirs.
  189. var ignore = true;
  190. Object.keys(localLinked).some(function(name) {
  191. // Bail if already found.
  192. var rootPath = localLinked[name];
  193. var contained = localPath.indexOf(rootPath + path.sep) !== -1;
  194. if (localPath !== rootPath && !contained) {
  195. return false;
  196. }
  197. var rest = localPath.slice(rootPath.length);
  198. if (rest.indexOf('node_modules') === -1) {
  199. ignore = false;
  200. maybeSync(localPath, name, rest);
  201. }
  202. return true;
  203. });
  204. ignoreCache[localPath] = ignore;
  205. return ignore;
  206. }
  207. },
  208. node: {
  209. fs: 'empty'
  210. },
  211. bail: true,
  212. devtool: 'source-map',
  213. externals: ['node-fetch', 'ws'],
  214. plugins,
  215. stats: {
  216. chunkModules: true
  217. }
  218. }
  219. ].concat(extraConfig);