webpack.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. var webpack = require('webpack');
  2. var path = require('path');
  3. var fs = require('fs-extra');
  4. var Handlebars = require('handlebars');
  5. var Build = require('@jupyterlab/buildutils').Build;
  6. var package_data = require('./package.json');
  7. // Ensure a clear build directory.
  8. var buildDir = path.resolve('./build');
  9. if (fs.existsSync(buildDir)) {
  10. fs.removeSync(buildDir);
  11. }
  12. fs.ensureDirSync(buildDir);
  13. // Handle the extensions.
  14. var jlab = package_data.jupyterlab;
  15. var extensions = jlab.extensions;
  16. var mimeExtensions = jlab.mimeExtensions;
  17. Build.ensureAssets({
  18. packageNames: Object.keys(mimeExtensions).concat(Object.keys(extensions)),
  19. output: jlab.outputDir
  20. });
  21. // Create the entry point file.
  22. var source = fs.readFileSync('index.js').toString();
  23. var template = Handlebars.compile(source);
  24. var data = {
  25. jupyterlab_extensions: extensions,
  26. jupyterlab_mime_extensions: mimeExtensions,
  27. };
  28. var result = template(data);
  29. fs.writeFileSync(path.join(buildDir, 'index.out.js'), result);
  30. fs.copySync('./package.json', path.join(buildDir, 'package.json'));
  31. // Set up variables for watch mode.
  32. var localLinked = {};
  33. var ignoreCache = new Map();
  34. Object.keys(jlab.linkedPackages).forEach(function (name) {
  35. var localPath = require.resolve(path.join(name, 'package.json'));
  36. localLinked[name] = path.dirname(localPath);
  37. });
  38. /**
  39. * Sync a local path to a linked package path if they are files and differ.
  40. */
  41. function maybeSync(localPath, name, rest) {
  42. var stats = fs.statSync(localPath);
  43. if (!stats.isFile(localPath)) {
  44. return;
  45. }
  46. var source = fs.realpathSync(path.join(jlab.linkedPackages[name], rest));
  47. if (source === fs.realpathSync(localPath)) {
  48. return;
  49. }
  50. fs.watchFile(source, { 'interval': 500 }, function(curr) {
  51. if (!curr || curr.nlink === 0) {
  52. return;
  53. }
  54. try {
  55. console.log('updating', path.join(name, rest));
  56. fs.copySync(source, localPath);
  57. } catch (err) {
  58. console.error(err);
  59. }
  60. });
  61. }
  62. /**
  63. * A WebPack Plugin that copies the assets to the static directory.
  64. */
  65. function JupyterLabPlugin(options) {
  66. _first = true;
  67. }
  68. JupyterLabPlugin.prototype.apply = function(compiler) {
  69. compiler.plugin('after-emit', function(compilation, callback) {
  70. var staticDir = jlab.staticDir;
  71. if (!staticDir) {
  72. callback();
  73. return;
  74. }
  75. // Ensure a clean static directory on the first emit.
  76. if (this._first && fs.existsSync(staticDir)) {
  77. fs.removeSync(staticDir);
  78. }
  79. this._first = false;
  80. fs.copySync(buildDir, staticDir);
  81. callback();
  82. }.bind(this));
  83. };
  84. module.exports = {
  85. entry: path.resolve(buildDir, 'index.out.js'),
  86. output: {
  87. path: path.resolve(buildDir),
  88. filename: '[name].bundle.js'
  89. },
  90. module: {
  91. rules: [
  92. { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  93. { test: /\.json$/, use: 'json-loader' },
  94. { test: /\.html$/, use: 'file-loader' },
  95. { test: /\.md$/, use: 'raw-loader' },
  96. { test: /\.js$/, use: ['source-map-loader'], enforce: 'pre',
  97. exclude: path.join(process.cwd(), 'node_modules')
  98. },
  99. { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
  100. { test: /\.js.map$/, use: 'file-loader' },
  101. { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
  102. { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=application/font-woff' },
  103. { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=application/octet-stream' },
  104. { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
  105. { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, use: 'url-loader?limit=10000&mimetype=image/svg+xml' }
  106. ],
  107. },
  108. watchOptions: {
  109. ignored: function(localPath) {
  110. localPath = path.resolve(localPath);
  111. if (ignoreCache.has(localPath)) {
  112. return ignoreCache.get(localPath);
  113. }
  114. // Limit the watched files to those in our local linked package dirs.
  115. var ignore = true;
  116. Object.keys(localLinked).some(function (name) {
  117. // Bail if already found.
  118. var rootPath = localLinked[name];
  119. var contained = localPath.indexOf(rootPath + path.sep) !== -1;
  120. if (localPath !== rootPath && !contained) {
  121. return false;
  122. }
  123. var rest = localPath.slice(rootPath.length);
  124. if (rest.indexOf('node_modules') === -1) {
  125. ignore = false;
  126. maybeSync(localPath, name, rest);
  127. }
  128. return true;
  129. });
  130. ignoreCache.set(localPath, ignore);
  131. return ignore;
  132. }
  133. },
  134. node: {
  135. fs: 'empty'
  136. },
  137. bail: true,
  138. devtool: 'cheap-source-map',
  139. plugins: [ new JupyterLabPlugin({}) ]
  140. }