Skip to content

Commit

Permalink
feat(core): Enable chunking for CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 27, 2018
1 parent a502b36 commit 480f20d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
4 changes: 3 additions & 1 deletion packages/pectin-api/lib/pectin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ async function isUpToDate(opts, config) {
}

// only need to test one output since all are built simultaneously
const outFile = config.output[0].file;
const outFile = config.output[0].dir
? path.join(config.output[0].dir, config.output[0].entryFileNames)
: config.output[0].file;

// short-circuit if output hasn't been built yet
let outputStat;
Expand Down
41 changes: 26 additions & 15 deletions packages/pectin-core/lib/getOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,41 @@ const npa = require('npm-package-arg');
const dotProp = require('dot-prop');

module.exports = function getOutput(pkg, cwd, isMultiConfig) {
const output = [
{
file: path.resolve(cwd, pkg.main),
format: 'cjs',
},
];
const output = [];

// generated chunks as of rollup v0.68.0 need chunkFileNames, not entryFileNames
const chunkFileNames = dotProp.get(pkg, 'rollup.chunkFileNames', '[name]-[hash].[format].js');
const entryFileNames = dotProp.get(pkg, 'rollup.entryFileNames', '[name].[format].js');

const cjsConfig = {
format: 'cjs',
};

if (isMultiConfig) {
cjsConfig.dir = path.dirname(path.resolve(cwd, pkg.main));
cjsConfig.chunkFileNames = chunkFileNames;
// only one entry point, thus no pattern is required
cjsConfig.entryFileNames = path.basename(pkg.main);
} else {
cjsConfig.file = path.resolve(cwd, pkg.main);
}

output.push(cjsConfig);

if (pkg.module) {
const cfg = {
const esmConfig = {
format: 'esm',
};

if (isMultiConfig) {
// code splitting is only enabled for multi-config output
cfg.dir = path.dirname(path.resolve(cwd, pkg.module));

// generated chunks as of rollup v0.68.0 need chunkFileNames, not entryFileNames
cfg.chunkFileNames = dotProp.get(pkg, 'rollup.chunkFileNames', '[name]-[hash].esm.js');
cfg.entryFileNames = dotProp.get(pkg, 'rollup.entryFileNames', '[name].esm.js');
esmConfig.dir = path.dirname(path.resolve(cwd, pkg.module));
esmConfig.chunkFileNames = chunkFileNames;
esmConfig.entryFileNames = entryFileNames;
} else {
cfg.file = path.resolve(cwd, pkg.module);
esmConfig.file = path.resolve(cwd, pkg.module);
}

output.push(cfg);
output.push(esmConfig);
}

// @see https://github.com/defunctzombie/package-browser-field-spec
Expand Down
3 changes: 2 additions & 1 deletion packages/pectin-core/lib/pectin-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function createMultiConfig(pkg, { cwd }) {
output: [output],
plugins,
// TODO: remove when rollup 1.0 makes this the default
experimentalCodeSplitting: output.format === 'esm',
experimentalCodeSplitting: true,
inlineDynamicImports: output.browser === true || output.format === 'umd',
};
});
}
18 changes: 11 additions & 7 deletions packages/pectin-core/test/pectin-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,13 @@ export default function main() {
const results = await generateResults(configs);

const fileNames = results.map(result => `dist/${result.fileName}`);
const [cjsEntry, esmEntry, esmChunk] = results.map(
const [cjsEntry, cjsChunk, esmEntry, esmChunk] = results.map(
result => `// dist/${result.fileName}\n${result.code}`
);

expect(fileNames).toStrictEqual([
'dist/index.js',
'dist/chunk-cbdffb19.cjs.js',
'dist/index.esm.js',
'dist/chunk-a2193cac.esm.js',
]);
Expand All @@ -238,16 +239,19 @@ export default function main() {
'use strict';
function main() {
return Promise.resolve().then(function () { return chunkyBacon$1; });
return Promise.resolve(require('./chunk-cbdffb19.cjs.js'));
}
var chunkyBacon = '_why';
module.exports = main;
"
`);
expect(cjsChunk).toMatchInlineSnapshot(`
"// dist/chunk-cbdffb19.cjs.js
'use strict';
var chunkyBacon$1 = /*#__PURE__*/Object.freeze({
default: chunkyBacon
});
var chunkyBacon = '_why';
module.exports = main;
exports.default = chunkyBacon;
"
`);
expect(esmEntry).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ describe('rollup-config-pectin', () => {
},
`
Object {
"experimentalCodeSplitting": false,
"experimentalCodeSplitting": true,
"inlineDynamicImports": false,
"input": "<REPO_ROOT>/packages/rollup-config-pectin/src/rollup-config-pectin.js",
"output": Array [
Object {
"chunkFileNames": "[name]-[hash].[format].js",
"dir": "<REPO_ROOT>/packages/rollup-config-pectin/lib",
"entryFileNames": "rollup-config-pectin.js",
"exports": "auto",
"file": "<REPO_ROOT>/packages/rollup-config-pectin/lib/rollup-config-pectin.js",
"format": "cjs",
},
],
Expand Down

0 comments on commit 480f20d

Please sign in to comment.