build-labextension.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env node
  2. /* -----------------------------------------------------------------------------
  3. | Copyright (c) Jupyter Development Team.
  4. | Distributed under the terms of the Modified BSD License.
  5. |----------------------------------------------------------------------------*/
  6. // Build an extension
  7. // Inputs:
  8. // Path to extension (required)
  9. // Dev vs prod (dev is default)
  10. // Output path (defaults to <extension>/build)
  11. // Outputs
  12. // Webpack build assets
  13. import * as path from 'path';
  14. import commander from 'commander';
  15. import { run } from '@jupyterlab/buildutils';
  16. commander
  17. .description('Build an extension')
  18. .option('--development', 'build in development mode (implies --source-map)')
  19. .option('--source-map', 'generate source maps')
  20. .requiredOption('--core-path <path>', 'the core package directory')
  21. .option(
  22. '--static-url <url>',
  23. 'url for build assets, if hosted outside the built extension'
  24. )
  25. .option('--watch')
  26. .action(async cmd => {
  27. const mode = cmd.development ? 'development' : 'production';
  28. const corePath = path.resolve(cmd.corePath || process.cwd());
  29. const packagePath = path.resolve(cmd.args[0]);
  30. const sourceMap = cmd.sourceMap ? 'true' : '';
  31. const webpack = require.resolve('webpack-cli/bin/cli.js');
  32. const config = path.join(__dirname, 'webpack.config.ext.js');
  33. let cmdText = `node "${webpack}" --config "${config}" --mode ${mode}`;
  34. if (cmd.watch) {
  35. cmdText += ' --watch';
  36. }
  37. const env = {
  38. PACKAGE_PATH: packagePath,
  39. NODE_ENV: mode,
  40. CORE_PATH: corePath,
  41. STATIC_URL: cmd.staticUrl,
  42. SOURCE_MAP: sourceMap
  43. };
  44. run(cmdText, { env: { ...process.env, ...env } });
  45. });
  46. commander.parse(process.argv);
  47. // If no arguments supplied
  48. if (!process.argv.slice(2).length) {
  49. commander.outputHelp();
  50. process.exit(1);
  51. }