webpack.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. const data = require('./package.json');
  4. const Build = require('@jupyterlab/buildutils').Build;
  5. const webpack = require('webpack');
  6. const merge = require('webpack-merge').default;
  7. const baseConfig = require('@jupyterlab/buildutils/lib/webpack.config.base');
  8. const { ModuleFederationPlugin } = webpack.container;
  9. const fs = require('fs-extra');
  10. const path = require('path');
  11. const Handlebars = require('handlebars');
  12. const names = Object.keys(data.dependencies).filter(function(name) {
  13. const packageData = require(path.join(name, 'package.json'));
  14. return packageData.jupyterlab !== undefined;
  15. });
  16. const jlab = data.jupyterlab;
  17. // Ensure a clear build directory.
  18. const buildDir = path.resolve(__dirname, 'build');
  19. if (fs.existsSync(buildDir)) {
  20. fs.removeSync(buildDir);
  21. }
  22. fs.ensureDirSync(buildDir);
  23. const extras = Build.ensureAssets({
  24. packageNames: names,
  25. output: buildDir
  26. });
  27. const singletons = {};
  28. data.jupyterlab.singletonPackages.forEach(element => {
  29. singletons[element] = { singleton: true };
  30. });
  31. // Handle the extensions.
  32. const extensions = jlab.extensions || {};
  33. const mimeExtensions = jlab.mimeExtensions || {};
  34. const externalExtensions = jlab.externalExtensions || {};
  35. // go through each external extension
  36. // add to mapping of extension and mime extensions, of package name
  37. // to path of the extension.
  38. for (const key in externalExtensions) {
  39. const {
  40. jupyterlab: { extension, mimeExtension }
  41. } = require(`${key}/package.json`);
  42. if (extension !== undefined) {
  43. extensions[key] = extension === true ? '' : extension;
  44. }
  45. if (mimeExtension !== undefined) {
  46. mimeExtensions[key] = mimeExtension === true ? '' : mimeExtension;
  47. }
  48. }
  49. // Create the entry point file.
  50. const source = fs.readFileSync('index.js').toString();
  51. const template = Handlebars.compile(source);
  52. const extData = {
  53. jupyterlab_extensions: extensions,
  54. jupyterlab_mime_extensions: mimeExtensions
  55. };
  56. const result = template(extData);
  57. fs.writeFileSync(path.join(buildDir, 'index.out.js'), result);
  58. // Make a bootstrap entrypoint
  59. const entryPoint = path.join(buildDir, 'bootstrap.js');
  60. const bootstrap = 'import("./index.out.js");';
  61. fs.writeFileSync(entryPoint, bootstrap);
  62. if (process.env.NODE_ENV === 'production') {
  63. baseConfig.mode = 'production';
  64. }
  65. module.exports = [
  66. merge(baseConfig, {
  67. entry: ['./publicpath.js', entryPoint],
  68. output: {
  69. path: buildDir,
  70. library: {
  71. type: 'var',
  72. name: ['_JUPYTERLAB', 'CORE_OUTPUT']
  73. },
  74. filename: 'bundle.js'
  75. },
  76. module: {
  77. rules: [
  78. // Workaround for https://github.com/jupyterlab/jupyterlab/issues/8655
  79. {
  80. test: /vega-statistics\/src\/.*.js$/,
  81. use: {
  82. loader: 'babel-loader',
  83. options: {
  84. presets: ['@babel/preset-env']
  85. }
  86. }
  87. }
  88. ]
  89. },
  90. plugins: [
  91. new ModuleFederationPlugin({
  92. library: {
  93. type: 'var',
  94. name: ['_JUPYTERLAB', 'CORE_LIBRARY_FEDERATION']
  95. },
  96. name: 'CORE_FEDERATION',
  97. shared: {
  98. ...data.resolutions,
  99. ...singletons
  100. }
  101. })
  102. ]
  103. })
  104. ].concat(extras);