forked from jupyterlab/jupyterlab-module-federation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ext.js
144 lines (119 loc) · 3.59 KB
/
webpack.config.ext.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
136
137
138
139
140
141
142
143
144
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
const Build = require('@jupyterlab/buildutils').Build;
const webpack = require('webpack');
const { merge } = require('webpack-merge');
const baseConfig = require('./webpack.config.base');
const { ModuleFederationPlugin } = webpack.container;
const path = require('path');
const fs = require('fs');
const Ajv = require('ajv');
console.log('1');
const packagePath = process.env.PACKAGE_PATH;
let outputPath = process.env.OUTPUT_PATH;
const nodeEnv = process.env.NODE_ENV;
if (nodeEnv === 'production') {
options.mode = 'production'
}
console.log('2');
const data = require(path.join(packagePath, '/package.json'));
const ajv = new Ajv({ useDefaults: true });
const validate = ajv.compile(require('./metadata_schema.json'));
var valid = validate(data.jupyterlab || {});
if (!valid) {
console.error(validate.errors);
process.exit(1);
}
console.log('3');
outputPath = path.join(outputPath, data.name);
// Handle the extension entry point and the lib entry point, if different
let extEntry = data.jupyterlab.extension || data.jupyterlab.mimeExtension;
const index = require.resolve(packagePath);
const exposes = {
'./index': index,
'./extension': index
}
if (extEntry !== true) {
exposes['./extension'] = path.join(packagePath, extEntry);
}
const coreData = require('./core_package/package.json');
const shared = {};
console.log('4');
// Start with core dependencies.
Object.keys(coreData.dependencies).forEach((element) => {
shared[element] = { requiredVersion: coreData.dependencies[element] };
});
// Add package dependencies.
Object.keys(data.dependencies).forEach((element) => {
if (!shared[element]) {
shared[element] = {};
}
shared[element].requiredVersion = data.dependencies[element];
});
// Remove non-shared.
(data.jupyterlab.nonSharedPackages || []).forEach((element) => {
delete shared[element];
});
// Start with core singletons.
coreData.jupyterlab.singletonPackages.forEach((element) => {
if (!shared[element]) {
shared[element] = {};
}
shared[element].import = false;
shared[element].singleton = true;
});
// Add package singletons.
(data.jupyterlab.singletonPackages || []).forEach((element) => {
if (!shared[element]) {
shared[element] = {};
}
shared[element].import = false;
});
// Remove non-singletons.
(data.jupyterlab.nonSingletonPackages || []).forEach((element) => {
if (!shared[element]) {
shared[element] = {};
}
shared[element].singleton = false;
});
console.log('5');
// Ensure a clean output directory.
fs.rmdirSync(outputPath, { recursive: true });
fs.mkdirSync(outputPath, { recursive: true });
console.log('6');
const extras = Build.ensureAssets({
packageNames: [data.name],
output: outputPath
});
console.log('7');
// Make a bootstrap entrypoint
const entryPoint = path.join(outputPath, 'bootstrap.js');
const bootstrap = 'import("' + exposes['./extension'] + '");'
fs.writeFileSync(entryPoint, bootstrap);
console.log('8');
module.exports = [
merge(baseConfig, {
entry: entryPoint,
output: {
filename: 'extension.js',
path: outputPath,
publicPath: `example/labextensions/${data.name}/`,
},
plugins: [
new ModuleFederationPlugin({
name: data.name,
library: {
type: 'var',
name: ['_JUPYTERLAB', data.name]
},
filename: 'remoteEntry.js',
exposes,
shared,
})
]
})
].concat(extras);
console.log('9');
const logPath = path.join(outputPath, 'build_log.json');
fs.writeFileSync(logPath, JSON.stringify(module.exports, null, ' '));
console.log('10');