Skip to content

Commit 04b639c

Browse files
committed
Generate Node.js build in two stages
This proved necessary in order to preserve all aliases in the new Node.js 12+ ESM entry point.
1 parent 45afe0e commit 04b639c

11 files changed

+265
-242
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ cjs
1111
/underscore.js
1212
/underscore-min.js
1313
/underscore-min.js.map
14+
/underscore-node-*-pre*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"lint": "eslint modules/*.js test/*.js",
7474
"test-node": "npm run prepare-tests && qunit test/",
7575
"test-browser": "npm run prepare-tests && npm i karma-phantomjs-launcher && karma start",
76-
"bundle": "rollup --config && eslint underscore-umd.js",
76+
"bundle": "rollup -c && eslint underscore-umd.js && rollup -c rollup.config2.js",
7777
"bundle-treeshake": "cd test-treeshake && rollup --config",
7878
"prepare-tests": "npm run bundle && npm run bundle-treeshake",
7979
"minify-umd": "terser underscore-umd.js -c \"evaluate=false\" --comments \"/ .*/\" -m",

rollup.common.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readFileSync } from 'fs';
2+
import { extend } from './underscore-esm.js';
3+
4+
var intro = readFileSync('modules/index.js', 'utf-8').split('\n').slice(3, 7).join('\n');
5+
6+
var outputBase = {
7+
strict: false,
8+
externalLiveBindings: false,
9+
freeze: false,
10+
};
11+
12+
var sourcemapBase = {
13+
sourcemap: true,
14+
sourcemapExcludeSources: true,
15+
};
16+
17+
export function outputConf(particular) {
18+
return extend(particular, outputBase);
19+
}
20+
21+
export function sourcemapConf(particular) {
22+
return extend(particular, outputBase, sourcemapBase);
23+
}
24+
25+
export function monolithConf(particular) {
26+
return extend(particular, outputBase, sourcemapBase, {intro});
27+
}

rollup.config.js

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,6 @@
1-
import { resolve } from 'path';
2-
import { readFileSync } from 'fs';
3-
import { extend, filter } from './underscore-esm.js';
41
import glob from 'glob';
5-
6-
var intro = readFileSync('modules/index.js', 'utf-8').split('\n').slice(3, 7).join('\n');
7-
8-
var outputBase = {
9-
strict: false,
10-
externalLiveBindings: false,
11-
freeze: false,
12-
};
13-
14-
var monolithicBase = {
15-
intro,
16-
sourcemap: true,
17-
sourcemapExcludeSources: true,
18-
};
19-
20-
function outputConf(particular) {
21-
return extend(particular, outputBase);
22-
}
23-
24-
function monolithConf(particular) {
25-
return extend(particular, outputBase, monolithicBase);
26-
}
27-
28-
function resolveModule(id) {
29-
return resolve(__dirname, 'modules', id);
30-
}
2+
import { filter } from './underscore-esm.js';
3+
import { outputConf, sourcemapConf, monolithConf } from './rollup.common.js';
314

325
export default [
336
// Monolithic ESM bundle for browsers and deno.
@@ -54,33 +27,18 @@ export default [
5427
noConflict: true,
5528
}),
5629
},
57-
// Custom CJS build for new Node.js.
30+
// Custom builds for Node.js, first pass. Second pass in rollup.config2.js.
5831
{
59-
input: 'modules/index-default.js',
32+
input: {
33+
'underscore-node-cjs-pre': 'modules/index-default.js',
34+
'underscore-node-mjs-pre': 'modules/index-all.js',
35+
},
6036
treeshake: false,
61-
output: monolithConf({
62-
entryFileNames: 'underscore-node.cjs',
63-
chunkFileNames: 'underscore-node-[name].cjs',
37+
output: sourcemapConf({
38+
chunkFileNames: 'underscore-node-f-pre.js',
6439
dir: '.',
6540
minifyInternalExports: false,
66-
exports: 'auto',
67-
format: 'cjs',
68-
manualChunks: function(path) {
69-
if (!path.match(/index(-default)?\.js$/)) return 'f';
70-
},
71-
}),
72-
},
73-
// Custom ESM build for new Node.js. Thin layer on top of CJS build.
74-
{
75-
input: 'modules/index-all.js',
76-
external: ['./index.js', './index-default.js'],
77-
output: monolithConf({
78-
file: 'underscore-node.mjs',
7941
format: 'esm',
80-
paths: {
81-
[resolveModule('index.js')]: './underscore-node-f.cjs',
82-
[resolveModule('index-default.js')]: './underscore-node.cjs',
83-
},
8442
}),
8543
},
8644
// AMD and CJS versions of the individual modules for development

rollup.config2.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { resolve } from 'path';
2+
import { monolithConf } from './rollup.common.js';
3+
4+
var sharedInput = './underscore-node-f-pre.js';
5+
var sharedOutput = './underscore-node-f.cjs';
6+
7+
export default [
8+
// ESM entry point for Node.js 12+.
9+
{
10+
input: 'underscore-node-mjs-pre.js',
11+
external: sharedInput,
12+
output: monolithConf({
13+
file: 'underscore-node.mjs',
14+
format: 'esm',
15+
paths: {
16+
[resolve(__dirname, sharedInput)]: sharedOutput,
17+
},
18+
}),
19+
},
20+
// CJS entry point for Node.js 12+, plus code shared with the ESM entry.
21+
{
22+
input: {
23+
'underscore-node-f': sharedInput,
24+
'underscore-node': 'underscore-node-cjs-pre.js',
25+
},
26+
preserveModules: true,
27+
output: monolithConf({
28+
entryFileNames: '[name].cjs',
29+
dir: '.',
30+
exports: 'auto',
31+
format: 'cjs',
32+
}),
33+
},
34+
];

0 commit comments

Comments
 (0)