Jelajahi Sumber

Merge pull request #8911 from thaddmt/thadd/build-extension-webpack-public-path

Update labextension build cli to include a parameter for setting the webpack publicPath option
Steven Silvester 4 tahun lalu
induk
melakukan
36160844b2

+ 3 - 1
builder/src/build-extension.ts

@@ -22,6 +22,7 @@ commander
   .description('Build an extension')
   .option('--prod', 'build in prod mode (default is dev)')
   .requiredOption('--core-path <path>', 'the core package directory')
+  .option('--static-path <path>', 'static path for build assets')
   .option('--watch')
   .action(async cmd => {
     let node_env = 'development';
@@ -42,7 +43,8 @@ commander
     const env = {
       PACKAGE_PATH: packagePath,
       NODE_ENV: node_env,
-      CORE_PATH: path.resolve(cmd.corePath)
+      CORE_PATH: path.resolve(cmd.corePath),
+      STATIC_PATH: cmd.staticPath
     };
     run(cmdText, { env: { ...process.env, ...env } });
   });

+ 5 - 1
builder/src/webpack.config.ext.ts

@@ -14,6 +14,7 @@ const { ModuleFederationPlugin } = webpack.container;
 const packagePath: string = process.env.PACKAGE_PATH || '';
 const nodeEnv: string = process.env.NODE_ENV || '';
 const corePath: string = process.env.CORE_PATH || '';
+const staticPath: string = process.env.STATIC_PATH || '';
 
 if (nodeEnv === 'production') {
   baseConfig.mode = 'production';
@@ -148,6 +149,9 @@ fs.copyFileSync(
 // 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 webpackPublicPathString = staticPath
+  ? `"${staticPath}"`
+  : `getOption('fullLabextensionsUrl') + '/${data.name}/'`;
 const publicpath = path.join(outputPath, 'publicPath.js');
 fs.writeFileSync(
   publicpath,
@@ -166,7 +170,7 @@ function getOption(name) {
 }
 
 // eslint-disable-next-line no-undef
-__webpack_public_path__ = getOption('fullLabextensionsUrl') + '/${data.name}/';
+__webpack_public_path__ = ${webpackPublicPathString};
 `
 );
 

+ 7 - 2
jupyterlab/dynamic_labextensions.py

@@ -163,7 +163,7 @@ def develop_labextension_py(module, user=False, sys_prefix=False, overwrite=Fals
     return full_dests
 
 
-def build_labextension(path, logger=None):
+def build_labextension(path, logger=None, static_path=None):
     """Build a labextension in the given path"""
     core_path = osp.join(HERE, 'staging')
     ext_path = osp.abspath(path)
@@ -172,7 +172,12 @@ def build_labextension(path, logger=None):
         logger.info('Building extension in %s' % path)
 
     builder = _ensure_builder(ext_path, core_path)
-    subprocess.check_call(['node', builder, '--core-path', core_path,  ext_path], cwd=ext_path)
+
+    arguments = ['node', builder, '--core-path', core_path,  ext_path]
+    if static_path is not None:
+        arguments.extend(['--static-path', static_path])
+
+    subprocess.check_call(arguments, cwd=ext_path)
 
 
 def watch_labextension(path, app_dir=None, logger=None):

+ 8 - 1
jupyterlab/labextensions.py

@@ -190,9 +190,16 @@ class DevelopLabExtensionApp(BaseExtensionApp):
 class BuildLabExtensionApp(BaseExtensionApp):
     description = "Build labextension"
 
+    static_path = Unicode('', config=True,
+        help="Sets the path for static assets when building")
+
+    aliases = {
+        'static-path': 'BuildLabExtensionApp.static_path'
+    }
+
     def run_task(self):
         self.extra_args = self.extra_args or [os.getcwd()]
-        build_labextension(self.extra_args[0], logger=self.log)
+        build_labextension(self.extra_args[0], logger=self.log, static_path=self.static_path or None)
 
 
 class WatchLabExtensionApp(BaseExtensionApp):

+ 2 - 0
scripts/ci_script.sh

@@ -185,8 +185,10 @@ if [[ $GROUP == usage ]]; then
     # Test with a dynamic install
     jupyter labextension develop extension --debug
     jupyter labextension build extension
+    python -m jupyterlab.browser_check
     jupyter labextension list 1>labextensions 2>&1
     cat labextensions | grep "@jupyterlab/mock-extension.*enabled.*OK"
+    jupyter labextension build extension --static-path /foo/
     jupyter labextension disable @jupyterlab/mock-extension --debug
     jupyter labextension enable @jupyterlab/mock-extension --debug 
     jupyter labextension uninstall @jupyterlab/mock-extension --debug