extension_helpers.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. var path = require('path');
  4. var findImports = require('find-imports');
  5. // Get the list of vendor files.
  6. var VENDOR_FILES = findImports('../lib/**/*.js', { flatten: true });
  7. var CODEMIRROR_FILES = VENDOR_FILES.filter(function(importPath) {
  8. return importPath.indexOf('codemirror') !== -1;
  9. });
  10. var codemirrorPaths = CODEMIRROR_FILES.map(function(importPath) {
  11. return importPath.replace('.js', '');
  12. });
  13. VENDOR_FILES = VENDOR_FILES.filter(function (importPath) {
  14. return (importPath.indexOf('codemirror') === -1 &&
  15. importPath.indexOf('phosphor') === -1);
  16. });
  17. /*
  18. Helper scripts to be used by extension authors (and extension extenders) in a
  19. webpack.config.json to create builds that do not include upstream extensions.
  20. Inspects the package.json of the user's package and those of its dependencies
  21. to find extensions that should be excluded.
  22. Slightly more than minimal valid setup in package.json:
  23. {
  24. "name": "foo-widget",
  25. "jupyter": {
  26. "lab": {
  27. "main": "lab-extension.js"
  28. }
  29. },
  30. "dependencies": {
  31. "jupyterlab": "*",
  32. "jupyter-js-widgets": "*"
  33. }
  34. }
  35. Example usage in webpack.config.js:
  36. var jlab_helpers = require('jupyterlab/scripts/extension_helpers');
  37. module.exports = [{
  38. entry: './src/lab/extension.js',
  39. output: {
  40. filename: 'lab-extension.js',
  41. path: '../pythonpkg/static',
  42. libraryTarget: 'this'
  43. },
  44. externals: jlab_helpers.upstream_externals(require)
  45. }];
  46. */
  47. // The "always ignore" externals used by JupyterLab, Phosphor and friends
  48. var BASE_EXTERNALS = [
  49. function(context, request, callback) {
  50. // All phosphor imports get mangled to use the external bundle.
  51. var regex = /^phosphor\/lib\/([a-z\/]+)$/;
  52. if(regex.test(request)) {
  53. var matches = regex.exec(request)[1];
  54. var lib = 'var phosphor.' + matches.split('/').join('.');
  55. return callback(null, lib);
  56. }
  57. callback();
  58. },
  59. {
  60. 'jupyter-js-services': 'jupyter.services',
  61. 'jquery': '$',
  62. 'jquery-ui': '$'
  63. }
  64. ];
  65. // Downstream extensions should exclude JupyterLab itself as well.
  66. var DEFAULT_EXTERNALS = BASE_EXTERNALS + [
  67. function(context, request, callback) {
  68. // JupyterLab imports get mangled to use the external bundle.
  69. regex = /^jupyterlab\/lib\/([a-z\/]+)$/;
  70. if(regex.test(request)) {
  71. var matches = regex.exec(request)[1];
  72. var lib = 'var jupyterlab.' + matches.split('/').join('.');
  73. return callback(null, lib);
  74. }
  75. if (codemirrorPaths.indexOf(request) !== -1) {
  76. return callback(null, 'var CodeMirror');
  77. }
  78. callback();
  79. },
  80. {
  81. 'codemirror': 'CodeMirror',
  82. '../lib/codemirror': 'CodeMirror',
  83. '../../lib/codemirror': 'CodeMirror',
  84. }
  85. ]
  86. // determine whether the package JSON contains a JupyterLab extension
  87. function validate_extension(pkg){
  88. try {
  89. // for now, just try to load the key... could check whether file exists?
  90. pkg['jupyter']['lab']['main']
  91. return true;
  92. } catch(err) {
  93. return false;
  94. }
  95. }
  96. // the publicly exposed function
  97. function upstream_externals(_require) {
  98. // remember which packages we have seen
  99. var _seen = {},
  100. // load the user's package.json
  101. _user_pkg = _require('./package.json');
  102. // check for whether this is the root package
  103. function _is_user_pkg(pkg) {
  104. return _user_pkg['name'] === pkg['name'];
  105. }
  106. // use the provided scoped _require and the current nested location
  107. // in the `node_modules` hierarchy to resolve down to the list of externals
  108. function _load_externals(pkg_path, pkg) {
  109. var pkg_externals = [pkg['name']];
  110. try {
  111. pkg_externals = pkg_externals.concat(_require(
  112. pkg_path + '/' + pkg['jupyter']['lab']['externals']));
  113. } catch (err) {
  114. // not really worth adding any output here... usually, just the name will
  115. // suffice
  116. }
  117. return pkg_externals || [];
  118. }
  119. // return an array of strings, functions or regexen that can be deferenced by
  120. // webpack `externals` config directive
  121. // https://webpack.github.io/docs/configuration.html#externals
  122. function _find_externals(pkg_path) {
  123. var pkg = _require(pkg_path + '/package.json'),
  124. lab_config;
  125. // only visit each named package once
  126. _seen[pkg['name']] = true;
  127. if (!validate_extension(pkg)) {
  128. if (!_is_user_pkg(pkg)) {
  129. return [];
  130. } else {
  131. throw Error(
  132. pkg['name'] + ' does not contain a jupyter configuration. ' +
  133. ' Please see TODO: where?'
  134. );
  135. }
  136. }
  137. console.info("Inspecting", pkg['name'],
  138. "for upstream JupyterLab extensions...");
  139. // ok, actually start building the externals. If it is the user package,
  140. // it SHOULDN'T be an external, as this is what the user will use for their
  141. // build... otherwise, load the externals, which is probably
  142. var externals = _is_user_pkg(pkg) ?
  143. DEFAULT_EXTERNALS :
  144. _load_externals(pkg_path, pkg, _require);
  145. // Recurse through the dependencies, and collect anything that has
  146. // a JupyterLab config
  147. return Object.keys(pkg['dependencies'])
  148. .filter(function(key){ return !_seen[key]; })
  149. .reduce(function(externals, dep_name){
  150. return externals.concat(
  151. _find_externals(pkg_path + '/node_modules/' + dep_name));
  152. }, externals);
  153. }
  154. return _find_externals(".");
  155. }
  156. module.exports = {
  157. upstream_externals: upstream_externals,
  158. validate_extension: validate_extension,
  159. BASE_EXTERNALS: BASE_EXTERNALS,
  160. CODEMIRROR_FILES: CODEMIRROR_FILES,
  161. VENDOR_FILES: VENDOR_FILES
  162. };