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