Skip to content

feat: transform in common js #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <eight04@gmail.com>",
"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);
`);
})
);
});
Loading