Skip to content

Commit

Permalink
Change: resolve id to external, fix commonjs issue (#47)
Browse files Browse the repository at this point in the history
* resolve commonjs

* feat: transform cjs

* feat: external global in cjs

* fix: format

* fix: delete unused configuration

* Fix: update lock

---------

Co-authored-by: eight04 <[email protected]>
  • Loading branch information
Shunjun and eight04 authored Aug 11, 2024
1 parent 6b2acd9 commit 443c690
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage
.eslintcache
.idea
*.tgz
pnpm-lock.yaml
25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const {createFilter} = require("@rollup/pluginutils");
const importToGlobals = require("./lib/import-to-globals");
const defaultDynamicWrapper = id => `Promise.resolve(${id})`;

function isVirtualModule(id) {
return id.startsWith("\0");
}

function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper} = {}) {
if (!globals) {
throw new TypeError("Missing mandatory option 'globals'");
Expand All @@ -24,14 +28,31 @@ function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynami
if (dynamicWrapperType !== "function") {
throw new TypeError(`Unexpected type of 'dynamicWrapper', got '${dynamicWrapperType}'`);
}
async function resolveId(importee, _, options) {
if (isVirtualModule(importee) || options.isEntry) return null;
const globalName = getName(importee)
return globalName ? false : null;
}
const filter = createFilter(include, exclude);
return {
name: "rollup-plugin-external-globals",
options,
transform
};

async function options(rawOptions) {
const plugins = Array.isArray(rawOptions.plugins)
? [...rawOptions.plugins]
: rawOptions.plugins
? [rawOptions.plugins]
: [];
plugins.unshift({
name: 'rollup-plugin-external-globals--resolver',
resolveId
});
return { ...rawOptions, plugins };
}
async function transform(code, id) {
if ((id[0] !== "\0" && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
if ((!isVirtualModule(id) && !filter(id)) || (isGlobalsObj && Object.keys(globals).every(id => !code.includes(id)))) {
return;
}
let ast;
Expand Down
98 changes: 98 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"author": "eight04 <[email protected]>",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-commonjs": "^26.0.1",
"c8": "^10.1.2",
"endent": "^2.1.0",
"eslint": "^9.5.0",
Expand Down
138 changes: 138 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require("assert");
const rollup = require("rollup");
const {withDir} = require("tempdir-yaml");
const {default: endent} = require("endent");
const commonjs = require("@rollup/plugin-commonjs");

const createPlugin = require("..");

Expand Down Expand Up @@ -482,4 +483,141 @@ describe("main", () => {
`);
})
);

it("require from default", () =>
withDir(`
- node_modules:
- bar:
- index.js: |
module.exports = "BAR";
- foo:
- index.js: |
const bar = require("bar");
console.log('foo');
module.exports = (val) => console.log(val || bar);
- entry.js: |
import log from "foo";
import bar from "bar"
log(bar);
`, async resolve => {
const {output: {"entry.js": {code}}} = await bundle(
resolve("entry.js"),
{
bar: "BAR"
},
{
plugins: [
commonjs({
defaultIsModuleExports: true
}),
{
name: "test",
resolveId(importee) {
if (["foo", "bar"].includes(importee)) {
return resolve(`node_modules/${importee}/index.js`)
}
}
}]
}
);
assert.equal(code.trim(), endent`
const _global_BAR = BAR;
const bar = _global_BAR;
console.log('foo');
var foo = (val) => console.log(val || bar);
foo(BAR);
`);
})
);

it("require from named exports", () =>
withDir(`
- bar.js: |
module.a = "A";
- entry.js: |
const { a } = require("bar");
console.log(a);
`, async resolve => {
const {output: {"entry.js": {code}}} = await bundle(
resolve("entry.js"),
{
bar: "BAR"
},
{
plugins: [
commonjs({
defaultIsModuleExports: true
}),
{
name: "test",
resolveId(importee) {
if (["bar"].includes(importee)) {
return resolve(`${importee}.js`)
}
}
}]
}
);
assert.equal(code.trim(), endent`
var entry = {};
const _global_BAR = BAR;
const { a } = _global_BAR;
console.log(a);
export { entry as default };
`);
})
);

it("require in function", () =>
withDir(`
- bar.js: |
module.a = "A";
- foo.js: |
const a = "A"
module.exports = (val) => {
const { a } = require("bar");
console.log(a);
}
- entry.js: |
import log from "foo";
import { a } from "bar";
log(a);
`, async resolve => {
const {output: {"entry.js": {code}}} = await bundle(
resolve("entry.js"),
{
bar: "BAR"
},
{
plugins: [
commonjs({
defaultIsModuleExports: true
}),
{
name: "test",
resolveId(importee) {
if (["foo", "bar"].includes(importee)) {
return resolve(`${importee}.js`)
}
}
}]
}
);
assert.equal(code.trim(), endent`
const _global_BAR = BAR;
var foo = (val) => {
const { a } = _global_BAR;
console.log(a);
};
foo(BAR.a);
`);
})
);
});

0 comments on commit 443c690

Please sign in to comment.