123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /* -----------------------------------------------------------------------------
- | Copyright (c) Jupyter Development Team.
- | Distributed under the terms of the Modified BSD License.
- |----------------------------------------------------------------------------*/
- const plib = require('path');
- const fs = require('fs-extra');
- const Handlebars = require('handlebars');
- const HtmlWebpackPlugin = require('html-webpack-plugin');
- const webpack = require('webpack');
- const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
- .BundleAnalyzerPlugin;
- const Build = require('@jupyterlab/buildutils').Build;
- const WPPlugin = require('@jupyterlab/buildutils').WPPlugin;
- const package_data = require('./package.json');
- // Handle the extensions.
- const jlab = package_data.jupyterlab;
- const extensions = jlab.extensions;
- const mimeExtensions = jlab.mimeExtensions;
- const { externalExtensions } = jlab;
- const packageNames = Object.keys(mimeExtensions).concat(
- Object.keys(extensions),
- Object.keys(externalExtensions)
- );
- // go throught each external extension
- // add to mapping of extension and mime extensions, of package name
- // to path of the extension.
- for (const key in externalExtensions) {
- const {
- jupyterlab: { extension, mimeExtension }
- } = require(`${key}/package.json`);
- if (extension !== undefined) {
- extensions[key] = extension === true ? '' : extension;
- }
- if (mimeExtension !== undefined) {
- mimeExtensions[key] = mimeExtension === true ? '' : mimeExtension;
- }
- }
- // Ensure a clear build directory.
- const buildDir = plib.resolve(jlab.buildDir);
- if (fs.existsSync(buildDir)) {
- fs.removeSync(buildDir);
- }
- fs.ensureDirSync(buildDir);
- // Build the assets
- const extraConfig = Build.ensureAssets({
- packageNames: packageNames,
- output: jlab.outputDir
- });
- // Create the entry point file.
- const source = fs.readFileSync('index.js').toString();
- const template = Handlebars.compile(source);
- const data = {
- jupyterlab_extensions: extensions,
- jupyterlab_mime_extensions: mimeExtensions
- };
- const result = template(data);
- fs.writeFileSync(plib.join(buildDir, 'index.out.js'), result);
- fs.copySync('./package.json', plib.join(buildDir, 'package.json'));
- fs.copySync(
- plib.join(jlab.outputDir, 'imports.css'),
- plib.join(buildDir, 'imports.css')
- );
- // Set up variables for the watch mode ignore plugins
- const watched = {};
- const ignoreCache = Object.create(null);
- Object.keys(jlab.linkedPackages).forEach(function(name) {
- if (name in watched) {
- return;
- }
- const localPkgPath = require.resolve(plib.join(name, 'package.json'));
- watched[name] = plib.dirname(localPkgPath);
- });
- // Set up source-map-loader to look in watched lib dirs
- const sourceMapRes = Object.values(watched).reduce((res, name) => {
- res.push(new RegExp(name + '/lib'));
- return res;
- }, []);
- /**
- * A filter function set up to exclude all files that are not
- * in a package contained by the Jupyterlab repo. Used to ignore
- * files during a `--watch` build.
- */
- function ignored(path) {
- path = plib.resolve(path);
- if (path in ignoreCache) {
- // Bail if already found.
- return ignoreCache[path];
- }
- // Limit the watched files to those in our local linked package dirs.
- let ignore = true;
- Object.keys(watched).some(name => {
- const rootPath = watched[name];
- const contained = path.indexOf(rootPath + plib.sep) !== -1;
- if (path !== rootPath && !contained) {
- return false;
- }
- const rest = path.slice(rootPath.length);
- if (rest.indexOf('node_modules') === -1) {
- ignore = false;
- }
- return true;
- });
- ignoreCache[path] = ignore;
- return ignore;
- }
- const plugins = [
- new WPPlugin.NowatchDuplicatePackageCheckerPlugin({
- verbose: true,
- exclude(instance) {
- // ignore known duplicates
- return ['domelementtype', 'hash-base', 'inherits'].includes(
- instance.name
- );
- }
- }),
- new HtmlWebpackPlugin({
- chunksSortMode: 'none',
- template: plib.join('templates', 'template.html'),
- title: jlab.name || 'JupyterLab'
- }),
- new webpack.HashedModuleIdsPlugin(),
- // custom plugin for ignoring files during a `--watch` build
- new WPPlugin.FilterWatchIgnorePlugin(ignored),
- // custom plugin that copies the assets to the static directory
- new WPPlugin.FrontEndPlugin(buildDir, jlab.staticDir)
- ];
- if (process.argv.includes('--analyze')) {
- plugins.push(new BundleAnalyzerPlugin());
- }
- module.exports = [
- {
- mode: 'development',
- entry: {
- main: ['whatwg-fetch', plib.resolve(buildDir, 'index.out.js')]
- },
- // Map Phosphor files to Lumino files.
- resolve: {
- alias: {
- '@phosphor/algorithm$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/algorithm/lib/index.js'
- ),
- '@phosphor/application$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/application/lib/index.js'
- ),
- '@phosphor/commands$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/commands/lib/index.js'
- ),
- '@phosphor/coreutils$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/coreutils/lib/index.js'
- ),
- '@phosphor/disposable$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/disposable/lib/index.js'
- ),
- '@phosphor/domutils$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/domutils/lib/index.js'
- ),
- '@phosphor/dragdrop$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/dragdrop/lib/index.js'
- ),
- '@phosphor/dragdrop/style': plib.resolve(
- __dirname,
- 'node_modules/@lumino/widgets/style'
- ),
- '@phosphor/messaging$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/messaging/lib/index.js'
- ),
- '@phosphor/properties$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/properties/lib'
- ),
- '@phosphor/signaling': plib.resolve(
- __dirname,
- 'node_modules/@lumino/signaling/lib/index.js'
- ),
- '@phosphor/widgets/style': plib.resolve(
- __dirname,
- 'node_modules/@lumino/widgets/style'
- ),
- '@phosphor/virtualdom$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/virtualdom/lib/index.js'
- ),
- '@phosphor/widgets$': plib.resolve(
- __dirname,
- 'node_modules/@lumino/widgets/lib/index.js'
- )
- }
- },
- output: {
- path: plib.resolve(buildDir),
- publicPath: '{{page_config.fullStaticUrl}}/',
- filename: '[name].[chunkhash].js'
- },
- optimization: {
- splitChunks: {
- chunks: 'all'
- }
- },
- module: {
- rules: [
- { test: /\.css$/, use: ['style-loader', 'css-loader'] },
- { test: /\.md$/, use: 'raw-loader' },
- { test: /\.txt$/, use: 'raw-loader' },
- {
- test: /\.js$/,
- include: sourceMapRes,
- use: ['source-map-loader'],
- enforce: 'pre'
- },
- { test: /\.(jpg|png|gif)$/, use: 'file-loader' },
- { test: /\.js.map$/, use: 'file-loader' },
- {
- test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
- use: 'url-loader?limit=10000&mimetype=application/font-woff'
- },
- {
- test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
- use: 'url-loader?limit=10000&mimetype=application/font-woff'
- },
- {
- test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
- use: 'url-loader?limit=10000&mimetype=application/octet-stream'
- },
- {
- test: /\.otf(\?v=\d+\.\d+\.\d+)?$/,
- use: 'url-loader?limit=10000&mimetype=application/octet-stream'
- },
- { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, use: 'file-loader' },
- {
- // In .css files, svg is loaded as a data URI.
- test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
- issuer: { test: /\.css$/ },
- use: {
- loader: 'svg-url-loader',
- options: { encoding: 'none', limit: 10000 }
- }
- },
- {
- // In .ts and .tsx files (both of which compile to .js), svg files
- // must be loaded as a raw string instead of data URIs.
- test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
- issuer: { test: /\.js$/ },
- use: {
- loader: 'raw-loader'
- }
- }
- ]
- },
- watchOptions: {
- poll: 500,
- aggregateTimeout: 1000
- },
- node: {
- fs: 'empty'
- },
- bail: true,
- devtool: 'inline-source-map',
- externals: ['node-fetch', 'ws'],
- plugins
- }
- ].concat(extraConfig);
|