webpack.config.js 6.5 KB

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