From 015aab0103fbb4958ef55ed1089648909b6f175d Mon Sep 17 00:00:00 2001 From: shunzi Date: Wed, 24 Jul 2024 23:52:17 +0800 Subject: [PATCH 1/6] resolve commonjs --- .gitignore | 1 + index.js | 37 +++++++++++++++++++++++++++--- package.json | 2 ++ test/test.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index dc13f67..530f054 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage .eslintcache .idea *.tgz +pnpm-lock.yaml \ No newline at end of file diff --git a/index.js b/index.js index 2289de3..4677bcb 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,17 @@ const MagicString = require("magic-string"); -const {createFilter} = require("@rollup/pluginutils"); +const { createFilter } = require("@rollup/pluginutils"); const importToGlobals = require("./lib/import-to-globals"); const defaultDynamicWrapper = id => `Promise.resolve(${id})`; -function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper} = {}) { + +function isVirtualModule(id) { + console.log(id); + + return id.startsWith("\0"); +} + +function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper } = {}) { if (!globals) { throw new TypeError("Missing mandatory option 'globals'"); } @@ -24,14 +31,38 @@ function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynami if (dynamicWrapperType !== "function") { throw new TypeError(`Unexpected type of 'dynamicWrapper', got '${dynamicWrapperType}'`); } + const resolvedGlobalIdMap = new Map() + async function resolveId(importee, importer, options) { + const globalName = getName(importee) + if (globalName) { + const resolvedId = await this.resolve(importee, importer, options) + if (resolvedId.id && !isVirtualModule(resolvedId.id)) { + resolvedGlobalIdMap.set(globalName, resolvedId) + } + } + return 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; diff --git a/package.json b/package.json index 45bbbd3..d8ddb18 100644 --- a/package.json +++ b/package.json @@ -36,9 +36,11 @@ "author": "eight04 ", "license": "MIT", "devDependencies": { + "@rollup/plugin-commonjs": "^26.0.1", "c8": "^10.1.2", "endent": "^2.1.0", "eslint": "^9.5.0", + "globals": "^15.8.0", "mocha": "^10.5.2", "rollup": "^4.18.0", "tempdir-yaml": "^0.3.0" diff --git a/test/test.js b/test/test.js index 0f4a284..4e0fbc5 100644 --- a/test/test.js +++ b/test/test.js @@ -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(".."); @@ -482,4 +483,68 @@ describe("main", () => { `); }) ); + it("transform commonjs", () => + withDir(` + - node_modules: + - bar: + - index.js: | + exports.default = "BAR"; + - foo: + - index.js: | + const bar = require("bar"); + console.log('foo'); + exports.default = () => console.log(bar); + - entry.js: | + import log from "foo"; + log(); + `, async resolve => { + const {output: {"entry.js": {code}}} = await bundle( + resolve("entry.js"), + { + bar: "BAR" + }, + { + plugins: [ + commonjs({ + defaultIsModuleExports: true + }), + { + name: "test", + transform(code, id) { + // console.log('------------------------------') + // console.log(id) + // console.log('vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv') + + // // console.log(code) + // // if (id.includes('foo')) { + // // console.log(code) + // // } + // console.log('------------------------------') + }, + resolveId(importee , importer , options) { + // console.log('------------------------------') + // console.log(importee) + // console.log('vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv') + // console.log(importer , options); + + // console.log('------------------------------') + + + + if(importee === 'foo') { + return resolve('node_modules/foo/index.js') + } + if(importee === 'bar') { + return resolve('node_modules/bar/index.js') + } + } + }] + } + ); + assert.equal(code.trim(), endent` + + `); + }) + ); + }); From e601c53c79326555a993f70c0ebb71c17a1c622c Mon Sep 17 00:00:00 2001 From: shunzi Date: Sun, 4 Aug 2024 23:50:35 +0800 Subject: [PATCH 2/6] feat: transform cjs --- index.js | 35 +++--- lib/helpers.js | 28 +++++ lib/import-to-globals.js | 36 ++++-- test/test.js | 237 +++++++++++++++++++++++++++++---------- 4 files changed, 252 insertions(+), 84 deletions(-) create mode 100644 lib/helpers.js diff --git a/index.js b/index.js index 4677bcb..24d5e82 100644 --- a/index.js +++ b/index.js @@ -2,16 +2,10 @@ const MagicString = require("magic-string"); const { createFilter } = require("@rollup/pluginutils"); const importToGlobals = require("./lib/import-to-globals"); +const { PROXY_SUFFIX, WRAPPED_SUFFIX, isVirtualModule, normaliseVirtualId } = require("./lib/helpers"); const defaultDynamicWrapper = id => `Promise.resolve(${id})`; - -function isVirtualModule(id) { - console.log(id); - - return id.startsWith("\0"); -} - -function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper } = {}) { +function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper, transformInCommonJs } = {}) { if (!globals) { throw new TypeError("Missing mandatory option 'globals'"); } @@ -32,15 +26,28 @@ function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynam throw new TypeError(`Unexpected type of 'dynamicWrapper', got '${dynamicWrapperType}'`); } const resolvedGlobalIdMap = new Map() + function getNameInternal(id) { + const name = getName(id); + if (name) return name; + if (id.endsWith(PROXY_SUFFIX) || id.endsWith(WRAPPED_SUFFIX)) { + return resolvedGlobalIdMap.get(normaliseVirtualId(id)); + } + } async function resolveId(importee, importer, options) { - const globalName = getName(importee) - if (globalName) { - const resolvedId = await this.resolve(importee, importer, options) - if (resolvedId.id && !isVirtualModule(resolvedId.id)) { - resolvedGlobalIdMap.set(globalName, resolvedId) + if (!transformInCommonJs) return null + try { + const globalName = getName(importee) + if (globalName) { + const resolvedId = await this.resolve(importee, importer, options) + if (resolvedId.id && !isVirtualModule(resolvedId.id) && !resolvedId.external) { + resolvedGlobalIdMap.set(resolvedId.id, globalName) + } } + } catch { + // ignore } return null + } const filter = createFilter(include, exclude); return { @@ -79,7 +86,7 @@ function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynam const isTouched = await importToGlobals({ ast, code, - getName, + getName: getNameInternal, getDynamicWrapper: dynamicWrapper }); return isTouched ? { diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 0000000..ede6eba --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,28 @@ +const PROXY_SUFFIX = '?commonjs-proxy'; +const WRAPPED_SUFFIX = '?commonjs-wrapped'; +const REQUIREFUNC_ID = "__require" + +const isWrappedId = (id, suffix) => id.endsWith(suffix); + +function isVirtualModule(id) { + return id.startsWith("\0"); +} + +function normaliseVirtualId(id) { + id = id.replace(/^\0/, '') + if (id.endsWith(PROXY_SUFFIX)) { + return id.slice(0, -PROXY_SUFFIX.length); + } + if (id.endsWith(WRAPPED_SUFFIX)) { + return id.slice(0, -WRAPPED_SUFFIX.length); + } +} + +module.exports = { + PROXY_SUFFIX, + WRAPPED_SUFFIX, + REQUIREFUNC_ID, + isWrappedId, + isVirtualModule, + normaliseVirtualId +} \ No newline at end of file diff --git a/lib/import-to-globals.js b/lib/import-to-globals.js index 508dbdb..4020db0 100644 --- a/lib/import-to-globals.js +++ b/lib/import-to-globals.js @@ -1,21 +1,28 @@ -const {attachScopes, makeLegalIdentifier} = require("@rollup/pluginutils"); +const { attachScopes, makeLegalIdentifier } = require("@rollup/pluginutils"); +const { isWrappedId, WRAPPED_SUFFIX, REQUIREFUNC_ID } = require("./helpers") let walk, isReference; async function prepare() { - [{walk}, {default: isReference}] = await Promise.all([ + [{ walk }, { default: isReference }] = await Promise.all([ import("estree-walker"), import("is-reference") ]); } -function analyzeImport(node, importBindings, code, getName, globals) { - const name = node.source.value && getName(node.source.value); +function analyzeImport(node, importBindings, requireFuncBindings, code, getName, globals) { + const id = node.source.value; + const name = id && getName(id); if (!name) { return false; } globals.add(name); + const isWrappedSuffix = isWrappedId(id, WRAPPED_SUFFIX) for (const spec of node.specifiers) { + if (isWrappedSuffix && spec.imported.name === REQUIREFUNC_ID) { + requireFuncBindings.set(spec.local.name, name); + continue; + } importBindings.set(spec.local.name, makeGlobalName( spec.imported ? spec.imported.name : "default", name @@ -66,12 +73,17 @@ function writeIdentifier(code, node, parent, name) { parent.local.isOverwritten = true; parent.exported.isOverwritten = true; } else { - code.overwrite(node.start, node.end, name, {contentOnly: true}); + code.overwrite(node.start, node.end, name, { contentOnly: true }); // FIXME: do we need this? node.isOverwritten = true; } } +function writeCallExpression(code, node, name) { + code.overwrite(node.start, node.end, name); + node.isOverwritten = true; +} + function analyzeExportNamed(node, code, getName, tempNames) { if (node.declaration || !node.source || !node.source.value) { return false; @@ -126,10 +138,11 @@ class ExportLeftHand { } } -async function importToGlobals({ast, code, getName, getDynamicWrapper}) { +async function importToGlobals({ ast, code, getName, getDynamicWrapper }) { await prepare(); let scope = attachScopes(ast, "scope"); const bindings = new Map; + const requireFuncBindings = new Map; const globals = new Set; let isTouched = false; const tempNames = new Set; @@ -137,12 +150,12 @@ async function importToGlobals({ast, code, getName, getDynamicWrapper}) { for (const node of ast.body) { if (node.type === "ImportDeclaration") { - isTouched = analyzeImport(node, bindings, code, getName, globals) || isTouched; + isTouched = analyzeImport(node, bindings, requireFuncBindings, code, getName, globals) || isTouched; } else if (node.type === "ExportNamedDeclaration") { isTouched = analyzeExportNamed(node, code, getName, tempNames) || isTouched; } } - + let topStatement; walk(ast, { enter(node, parent) { @@ -164,6 +177,11 @@ async function importToGlobals({ast, code, getName, getDynamicWrapper}) { } else { writeIdentifier(code, node, parent, bindings.get(node.name)); } + } else if (requireFuncBindings.has(node.name) && !scope.contains(node.name)) { + if (parent.type === "CallExpression" && parent.callee === node) { + writeCallExpression(code, parent, requireFuncBindings.get(node.name)); + this.skip(); + } } else if (globals.has(node.name) && scope.contains(node.name)) { // conflict with local variable writeIdentifier(code, node, parent, `_local_${node.name}`); @@ -189,7 +207,7 @@ async function importToGlobals({ast, code, getName, getDynamicWrapper}) { } } }); - + return isTouched; } diff --git a/test/test.js b/test/test.js index 4e0fbc5..ae891db 100644 --- a/test/test.js +++ b/test/test.js @@ -2,13 +2,13 @@ const assert = require("assert"); const rollup = require("rollup"); -const {withDir} = require("tempdir-yaml"); -const {default: endent} = require("endent"); +const { withDir } = require("tempdir-yaml"); +const { default: endent } = require("endent"); const commonjs = require("@rollup/plugin-commonjs"); const createPlugin = require(".."); -async function bundle(file, globals, {plugins = []} = {}, options = {}) { +async function bundle(file, globals, { plugins = [] } = {}, options = {}) { const warns = []; const bundle = await rollup.rollup({ input: [file], @@ -71,7 +71,7 @@ describe("main", () => { import bar from "foo"; console.log(bar); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), id => id.toUpperCase()); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), id => id.toUpperCase()); assert.equal(code.trim(), endent` console.log(FOO); `); @@ -84,7 +84,7 @@ describe("main", () => { import foo from "foo"; console.log(foo); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -99,7 +99,7 @@ describe("main", () => { import FOO from "foo"; console.log(FOO); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -114,7 +114,7 @@ describe("main", () => { import {bar} from "foo"; console.log(bar); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` console.log(FOO.bar); `); @@ -127,7 +127,7 @@ describe("main", () => { import {bar as baz} from "foo"; console.log(baz); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` console.log(FOO.bar); `); @@ -140,7 +140,7 @@ describe("main", () => { import foo from "foo"; console.log({foo}); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -161,7 +161,7 @@ describe("main", () => { console.log(foo); } `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` { console.log(FOO); @@ -181,7 +181,7 @@ describe("main", () => { const FOO = 123; console.log(foo, FOO); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -196,7 +196,7 @@ describe("main", () => { export const FOO = 123; console.log(foo, FOO); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -214,7 +214,7 @@ describe("main", () => { export {FOO}; console.log(foo, FOO); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -231,7 +231,7 @@ describe("main", () => { import bar from "bar"; console.log(foo, bar); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` import bar from 'bar'; @@ -246,7 +246,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` Promise.resolve(FOO) .then(console.log); @@ -260,7 +260,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: (name) => `Promise.all([${name}, BAR])`}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: (name) => `Promise.all([${name}, BAR])` }); assert.equal(code.trim(), endent` Promise.all([FOO, BAR]) .then(console.log); @@ -276,7 +276,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: () => false}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: () => false }); assert.equal(code.trim(), endent` console.log(FOO); import('foo') @@ -291,7 +291,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - await assert.rejects(bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: null}), { + await assert.rejects(bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: null }), { name: "TypeError", message: /Unexpected type/ }); @@ -304,7 +304,7 @@ describe("main", () => { export {foo as bar} from "foo"; export {mud} from "mud"; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO", mud: "MUD" }); @@ -323,7 +323,7 @@ describe("main", () => { export {foo as bar} from "foo"; export {foo as baz} from "foo"; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -341,7 +341,7 @@ describe("main", () => { export {default as baz} from "bak"; export {default as BOO} from "boo"; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { bak: "BAK", boo: "BOO", }); @@ -359,7 +359,7 @@ describe("main", () => { - entry.js: | export {} from "foo"; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), ""); }) ); @@ -370,7 +370,7 @@ describe("main", () => { - entry.js: | export {foo} from "bar"; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), "export { foo } from 'bar';"); }) ); @@ -382,7 +382,7 @@ describe("main", () => { export {foo}; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` const _global_FOO = FOO; @@ -399,7 +399,7 @@ describe("main", () => { export {foo}; export {foo as bar}; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` const _global_FOO = FOO; @@ -415,7 +415,7 @@ describe("main", () => { console.log(foo); export {foo}; `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` console.log(FOO); const _global_FOO = FOO; @@ -433,7 +433,7 @@ describe("main", () => { return _require_promise_; } `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {promise: "Promise"}); + const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { promise: "Promise" }); assert.equal(code.trim(), endent` function entry () { return Promise; @@ -451,7 +451,7 @@ describe("main", () => { console.log(foo); `, async resolve => { let entryCode = ""; - const {output: {"entry.js": {code}}} = await bundle( + const { output: { "entry.js": { code } } } = await bundle( resolve("entry.js"), { foo: "BAR" @@ -483,22 +483,24 @@ describe("main", () => { `); }) ); - it("transform commonjs", () => + + it("transform cjs with default exports", () => withDir(` - node_modules: - bar: - index.js: | - exports.default = "BAR"; + module.exports = "BAR"; - foo: - index.js: | const bar = require("bar"); console.log('foo'); - exports.default = () => console.log(bar); + module.exports = (val) => console.log(val || bar); - entry.js: | import log from "foo"; - log(); + import bar from "bar" + log(bar); `, async resolve => { - const {output: {"entry.js": {code}}} = await bundle( + const { output: { "entry.js": { code } } } = await bundle( resolve("entry.js"), { bar: "BAR" @@ -509,42 +511,155 @@ describe("main", () => { defaultIsModuleExports: true }), { - name: "test", - transform(code, id) { - // console.log('------------------------------') - // console.log(id) - // console.log('vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv') - - // // console.log(code) - // // if (id.includes('foo')) { - // // console.log(code) - // // } - // console.log('------------------------------') - }, - resolveId(importee , importer , options) { - // console.log('------------------------------') - // console.log(importee) - // console.log('vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv') - // console.log(importer , options); - - // console.log('------------------------------') - - - - if(importee === 'foo') { - return resolve('node_modules/foo/index.js') + name: "test", + resolveId(importee) { + if (["foo", "bar"].includes(importee)) { + return resolve(`node_modules/${importee}/index.js`) + } } - if(importee === 'bar') { - return resolve('node_modules/bar/index.js') + }] + }, + { + transformInCommonJs: true + } + ); + assert.equal(code.trim(), endent` + const bar = BAR; + console.log('foo'); + var foo = (val) => console.log(val || bar); + + foo(BAR); + `); + }) + ); + + it("transform cjs with 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`) + } } - } - }] + }] + }, + { + transformInCommonJs: true } ); assert.equal(code.trim(), endent` + var entry = {}; + + const { a } = BAR; + console.log(a); + + export { entry as default }; + `); + }) + ); + it("transform cjs 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`) + } + } + }] + }, + { + transformInCommonJs: true + } + ); + assert.equal(code.trim(), endent` + var foo = (val) => { + const { a } = BAR; + console.log(a); + }; + + foo(BAR.a); `); }) ); + it("untransform cjs require without transformInCommonJs", () => + 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.notEqual(code.trim(), endent` + var entry = {}; + + const { a } = BAR; + console.log(a); + + export { entry as default }; + `); + }) + ); }); From 7aad2a96692d04bfd2ba7401e21ca221e392e9a4 Mon Sep 17 00:00:00 2001 From: shunzi Date: Fri, 9 Aug 2024 00:39:49 +0800 Subject: [PATCH 3/6] feat: external global in cjs --- index.js | 37 +++++++------------------ lib/helpers.js | 28 ------------------- lib/import-to-globals.js | 36 ++++++------------------ test/test.js | 60 ++++++++-------------------------------- 4 files changed, 31 insertions(+), 130 deletions(-) delete mode 100644 lib/helpers.js diff --git a/index.js b/index.js index 24d5e82..01bf0bb 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,13 @@ const MagicString = require("magic-string"); const { createFilter } = require("@rollup/pluginutils"); const importToGlobals = require("./lib/import-to-globals"); -const { PROXY_SUFFIX, WRAPPED_SUFFIX, isVirtualModule, normaliseVirtualId } = require("./lib/helpers"); const defaultDynamicWrapper = id => `Promise.resolve(${id})`; -function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper, transformInCommonJs } = {}) { +function isVirtualModule(id) { + return id.startsWith("\0"); +} + +function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper } = {}) { if (!globals) { throw new TypeError("Missing mandatory option 'globals'"); } @@ -25,29 +28,10 @@ function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynam if (dynamicWrapperType !== "function") { throw new TypeError(`Unexpected type of 'dynamicWrapper', got '${dynamicWrapperType}'`); } - const resolvedGlobalIdMap = new Map() - function getNameInternal(id) { - const name = getName(id); - if (name) return name; - if (id.endsWith(PROXY_SUFFIX) || id.endsWith(WRAPPED_SUFFIX)) { - return resolvedGlobalIdMap.get(normaliseVirtualId(id)); - } - } - async function resolveId(importee, importer, options) { - if (!transformInCommonJs) return null - try { - const globalName = getName(importee) - if (globalName) { - const resolvedId = await this.resolve(importee, importer, options) - if (resolvedId.id && !isVirtualModule(resolvedId.id) && !resolvedId.external) { - resolvedGlobalIdMap.set(resolvedId.id, globalName) - } - } - } catch { - // ignore - } - return null - + 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 { @@ -55,7 +39,6 @@ function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynam options, transform }; - async function options(rawOptions) { const plugins = Array.isArray(rawOptions.plugins) ? [...rawOptions.plugins] @@ -86,7 +69,7 @@ function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynam const isTouched = await importToGlobals({ ast, code, - getName: getNameInternal, + getName, getDynamicWrapper: dynamicWrapper }); return isTouched ? { diff --git a/lib/helpers.js b/lib/helpers.js deleted file mode 100644 index ede6eba..0000000 --- a/lib/helpers.js +++ /dev/null @@ -1,28 +0,0 @@ -const PROXY_SUFFIX = '?commonjs-proxy'; -const WRAPPED_SUFFIX = '?commonjs-wrapped'; -const REQUIREFUNC_ID = "__require" - -const isWrappedId = (id, suffix) => id.endsWith(suffix); - -function isVirtualModule(id) { - return id.startsWith("\0"); -} - -function normaliseVirtualId(id) { - id = id.replace(/^\0/, '') - if (id.endsWith(PROXY_SUFFIX)) { - return id.slice(0, -PROXY_SUFFIX.length); - } - if (id.endsWith(WRAPPED_SUFFIX)) { - return id.slice(0, -WRAPPED_SUFFIX.length); - } -} - -module.exports = { - PROXY_SUFFIX, - WRAPPED_SUFFIX, - REQUIREFUNC_ID, - isWrappedId, - isVirtualModule, - normaliseVirtualId -} \ No newline at end of file diff --git a/lib/import-to-globals.js b/lib/import-to-globals.js index 4020db0..508dbdb 100644 --- a/lib/import-to-globals.js +++ b/lib/import-to-globals.js @@ -1,28 +1,21 @@ -const { attachScopes, makeLegalIdentifier } = require("@rollup/pluginutils"); -const { isWrappedId, WRAPPED_SUFFIX, REQUIREFUNC_ID } = require("./helpers") +const {attachScopes, makeLegalIdentifier} = require("@rollup/pluginutils"); let walk, isReference; async function prepare() { - [{ walk }, { default: isReference }] = await Promise.all([ + [{walk}, {default: isReference}] = await Promise.all([ import("estree-walker"), import("is-reference") ]); } -function analyzeImport(node, importBindings, requireFuncBindings, code, getName, globals) { - const id = node.source.value; - const name = id && getName(id); +function analyzeImport(node, importBindings, code, getName, globals) { + const name = node.source.value && getName(node.source.value); if (!name) { return false; } globals.add(name); - const isWrappedSuffix = isWrappedId(id, WRAPPED_SUFFIX) for (const spec of node.specifiers) { - if (isWrappedSuffix && spec.imported.name === REQUIREFUNC_ID) { - requireFuncBindings.set(spec.local.name, name); - continue; - } importBindings.set(spec.local.name, makeGlobalName( spec.imported ? spec.imported.name : "default", name @@ -73,17 +66,12 @@ function writeIdentifier(code, node, parent, name) { parent.local.isOverwritten = true; parent.exported.isOverwritten = true; } else { - code.overwrite(node.start, node.end, name, { contentOnly: true }); + code.overwrite(node.start, node.end, name, {contentOnly: true}); // FIXME: do we need this? node.isOverwritten = true; } } -function writeCallExpression(code, node, name) { - code.overwrite(node.start, node.end, name); - node.isOverwritten = true; -} - function analyzeExportNamed(node, code, getName, tempNames) { if (node.declaration || !node.source || !node.source.value) { return false; @@ -138,11 +126,10 @@ class ExportLeftHand { } } -async function importToGlobals({ ast, code, getName, getDynamicWrapper }) { +async function importToGlobals({ast, code, getName, getDynamicWrapper}) { await prepare(); let scope = attachScopes(ast, "scope"); const bindings = new Map; - const requireFuncBindings = new Map; const globals = new Set; let isTouched = false; const tempNames = new Set; @@ -150,12 +137,12 @@ async function importToGlobals({ ast, code, getName, getDynamicWrapper }) { for (const node of ast.body) { if (node.type === "ImportDeclaration") { - isTouched = analyzeImport(node, bindings, requireFuncBindings, code, getName, globals) || isTouched; + isTouched = analyzeImport(node, bindings, code, getName, globals) || isTouched; } else if (node.type === "ExportNamedDeclaration") { isTouched = analyzeExportNamed(node, code, getName, tempNames) || isTouched; } } - + let topStatement; walk(ast, { enter(node, parent) { @@ -177,11 +164,6 @@ async function importToGlobals({ ast, code, getName, getDynamicWrapper }) { } else { writeIdentifier(code, node, parent, bindings.get(node.name)); } - } else if (requireFuncBindings.has(node.name) && !scope.contains(node.name)) { - if (parent.type === "CallExpression" && parent.callee === node) { - writeCallExpression(code, parent, requireFuncBindings.get(node.name)); - this.skip(); - } } else if (globals.has(node.name) && scope.contains(node.name)) { // conflict with local variable writeIdentifier(code, node, parent, `_local_${node.name}`); @@ -207,7 +189,7 @@ async function importToGlobals({ ast, code, getName, getDynamicWrapper }) { } } }); - + return isTouched; } diff --git a/test/test.js b/test/test.js index ae891db..9d9d4da 100644 --- a/test/test.js +++ b/test/test.js @@ -484,7 +484,7 @@ describe("main", () => { }) ); - it("transform cjs with default exports", () => + it("require from default", () => withDir(` - node_modules: - bar: @@ -519,12 +519,11 @@ describe("main", () => { } }] }, - { - transformInCommonJs: true - } ); assert.equal(code.trim(), endent` - const bar = BAR; + const _global_BAR = BAR; + + const bar = _global_BAR; console.log('foo'); var foo = (val) => console.log(val || bar); @@ -533,7 +532,7 @@ describe("main", () => { }) ); - it("transform cjs with named exports", () => + it("require from named exports", () => withDir(` - bar.js: | module.a = "A"; @@ -567,7 +566,9 @@ describe("main", () => { assert.equal(code.trim(), endent` var entry = {}; - const { a } = BAR; + const _global_BAR = BAR; + + const { a } = _global_BAR; console.log(a); export { entry as default }; @@ -575,7 +576,7 @@ describe("main", () => { }) ); - it("transform cjs require in function", () => + it("require in function", () => withDir(` - bar.js: | module.a = "A"; @@ -614,8 +615,10 @@ describe("main", () => { } ); assert.equal(code.trim(), endent` + const _global_BAR = BAR; + var foo = (val) => { - const { a } = BAR; + const { a } = _global_BAR; console.log(a); }; @@ -623,43 +626,4 @@ describe("main", () => { `); }) ); - - it("untransform cjs require without transformInCommonJs", () => - 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.notEqual(code.trim(), endent` - var entry = {}; - - const { a } = BAR; - console.log(a); - - export { entry as default }; - `); - }) - ); }); From aa094f164d6c5b49fd328f65d95feb9f60128207 Mon Sep 17 00:00:00 2001 From: shunzi Date: Fri, 9 Aug 2024 00:50:54 +0800 Subject: [PATCH 4/6] fix: format --- index.js | 4 ++-- test/test.js | 62 ++++++++++++++++++++++++++-------------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 01bf0bb..99712b8 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const MagicString = require("magic-string"); -const { createFilter } = require("@rollup/pluginutils"); +const {createFilter} = require("@rollup/pluginutils"); const importToGlobals = require("./lib/import-to-globals"); const defaultDynamicWrapper = id => `Promise.resolve(${id})`; @@ -8,7 +8,7 @@ function isVirtualModule(id) { return id.startsWith("\0"); } -function createPlugin(globals, { include, exclude, dynamicWrapper = defaultDynamicWrapper } = {}) { +function createPlugin(globals, {include, exclude, dynamicWrapper = defaultDynamicWrapper} = {}) { if (!globals) { throw new TypeError("Missing mandatory option 'globals'"); } diff --git a/test/test.js b/test/test.js index 9d9d4da..ae147ec 100644 --- a/test/test.js +++ b/test/test.js @@ -2,13 +2,13 @@ const assert = require("assert"); const rollup = require("rollup"); -const { withDir } = require("tempdir-yaml"); -const { default: endent } = require("endent"); +const {withDir} = require("tempdir-yaml"); +const {default: endent} = require("endent"); const commonjs = require("@rollup/plugin-commonjs"); const createPlugin = require(".."); -async function bundle(file, globals, { plugins = [] } = {}, options = {}) { +async function bundle(file, globals, {plugins = []} = {}, options = {}) { const warns = []; const bundle = await rollup.rollup({ input: [file], @@ -71,7 +71,7 @@ describe("main", () => { import bar from "foo"; console.log(bar); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), id => id.toUpperCase()); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), id => id.toUpperCase()); assert.equal(code.trim(), endent` console.log(FOO); `); @@ -84,7 +84,7 @@ describe("main", () => { import foo from "foo"; console.log(foo); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -99,7 +99,7 @@ describe("main", () => { import FOO from "foo"; console.log(FOO); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -114,7 +114,7 @@ describe("main", () => { import {bar} from "foo"; console.log(bar); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` console.log(FOO.bar); `); @@ -127,7 +127,7 @@ describe("main", () => { import {bar as baz} from "foo"; console.log(baz); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` console.log(FOO.bar); `); @@ -140,7 +140,7 @@ describe("main", () => { import foo from "foo"; console.log({foo}); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -161,7 +161,7 @@ describe("main", () => { console.log(foo); } `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` { console.log(FOO); @@ -181,7 +181,7 @@ describe("main", () => { const FOO = 123; console.log(foo, FOO); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -196,7 +196,7 @@ describe("main", () => { export const FOO = 123; console.log(foo, FOO); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -214,7 +214,7 @@ describe("main", () => { export {FOO}; console.log(foo, FOO); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` const _local_FOO = 123; console.log(FOO, _local_FOO); @@ -231,7 +231,7 @@ describe("main", () => { import bar from "bar"; console.log(foo, bar); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` import bar from 'bar'; @@ -246,7 +246,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` Promise.resolve(FOO) .then(console.log); @@ -260,7 +260,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: (name) => `Promise.all([${name}, BAR])` }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: (name) => `Promise.all([${name}, BAR])`}); assert.equal(code.trim(), endent` Promise.all([FOO, BAR]) .then(console.log); @@ -276,7 +276,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: () => false }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: () => false}); assert.equal(code.trim(), endent` console.log(FOO); import('foo') @@ -291,7 +291,7 @@ describe("main", () => { import("foo") .then(console.log); `, async resolve => { - await assert.rejects(bundle(resolve("entry.js"), { foo: "FOO" }, void 0, { dynamicWrapper: null }), { + await assert.rejects(bundle(resolve("entry.js"), {foo: "FOO"}, void 0, {dynamicWrapper: null}), { name: "TypeError", message: /Unexpected type/ }); @@ -304,7 +304,7 @@ describe("main", () => { export {foo as bar} from "foo"; export {mud} from "mud"; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { foo: "FOO", mud: "MUD" }); @@ -323,7 +323,7 @@ describe("main", () => { export {foo as bar} from "foo"; export {foo as baz} from "foo"; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { foo: "FOO" }); assert.equal(code.trim(), endent` @@ -341,7 +341,7 @@ describe("main", () => { export {default as baz} from "bak"; export {default as BOO} from "boo"; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), { bak: "BAK", boo: "BOO", }); @@ -359,7 +359,7 @@ describe("main", () => { - entry.js: | export {} from "foo"; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), ""); }) ); @@ -370,7 +370,7 @@ describe("main", () => { - entry.js: | export {foo} from "bar"; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), "export { foo } from 'bar';"); }) ); @@ -382,7 +382,7 @@ describe("main", () => { export {foo}; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` const _global_FOO = FOO; @@ -399,7 +399,7 @@ describe("main", () => { export {foo}; export {foo as bar}; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` const _global_FOO = FOO; @@ -415,7 +415,7 @@ describe("main", () => { console.log(foo); export {foo}; `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { foo: "FOO" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {foo: "FOO"}); assert.equal(code.trim(), endent` console.log(FOO); const _global_FOO = FOO; @@ -433,7 +433,7 @@ describe("main", () => { return _require_promise_; } `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle(resolve("entry.js"), { promise: "Promise" }); + const {output: {"entry.js": {code}}} = await bundle(resolve("entry.js"), {promise: "Promise"}); assert.equal(code.trim(), endent` function entry () { return Promise; @@ -451,7 +451,7 @@ describe("main", () => { console.log(foo); `, async resolve => { let entryCode = ""; - const { output: { "entry.js": { code } } } = await bundle( + const {output: {"entry.js": {code}}} = await bundle( resolve("entry.js"), { foo: "BAR" @@ -500,7 +500,7 @@ describe("main", () => { import bar from "bar" log(bar); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle( + const {output: {"entry.js": {code}}} = await bundle( resolve("entry.js"), { bar: "BAR" @@ -540,7 +540,7 @@ describe("main", () => { const { a } = require("bar"); console.log(a); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle( + const {output: {"entry.js": {code}}} = await bundle( resolve("entry.js"), { bar: "BAR" @@ -591,7 +591,7 @@ describe("main", () => { import { a } from "bar"; log(a); `, async resolve => { - const { output: { "entry.js": { code } } } = await bundle( + const {output: {"entry.js": {code}}} = await bundle( resolve("entry.js"), { bar: "BAR" From 60573fe52323d586b3f665c7a59a22f9c83911e5 Mon Sep 17 00:00:00 2001 From: shunzi Date: Sat, 10 Aug 2024 23:20:27 +0800 Subject: [PATCH 5/6] fix: delete unused configuration --- package.json | 1 - test/test.js | 8 +------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/package.json b/package.json index d8ddb18..0ec219b 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "c8": "^10.1.2", "endent": "^2.1.0", "eslint": "^9.5.0", - "globals": "^15.8.0", "mocha": "^10.5.2", "rollup": "^4.18.0", "tempdir-yaml": "^0.3.0" diff --git a/test/test.js b/test/test.js index ae147ec..69a8372 100644 --- a/test/test.js +++ b/test/test.js @@ -518,7 +518,7 @@ describe("main", () => { } } }] - }, + } ); assert.equal(code.trim(), endent` const _global_BAR = BAR; @@ -558,9 +558,6 @@ describe("main", () => { } } }] - }, - { - transformInCommonJs: true } ); assert.equal(code.trim(), endent` @@ -609,9 +606,6 @@ describe("main", () => { } } }] - }, - { - transformInCommonJs: true } ); assert.equal(code.trim(), endent` From 5c6c41f1b54b6813d10f7b8125c6386970470fdb Mon Sep 17 00:00:00 2001 From: eight04 Date: Sun, 11 Aug 2024 21:28:02 +0800 Subject: [PATCH 6/6] Fix: update lock --- package-lock.json | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/package-lock.json b/package-lock.json index 28d2a71..1a76961 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "magic-string": "^0.30.10" }, "devDependencies": { + "@rollup/plugin-commonjs": "^26.0.1", "c8": "^10.1.2", "endent": "^2.1.0", "eslint": "^9.5.0", @@ -334,6 +335,96 @@ "node": ">=14" } }, + "node_modules/@rollup/plugin-commonjs": { + "version": "26.0.1", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-26.0.1.tgz", + "integrity": "sha512-UnsKoZK6/aGIH6AdkptXhNvhaqftcjq3zZdT+LY5Ftms6JR06nADcDsYp5hTU9E2lbJUEOhdlY5J4DNTneM+jQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@rollup/pluginutils": "^5.0.1", + "commondir": "^1.0.1", + "estree-walker": "^2.0.2", + "glob": "^10.4.1", + "is-reference": "1.2.1", + "magic-string": "^0.30.3" + }, + "engines": { + "node": ">=16.0.0 || 14 >= 14.17" + }, + "peerDependencies": { + "rollup": "^2.68.0||^3.0.0||^4.0.0" + }, + "peerDependenciesMeta": { + "rollup": { + "optional": true + } + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/brace-expansion": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz", + "integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==", + "dev": true, + "license": "MIT", + "dependencies": { + "balanced-match": "^1.0.0" + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/estree-walker": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-2.0.2.tgz", + "integrity": "sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==", + "dev": true, + "license": "MIT" + }, + "node_modules/@rollup/plugin-commonjs/node_modules/glob": { + "version": "10.4.5", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.4.5.tgz", + "integrity": "sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==", + "dev": true, + "license": "ISC", + "dependencies": { + "foreground-child": "^3.1.0", + "jackspeak": "^3.1.2", + "minimatch": "^9.0.4", + "minipass": "^7.1.2", + "package-json-from-dist": "^1.0.0", + "path-scurry": "^1.11.1" + }, + "bin": { + "glob": "dist/esm/bin.mjs" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/is-reference": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.2.1.tgz", + "integrity": "sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "*" + } + }, + "node_modules/@rollup/plugin-commonjs/node_modules/minimatch": { + "version": "9.0.5", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz", + "integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==", + "dev": true, + "license": "ISC", + "dependencies": { + "brace-expansion": "^2.0.1" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/@rollup/pluginutils": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-5.1.0.tgz", @@ -877,6 +968,13 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "node_modules/commondir": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", + "integrity": "sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==", + "dev": true, + "license": "MIT" + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",