浏览代码

Dynamically set public path in generated extensions according to page config.

This uses the idea from https://github.com/webpack/webpack/issues/10352#issuecomment-675649389 to get the public path set in remoteEntry.js. Eventually, we might be able to specify an init option for code to run when a remoteEntry file is loaded.

Fixes #8827
Jason Grout 4 年之前
父节点
当前提交
42585410f1
共有 1 个文件被更改,包括 30 次插入3 次删除
  1. 30 3
      buildutils/src/webpack.config.ext.ts

+ 30 - 3
buildutils/src/webpack.config.ext.ts

@@ -144,15 +144,42 @@ fs.copyFileSync(
   path.join(outputPath, 'package.orig.json')
 );
 
+// TODO: We don't need this file after our compilation, since it is folded
+// into remoteEntry.js. We should either delete it, or figure out a way to
+// have the entry point below be dynamically generated text without having to
+// write to a file.
+const publicpath = path.join(outputPath, 'publicPath.js');
+fs.writeFileSync(
+  publicpath,
+  `
+function getOption(name) {
+  let configData = Object.create(null);
+  // Use script tag if available.
+  if (typeof document !== 'undefined' && document) {
+    const el = document.getElementById('jupyter-config-data');
+
+    if (el) {
+      configData = JSON.parse(el.textContent || '{}');
+    }
+  }
+  return configData[name] || '';
+}
+
+// eslint-disable-next-line no-undef
+__webpack_public_path__ = getOption('fullLabextensionsUrl') + '/${data.name}/';
+`
+);
+
 module.exports = [
   merge(baseConfig, {
     // Using empty object {} for entry because we want only
     // entrypoints generated by ModuleFederationPlugin
-    entry: {},
+    entry: {
+      [data.name]: publicpath
+    },
     output: {
       filename: '[name].[chunkhash].js',
-      path: outputPath,
-      publicPath: `/lab/extensions/${data.name}/`
+      path: outputPath
     },
     module: {
       rules: [{ test: /\.html$/, use: 'file-loader' }]