webpack.config.js 6.7 KB

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