Skip to content

Commit

Permalink
Generate Node.js build in two stages
Browse files Browse the repository at this point in the history
This proved necessary in order to preserve all aliases in the new
Node.js 12+ ESM entry point.
  • Loading branch information
jgonggrijp committed Mar 11, 2021
1 parent 45afe0e commit 04b639c
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 242 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ cjs
/underscore.js
/underscore-min.js
/underscore-min.js.map
/underscore-node-*-pre*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"lint": "eslint modules/*.js test/*.js",
"test-node": "npm run prepare-tests && qunit test/",
"test-browser": "npm run prepare-tests && npm i karma-phantomjs-launcher && karma start",
"bundle": "rollup --config && eslint underscore-umd.js",
"bundle": "rollup -c && eslint underscore-umd.js && rollup -c rollup.config2.js",
"bundle-treeshake": "cd test-treeshake && rollup --config",
"prepare-tests": "npm run bundle && npm run bundle-treeshake",
"minify-umd": "terser underscore-umd.js -c \"evaluate=false\" --comments \"/ .*/\" -m",
Expand Down
27 changes: 27 additions & 0 deletions rollup.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { readFileSync } from 'fs';
import { extend } from './underscore-esm.js';

var intro = readFileSync('modules/index.js', 'utf-8').split('\n').slice(3, 7).join('\n');

var outputBase = {
strict: false,
externalLiveBindings: false,
freeze: false,
};

var sourcemapBase = {
sourcemap: true,
sourcemapExcludeSources: true,
};

export function outputConf(particular) {
return extend(particular, outputBase);
}

export function sourcemapConf(particular) {
return extend(particular, outputBase, sourcemapBase);
}

export function monolithConf(particular) {
return extend(particular, outputBase, sourcemapBase, {intro});
}
60 changes: 9 additions & 51 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
import { resolve } from 'path';
import { readFileSync } from 'fs';
import { extend, filter } from './underscore-esm.js';
import glob from 'glob';

var intro = readFileSync('modules/index.js', 'utf-8').split('\n').slice(3, 7).join('\n');

var outputBase = {
strict: false,
externalLiveBindings: false,
freeze: false,
};

var monolithicBase = {
intro,
sourcemap: true,
sourcemapExcludeSources: true,
};

function outputConf(particular) {
return extend(particular, outputBase);
}

function monolithConf(particular) {
return extend(particular, outputBase, monolithicBase);
}

function resolveModule(id) {
return resolve(__dirname, 'modules', id);
}
import { filter } from './underscore-esm.js';
import { outputConf, sourcemapConf, monolithConf } from './rollup.common.js';

export default [
// Monolithic ESM bundle for browsers and deno.
Expand All @@ -54,33 +27,18 @@ export default [
noConflict: true,
}),
},
// Custom CJS build for new Node.js.
// Custom builds for Node.js, first pass. Second pass in rollup.config2.js.
{
input: 'modules/index-default.js',
input: {
'underscore-node-cjs-pre': 'modules/index-default.js',
'underscore-node-mjs-pre': 'modules/index-all.js',
},
treeshake: false,
output: monolithConf({
entryFileNames: 'underscore-node.cjs',
chunkFileNames: 'underscore-node-[name].cjs',
output: sourcemapConf({
chunkFileNames: 'underscore-node-f-pre.js',
dir: '.',
minifyInternalExports: false,
exports: 'auto',
format: 'cjs',
manualChunks: function(path) {
if (!path.match(/index(-default)?\.js$/)) return 'f';
},
}),
},
// Custom ESM build for new Node.js. Thin layer on top of CJS build.
{
input: 'modules/index-all.js',
external: ['./index.js', './index-default.js'],
output: monolithConf({
file: 'underscore-node.mjs',
format: 'esm',
paths: {
[resolveModule('index.js')]: './underscore-node-f.cjs',
[resolveModule('index-default.js')]: './underscore-node.cjs',
},
}),
},
// AMD and CJS versions of the individual modules for development
Expand Down
34 changes: 34 additions & 0 deletions rollup.config2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { resolve } from 'path';
import { monolithConf } from './rollup.common.js';

var sharedInput = './underscore-node-f-pre.js';
var sharedOutput = './underscore-node-f.cjs';

export default [
// ESM entry point for Node.js 12+.
{
input: 'underscore-node-mjs-pre.js',
external: sharedInput,
output: monolithConf({
file: 'underscore-node.mjs',
format: 'esm',
paths: {
[resolve(__dirname, sharedInput)]: sharedOutput,
},
}),
},
// CJS entry point for Node.js 12+, plus code shared with the ESM entry.
{
input: {
'underscore-node-f': sharedInput,
'underscore-node': 'underscore-node-cjs-pre.js',
},
preserveModules: true,
output: monolithConf({
entryFileNames: '[name].cjs',
dir: '.',
exports: 'auto',
format: 'cjs',
}),
},
];
Loading

0 comments on commit 04b639c

Please sign in to comment.