Skip to content

Commit

Permalink
Fix bug preventing Rollup from inlining relative CJS modules.
Browse files Browse the repository at this point in the history
Although the upgrade from rollup@1.31.1 to rollup@2.x has been almost
entirely seamless (yay!), rollup@2.26.8 included a change
(rollup/rollup#3753) that made it possible for
the options.external function to receive fully resolved, absolute ID
strings, so our implementation of that function needed to be updated to
accommodate that possibility.
  • Loading branch information
benjamn committed Jun 1, 2021
1 parent 7d70b01 commit ba935ec
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions config/rollup.config.js
Expand Up @@ -7,15 +7,41 @@ const entryPoints = require('./entryPoints');

const distDir = './dist';

function isExternal(id) {
return !(id.startsWith("./") || id.startsWith("../"));
function isExternal(id, parentId, entryPointsAreExternal = true) {
// Rollup v2.26.8 started passing absolute id strings to this function, thanks
// apparently to https://github.com/rollup/rollup/pull/3753, so we relativize
// the id again in those cases.
if (path.posix.isAbsolute(id)) {
id = path.posix.relative(
path.posix.dirname(parentId),
id,
);
if (!id.startsWith(".")) {
id = "./" + id;
}
}

const isRelative =
id.startsWith("./") ||
id.startsWith("../");

if (!isRelative) {
return true;
}

if (entryPointsAreExternal &&
entryPoints.check(id, parentId)) {
return true;
}

return false;
}

function prepareCJS(input, output) {
return {
input,
external(id) {
return isExternal(id);
external(id, parentId) {
return isExternal(id, parentId, false);
},
output: {
file: output,
Expand Down Expand Up @@ -62,7 +88,7 @@ function prepareBundle({
return {
input: `${dir}/index.js`,
external(id, parentId) {
return isExternal(id) || entryPoints.check(id, parentId);
return isExternal(id, parentId, true);
},
output: {
file: `${dir}/${bundleName}.cjs.js`,
Expand Down

0 comments on commit ba935ec

Please sign in to comment.