webpack.config.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 MiniCssExtractPlugin = require('mini-css-extract-plugin');
  13. var Build = require('@jupyterlab/buildutils').Build;
  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 = path.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(path.join(buildDir, 'index.out.js'), result);
  40. fs.copySync('./package.json', path.join(buildDir, 'package.json'));
  41. fs.copySync(
  42. path.join(jlab.outputDir, 'imports.css'),
  43. path.join(buildDir, 'imports.css')
  44. );
  45. // Set up variables for watch mode.
  46. var localLinked = {};
  47. var ignoreCache = Object.create(null);
  48. Object.keys(jlab.linkedPackages).forEach(function(name) {
  49. var localPath = require.resolve(path.join(name, 'package.json'));
  50. localLinked[name] = path.dirname(localPath);
  51. });
  52. var ignorePatterns = [/^\.\#/]; // eslint-disable-line
  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. var stats = fs.statSync(localPath);
  58. if (!stats.isFile(localPath)) {
  59. return;
  60. }
  61. var source = fs.realpathSync(path.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 WebPack Plugin that copies the assets to the static directory and
  78. * fixes the output of the HTMLWebpackPlugin
  79. */
  80. function JupyterFrontEndPlugin() {}
  81. JupyterFrontEndPlugin.prototype.apply = function(compiler) {
  82. compiler.hooks.afterEmit.tap(
  83. 'JupyterFrontEndPlugin',
  84. function() {
  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. chunksSortMode: 'none',
  112. template: path.join('templates', 'template.html'),
  113. title: jlab.name || 'JupyterLab'
  114. }),
  115. new webpack.HashedModuleIdsPlugin(),
  116. new JupyterFrontEndPlugin({}),
  117. new MiniCssExtractPlugin({
  118. filename: '[name].[chunkhash].css'
  119. })
  120. ];
  121. if (process.argv.includes('--analyze')) {
  122. plugins.push(new Visualizer());
  123. }
  124. module.exports = [
  125. {
  126. mode: 'development',
  127. entry: {
  128. main: ['whatwg-fetch', path.resolve(buildDir, 'index.out.js')]
  129. },
  130. output: {
  131. path: path.resolve(buildDir),
  132. publicPath: '{{page_config.fullStaticUrl}}/',
  133. filename: '[name].[chunkhash].js'
  134. },
  135. optimization: {
  136. splitChunks: {
  137. chunks: 'all',
  138. // Put all CSS in one chunk, otherwise we get some out of order issues
  139. cacheGroups: {
  140. styles: {
  141. name: 'styles',
  142. test: /\.css$/,
  143. chunks: 'all',
  144. enforce: true
  145. }
  146. }
  147. }
  148. },
  149. module: {
  150. rules: [
  151. {
  152. test: /\.css$/,
  153. use: [MiniCssExtractPlugin.loader, 'css-loader']
  154. },
  155. { test: /\.md$/, use: 'raw-loader' },
  156. { test: /\.txt$/, use: 'raw-loader' },
  157. {
  158. test: /\.js$/,
  159. use: ['source-map-loader'],
  160. enforce: 'pre',
  161. // eslint-disable-next-line no-undef
  162. exclude: /node_modules/
  163. },
  164. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  165. { test: /\.js.map$/, use: 'file-loader' },
  166. {
  167. test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
  168. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  169. },
  170. {
  171. test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
  172. use: 'url-loader?limit=10000&mimetype=application/font-woff'
  173. },
  174. {
  175. test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
  176. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  177. },
  178. {
  179. test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
  180. use: 'url-loader?limit=10000&mimetype=application/octet-stream'
  181. },
  182. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  183. {
  184. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  185. use: 'url-loader?limit=10000&mimetype=image/svg+xml'
  186. }
  187. ]
  188. },
  189. watchOptions: {
  190. ignored: function(localPath) {
  191. localPath = path.resolve(localPath);
  192. if (localPath in ignoreCache) {
  193. return ignoreCache[localPath];
  194. }
  195. // Ignore files with certain patterns
  196. var baseName = localPath.replace(/^.*[\\\/]/, ''); // eslint-disable-line
  197. if (
  198. ignorePatterns.some(function(rexp) {
  199. return baseName.match(rexp);
  200. })
  201. ) {
  202. return true;
  203. }
  204. // Limit the watched files to those in our local linked package dirs.
  205. var ignore = true;
  206. Object.keys(localLinked).some(function(name) {
  207. // Bail if already found.
  208. var rootPath = localLinked[name];
  209. var contained = localPath.indexOf(rootPath + path.sep) !== -1;
  210. if (localPath !== rootPath && !contained) {
  211. return false;
  212. }
  213. var rest = localPath.slice(rootPath.length);
  214. if (rest.indexOf('node_modules') === -1) {
  215. ignore = false;
  216. maybeSync(localPath, name, rest);
  217. }
  218. return true;
  219. });
  220. ignoreCache[localPath] = ignore;
  221. return ignore;
  222. }
  223. },
  224. node: {
  225. fs: 'empty'
  226. },
  227. bail: true,
  228. devtool: 'source-map',
  229. externals: ['node-fetch', 'ws'],
  230. plugins,
  231. stats: {
  232. chunkModules: true
  233. }
  234. }
  235. ].concat(extraConfig);