webpack.config.js 5.5 KB

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