Skip to content

Commit c7ab565

Browse files
committed
avoid require() externals in UMD bundles
1 parent 4e6d566 commit c7ab565

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsondiffpatch",
3-
"version": "0.3.9",
3+
"version": "0.3.10",
44
"author": "Benjamin Eidelman <[email protected]>",
55
"description": "Diff & Patch for Javascript objects",
66
"contributors": [

rollup-config-factory.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ import Visualizer from 'rollup-plugin-visualizer';
1414
* @param {string} dirName Output destination directory
1515
*/
1616
export function createBrowserUmdBuildConfig(dirName = 'dist') {
17+
const external = [
18+
'chalk',
19+
];
1720
return {
1821
input: 'src/main.js',
19-
external: [
20-
// external node modules
21-
'chalk',
22-
// 'diff-match-patch'
23-
],
22+
external,
2423
output: {
2524
name: pkg.name,
2625
file: pkg.browser.replace(/^dist\//, `${dirName}/`),
2726
format: 'umd',
27+
...outputExternal(external),
2828
},
2929
plugins: [
30+
createEmptyModuleDist(),
3031
replace({ 'process.browser': true }),
3132
babel({
3233
exclude: 'node_modules/**',
@@ -43,26 +44,28 @@ export function createBrowserUmdBuildConfig(dirName = 'dist') {
4344
* @param {string} dirName Output destination directory
4445
*/
4546
export function createSlimBrowserUmdBuildConfig(dirName = 'dist') {
47+
const external = [
48+
'chalk',
49+
'diff-match-patch',
50+
];
4651
return {
4752
input: 'src/main.js',
48-
external: [
49-
// external node modules
50-
'chalk',
51-
'diff-match-patch',
52-
],
53+
external,
5354
output: {
5455
name: pkg.name,
5556
file: pkg.browser
5657
.replace('.js', '.slim.js')
5758
.replace(/^dist\//, `${dirName}/`),
5859
format: 'umd',
60+
...outputExternal(external),
5961
},
6062
plugins: [
6163
new Visualizer({
6264
filename: pkg.browser
6365
.replace('.js', '.slim.stats.html')
6466
.replace(/^dist\//, `${dirName}/`),
6567
}),
68+
createEmptyModuleDist(),
6669
replace({ 'process.browser': true }),
6770
babel({
6871
exclude: 'node_modules/**',
@@ -208,6 +211,9 @@ export const createBrowserTestBuild = (
208211
.replace(/^dist\//, `${dirName}/`),
209212
sourcemap: true,
210213
format: 'umd',
214+
globals: {
215+
'chalk': 'chalk',
216+
},
211217
},
212218
};
213219
};
@@ -235,3 +241,37 @@ function copyFromFolderToDist(folder) {
235241
};
236242
};
237243
}
244+
245+
function createEmptyModuleDist() {
246+
return function() {
247+
let executed = false;
248+
return {
249+
ongenerate: () => {
250+
if (executed) {
251+
return;
252+
}
253+
const distFilename = path.join(__dirname, 'dist', 'empty.js');
254+
mkdirp(path.dirname(distFilename));
255+
fs.writeFileSync(distFilename, '');
256+
console.log(`dist/empty.js (created)`);
257+
executed = true;
258+
},
259+
};
260+
};
261+
}
262+
263+
function outputExternal(names) {
264+
if (!names || names.length < 1) {
265+
return;
266+
}
267+
return {
268+
globals: names.reduce((accum, name) => ({
269+
...accum,
270+
[name]: name,
271+
}), {}),
272+
paths: names.reduce((accum, name) => ({
273+
...accum,
274+
[name]: './empty',
275+
}), {}),
276+
};
277+
}

0 commit comments

Comments
 (0)