extension_helpers.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. var path = require('path');
  4. /*
  5. Helper scripts to be used by extension authors (and extension extenders) in a
  6. webpack.config.json to create builds that do not include upstream extensions.
  7. Inspects the package.json of the user's package and those of its dependencies
  8. to find extensions that should be excluded.
  9. Slightly more than minimal valid setup in package.json:
  10. {
  11. "name": "foo-widget",
  12. "jupyter": {
  13. "lab": {
  14. "main": "lab-extension.js"
  15. }
  16. },
  17. "dependencies": {
  18. "jupyterlab": "*",
  19. "jupyter-js-widgets": "*"
  20. }
  21. }
  22. Example usage in webpack.config.js:
  23. var jlab_helpers = require('jupyterlab/scripts/extension_helpers');
  24. module.exports = [{
  25. entry: './src/lab/extension.js',
  26. output: {
  27. filename: 'lab-extension.js',
  28. path: '../pythonpkg/static',
  29. libraryTarget: 'this'
  30. },
  31. externals: jlab_helpers.upstream_externals(require)
  32. }];
  33. */
  34. // The "always ignore" externals used by JupyterLab, Phosphor and friends
  35. var DEFAULT_EXTERNALS = [
  36. function(context, request, callback){
  37. console.log('TODO: Phosphor external rewriter...');
  38. },
  39. 'jupyter-js-services',
  40. /codemirror/
  41. ];
  42. // determine whether the package JSON contains a JupyterLab extension
  43. function validate_extension(pkg){
  44. try {
  45. // for now, just try to load the key... could check whether file exists?
  46. pkg['jupyter']['lab']['main']
  47. return true;
  48. } catch(err) {
  49. return false;
  50. }
  51. }
  52. // the publicly exposed function
  53. function upstream_externals(_require) {
  54. // remember which packages we have seen
  55. var _seen = {},
  56. // load the user's package.json
  57. _user_pkg = _require('./package.json');
  58. // check for whether this is the root package
  59. function _is_user_pkg(pkg) {
  60. return _user_pkg['name'] === pkg['name'];
  61. }
  62. // use the provided scoped _require and the current nested location
  63. // in the `node_modules` hierarchy to resolve down to the list of externals
  64. function _load_externals(pkg_path, pkg) {
  65. var pkg_externals = [pkg['name']];
  66. try {
  67. pkg_externals = pkg_externals.concat(_require(
  68. pkg_path + '/' + pkg['jupyter']['lab']['externals']));
  69. } catch (err) {
  70. // not really worth adding any output here... usually, just the name will
  71. // suffice
  72. }
  73. return pkg_externals || [];
  74. }
  75. // return an array of strings, functions or regexen that can be deferenced by
  76. // webpack `externals` config directive
  77. // https://webpack.github.io/docs/configuration.html#externals
  78. function _find_externals(pkg_path) {
  79. var pkg = _require(pkg_path + '/package.json'),
  80. lab_config;
  81. // only visit each named package once
  82. _seen[pkg['name']] = true;
  83. if (!validate_extension(pkg)) {
  84. if (!_is_user_pkg(pkg)) {
  85. return [];
  86. } else {
  87. throw Error(
  88. pkg['name'] + ' does not contain a jupyter configuration. ' +
  89. ' Please see TODO: where?'
  90. );
  91. }
  92. }
  93. console.info("Inspecting", pkg['name'],
  94. "for upstream JupyterLab extensions...");
  95. // ok, actually start building the externals. If it is the user package,
  96. // it SHOULDN'T be an external, as this is what the user will use for their
  97. // build... otherwise, load the externals, which is probably
  98. var externals = _is_user_pkg(pkg) ?
  99. DEFAULT_EXTERNALS :
  100. _load_externals(pkg_path, pkg, _require);
  101. // Recurse through the dependencies, and collect anything that has
  102. // a JupyterLab config
  103. return Object.keys(pkg['dependencies'])
  104. .filter(function(key){ return !_seen[key]; })
  105. .reduce(function(externals, dep_name){
  106. return externals.concat(
  107. _find_externals(pkg_path + '/node_modules/' + dep_name));
  108. }, externals);
  109. }
  110. return _find_externals(".");
  111. }
  112. module.exports = {
  113. upstream_externals: upstream_externals,
  114. validate_extension: validate_extension
  115. };