diff --git a/codecov.yml b/codecov.yml index f1b170aee..c26c3bd80 100644 --- a/codecov.yml +++ b/codecov.yml @@ -5,6 +5,6 @@ coverage: round: down range: 70...100 status: - project: 'no' - patch: 'yes' -comment: 'off' + project: "no" + patch: "yes" +comment: "off" diff --git a/package.json b/package.json index c4c53c253..3b818222b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "ci:lint": "node scripts/run-changed.js ci:lint", "ci:test": "node scripts/run-changed.js ci:test", "lint": "pnpm run lint:docs && npm run lint:json && pnpm run lint:package && node scripts/run-changed.js lint", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint scripts util", "lint:json": "prettier --write tsconfig.*.json codecov.yml pnpm-workspace.yaml", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", diff --git a/packages/alias/package.json b/packages/alias/package.json index e3fdc0b52..2224eaafd 100755 --- a/packages/alias/package.json +++ b/packages/alias/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/auto-install/package.json b/packages/auto-install/package.json index 287004fb1..94c4c018d 100755 --- a/packages/auto-install/package.json +++ b/packages/auto-install/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", @@ -54,9 +54,7 @@ }, "types": "types/index.d.ts", "ava": { - "babel": { - "compileEnhancements": false - }, + "babel": true, "extensions": [ "ts" ], diff --git a/packages/auto-install/test/npm-bare.js b/packages/auto-install/test/npm-bare.js index 101ddbd62..593082e90 100644 --- a/packages/auto-install/test/npm-bare.js +++ b/packages/auto-install/test/npm-bare.js @@ -15,7 +15,7 @@ const input = join(cwd, '../input.js'); process.chdir(cwd); test('npm, bare', async (t) => { - t.timeout(30000); + t.timeout(50000); await rollup({ input, output: { diff --git a/packages/auto-install/test/npm.js b/packages/auto-install/test/npm.js index b1e00c838..4b102c326 100644 --- a/packages/auto-install/test/npm.js +++ b/packages/auto-install/test/npm.js @@ -17,7 +17,7 @@ const pkgFile = join(cwd, 'package.json'); process.chdir(cwd); test('invalid manager', (t) => { - t.timeout(30000); + t.timeout(50000); const error = t.throws( () => rollup({ @@ -36,7 +36,7 @@ test('invalid manager', (t) => { }); test('npm', async (t) => { - t.timeout(30000); + t.timeout(50000); await rollup({ input, output: { diff --git a/packages/auto-install/test/yarn-bare.js b/packages/auto-install/test/yarn-bare.js index cfa913c02..6b991594e 100644 --- a/packages/auto-install/test/yarn-bare.js +++ b/packages/auto-install/test/yarn-bare.js @@ -15,7 +15,7 @@ const input = join(cwd, '../input.js'); process.chdir(cwd); test('yarn, bare', async (t) => { - t.timeout(30000); + t.timeout(50000); await rollup({ input, output: { diff --git a/packages/auto-install/test/yarn.js b/packages/auto-install/test/yarn.js index cbef6e1c4..70f86e23a 100644 --- a/packages/auto-install/test/yarn.js +++ b/packages/auto-install/test/yarn.js @@ -15,7 +15,7 @@ const input = join(cwd, '../input.js'); process.chdir(cwd); test('yarn', async (t) => { - t.timeout(30000); + t.timeout(50000); await rollup({ input, output: { diff --git a/packages/babel/package.json b/packages/babel/package.json index 36af9524b..f6796b2e3 100644 --- a/packages/babel/package.json +++ b/packages/babel/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/beep/package.json b/packages/beep/package.json index a47c77006..4567045a7 100644 --- a/packages/beep/package.json +++ b/packages/beep/package.json @@ -20,7 +20,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "ava --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache lib test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "test": "ava" diff --git a/packages/buble/package.json b/packages/buble/package.json index bbdb4a5d9..ffc1b1a0b 100644 --- a/packages/buble/package.json +++ b/packages/buble/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/commonjs/README.md b/packages/commonjs/README.md index 5d67d347c..1dd6f0723 100644 --- a/packages/commonjs/README.md +++ b/packages/commonjs/README.md @@ -34,9 +34,9 @@ export default { input: 'src/index.js', output: { dir: 'output', - format: 'cjs' + format: 'cjs', }, - plugins: [commonjs()] + plugins: [commonjs()], }; ``` @@ -46,7 +46,7 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma ### `dynamicRequireTargets` -Type: `String|Array[String]`
+Type: `string | string[]`
Default: `[]` Some modules contain dynamic `require` calls, or require modules that contain circular dependencies, which are not handled well by static imports. @@ -66,60 +66,182 @@ commonjs({ '!node_modules/logform/index.js', '!node_modules/logform/format.js', '!node_modules/logform/levels.js', - '!node_modules/logform/browser.js' - ] + '!node_modules/logform/browser.js', + ], }); ``` ### `exclude` -Type: `String` | `Array[...String]`
+Type: `string | string[]`
Default: `null` A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default non-CommonJS modules are ignored. ### `include` -Type: `String` | `Array[...String]`
+Type: `string | string[]`
Default: `null` A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default CommonJS modules are targeted. ### `extensions` -Type: `Array[...String]`
+Type: `string[]`
Default: `['.js']` Search for extensions other than .js in the order specified. ### `ignoreGlobal` -Type: `Boolean`
+Type: `boolean`
Default: `false` If true, uses of `global` won't be dealt with by this plugin. ### `sourceMap` -Type: `Boolean`
+Type: `boolean`
Default: `true` If false, skips source map generation for CommonJS modules. ### `transformMixedEsModules` -Type: `Boolean`
+Type: `boolean`
Default: `false` Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to `true` if it's known that `require` calls should be transformed, or `false` if the code contains env detection and the `require` should survive a transformation. ### `ignore` -Type: `Array[...String | (String) => Boolean]`
+Type: `string[] | ((id: string) => boolean)`
Default: `[]` Sometimes you have to leave require statements unconverted. Pass an array containing the IDs or an `id => boolean` function. Only use this option if you know what you're doing! +### `esmExternals` + +Type: `boolean | string[] || ((id: string) => boolean)` +Default: `false` + +Controls how imports from external dependencies are rendered. By default, all external dependencies are assumed to be CommonJS. This means they are rendered as default imports to be compatible with e.g. NodeJS where ES modules can only import a default export from a CommonJS dependency: + +```js +// input +const foo = require('foo'); + +// output +import foo from 'foo'; +``` + +This is likely not desired for ES module dependencies: Here `require` should usually return the namespace to be compatible with how bundled modules are handled. + +If you set `esmExternals` to `true`, all external dependencies are assumed to be ES modules and will adhere to the `requireReturnsDefault` option. If that option is not set, they will be rendered as namespace imports. + +You can also supply an array of ids that are to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module. + +### `requireReturnsDefault` + +Type: `boolean | "auto" | "preferred" | ((id: string) => boolean | "auto" | "preferred")`
+Default: `false` + +Controls what is returned when requiring an ES module or external dependency from a CommonJS file. By default, this plugin will render it as a namespace import, i.e. + +```js +// input +const foo = require('foo'); + +// output +import * as foo from 'foo'; +``` + +This is in line with how other bundlers handle this situation and is also the most likely behaviour in case Node should ever support this. However there are some situations where this may not be desired: + +- There is code in an external dependency that cannot be changed where a `require` statement expects the default export to be returned from an ES module. +- If the imported module is in the same bundle, Rollup will generate a namespace object for the imported module which can increase bundle size unnecessarily: + + ```js + // input: main.js + const dep = require('./dep.js'); + console.log(dep.default); + + // input: dep.js + export default 'foo'; + + // output + var dep = 'foo'; + + var dep$1 = /*#__PURE__*/ Object.freeze({ + __proto__: null, + default: dep, + }); + + console.log(dep$1.default); + ``` + +For these situations, you can change Rollup's behaviour either globally or per module. To change it globally, set the `requireReturnsDefault` option to one of the following values: + +- `false`: This is the default, requiring an ES module returns its namespace. For external dependencies when using `esmExternals: true`, no additional interop code is generated: + + ```js + // input + const dep = require('dep'); + console.log(dep); + + // output + import * as dep from 'dep'; + + console.log(dep); + ``` + +- `"auto"`: This is complementary to how [`output.exports`](https://rollupjs.org/guide/en/#outputexports): `"auto"` works in Rollup: If a module has a default export and no named exports, requiring that module returns the default export. In all other cases, the namespace is returned. For external dependencies when using `esmExternals: true`, a corresponding interop helper is added: + + ```js + // output + import * as dep$1 from 'dep'; + + function getDefaultExportFromNamespaceIfNotNamed(n) { + return n && + Object.prototype.hasOwnProperty.call(n, 'default') && + Object.keys(n).length === 1 + ? n['default'] + : n; + } + + var dep = getDefaultExportFromNamespaceIfNotNamed(dep$1); + + console.log(dep); + ``` + +- `"preferred"`: If a module has a default export, requiring that module always returns the default export, no matter whether additional named exports exist. This is similar to how previous versions of this plugin worked. Again for external dependencies when using `esmExternals: true`, an interop helper is added: + + ```js + // output + import * as dep$1 from 'dep'; + + function getDefaultExportFromNamespaceIfPresent(n) { + return n && Object.prototype.hasOwnProperty.call(n, 'default') + ? n['default'] + : n; + } + + var dep = getDefaultExportFromNamespaceIfPresent(dep$1); + + console.log(dep); + ``` + +- `true`: This will always try to return the default export on require without checking if it actually exists. This can throw at build time if there is no default export. This is how external dependencies are handled when `esmExternals` is not used. The advantage over the other options is that, like `false`, this does not add an interop helper for external dependencies, keeping the code lean: + + ```js + // output + import dep from 'dep'; + + console.log(dep); + ``` + +To change this for individual modules, you can supply a function for `requireReturnsDefault` instead. This function will then be called once for each required ES module or external dependency with the corresponding id and allows you to return different values for different modules. + ## Using with @rollup/plugin-node-resolve Since most CommonJS packages you are importing are probably dependencies in `node_modules`, you may need to use [@rollup/plugin-node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve): @@ -134,9 +256,9 @@ export default { output: { file: 'bundle.js', format: 'iife', - name: 'MyModule' + name: 'MyModule', }, - plugins: [resolve(), commonjs()] + plugins: [resolve(), commonjs()], }; ``` @@ -146,7 +268,7 @@ Symlinks are common in monorepos and are also created by the `npm link` command. ```js commonjs({ - include: /node_modules/ + include: /node_modules/, }); ``` diff --git a/packages/commonjs/package.json b/packages/commonjs/package.json index 50184d483..732e59496 100644 --- a/packages/commonjs/package.json +++ b/packages/commonjs/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose && pnpm run test:ts", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", @@ -61,7 +61,6 @@ "devDependencies": { "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^8.4.0", - "acorn": "^7.3.1", "locate-character": "^2.0.5", "require-relative": "^0.8.7", "rollup": "^2.23.0", diff --git a/packages/commonjs/src/dynamic-packages-manager.js b/packages/commonjs/src/dynamic-packages-manager.js new file mode 100644 index 000000000..220e41f8e --- /dev/null +++ b/packages/commonjs/src/dynamic-packages-manager.js @@ -0,0 +1,59 @@ +import { existsSync, readFileSync } from 'fs'; +import { join } from 'path'; + +import { + DYNAMIC_PACKAGES_ID, + DYNAMIC_REGISTER_PREFIX, + getVirtualPathForDynamicRequirePath, + HELPERS_ID +} from './helpers'; +import { normalizePathSlashes } from './transform'; + +export function getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir) { + let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`; + for (const dir of dynamicRequireModuleDirPaths) { + let entryPoint = 'index.js'; + + try { + if (existsSync(join(dir, 'package.json'))) { + entryPoint = + JSON.parse(readFileSync(join(dir, 'package.json'), { encoding: 'utf8' })).main || + entryPoint; + } + } catch (ignored) { + // ignored + } + + code += `\ncommonjsRegister(${JSON.stringify( + getVirtualPathForDynamicRequirePath(dir, commonDir) + )}, function (module, exports) { + module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))}); +});`; + } + return code; +} + +export function getDynamicPackagesEntryIntro( + id, + dynamicRequireModuleDirPaths, + dynamicRequireModuleSet +) { + try { + const code = readFileSync(id, { encoding: 'utf8' }); + let dynamicImports = Array.from( + dynamicRequireModuleSet, + (dynamicId) => `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + dynamicId)});` + ).join('\n'); + + if (dynamicRequireModuleDirPaths.length) { + dynamicImports += `require(${JSON.stringify( + DYNAMIC_REGISTER_PREFIX + DYNAMIC_PACKAGES_ID + )});`; + } + + return `${dynamicImports}\n${code}`; + } catch (ex) { + this.warn(`Failed to read file ${id}, dynamic modules might not work correctly`); + return null; + } +} diff --git a/packages/commonjs/src/helpers.js b/packages/commonjs/src/helpers.js index 1341d4fb0..b2f50fe2d 100644 --- a/packages/commonjs/src/helpers.js +++ b/packages/commonjs/src/helpers.js @@ -28,7 +28,7 @@ export const HELPERS_ID = '\0commonjsHelpers.js'; // rollup/rollup-plugin-commonjs#224 // We should remove it once Rollup core and this plugin are updated to not use // this pattern any more -export const HELPERS = ` +const HELPERS = ` export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; export function getDefaultExportFromCjs (x) { @@ -45,18 +45,22 @@ export function createCommonjsModule(fn, basedir, module) { }, fn(module, module.exports), module.exports; } -export function getCjsExportFromNamespace (n) { - return n && n['default'] || n; +export function getDefaultExportFromNamespaceIfPresent (n) { + return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n; +} + +export function getDefaultExportFromNamespaceIfNotNamed (n) { + return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n; } `; -export const HELPER_NON_DYNAMIC = ` +const HELPER_NON_DYNAMIC = ` export function commonjsRequire () { throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs'); } `; -export const HELPERS_DYNAMIC = ` +const HELPERS_DYNAMIC = ` export function commonjsRegister (path, loader) { DYNAMIC_REQUIRE_LOADERS[path] = loader; } @@ -194,3 +198,7 @@ export function commonjsRequire (path, originalModuleDir) { commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE; `; + +export function getHelpersModule(isDynamicRequireModulesEnabled) { + return `${HELPERS}${isDynamicRequireModulesEnabled ? HELPERS_DYNAMIC : HELPER_NON_DYNAMIC}`; +} diff --git a/packages/commonjs/src/index.js b/packages/commonjs/src/index.js index 958c079b3..2488be177 100644 --- a/packages/commonjs/src/index.js +++ b/packages/commonjs/src/index.js @@ -1,43 +1,57 @@ -import { existsSync, readFileSync } from 'fs'; -import { extname, join } from 'path'; +import { extname } from 'path'; import { createFilter } from '@rollup/pluginutils'; - import getCommonDir from 'commondir'; import { peerDependencies } from '../package.json'; +import { getDynamicPackagesEntryIntro, getDynamicPackagesModule } from './dynamic-packages-manager'; import getDynamicRequirePaths from './dynamic-require-paths'; - import { DYNAMIC_JSON_PREFIX, DYNAMIC_PACKAGES_ID, - DYNAMIC_REGISTER_PREFIX, - getVirtualPathForDynamicRequirePath, EXTERNAL_SUFFIX, + getHelpersModule, getIdFromExternalProxyId, getIdFromProxyId, - HELPERS, HELPERS_ID, - HELPER_NON_DYNAMIC, - HELPERS_DYNAMIC, PROXY_SUFFIX } from './helpers'; - -import { getIsCjsPromise, setIsCjsPromise } from './is-cjs'; +import { setIsCjsPromise } from './is-cjs'; +import { + getDynamicJsonProxy, + getDynamicRequireProxy, + getSpecificHelperProxy, + getStaticRequireProxy, + getUnknownRequireProxy +} from './proxies'; import getResolveId from './resolve-id'; import { checkEsModule, - normalizePathSlashes, hasCjsKeywords, + normalizePathSlashes, transformCommonjs } from './transform'; -import { getName } from './utils'; export default function commonjs(options = {}) { const extensions = options.extensions || ['.js']; const filter = createFilter(options.include, options.exclude); - const { ignoreGlobal } = options; + const { + ignoreGlobal, + requireReturnsDefault: requireReturnsDefaultOption, + esmExternals + } = options; + const getRequireReturnsDefault = + typeof requireReturnsDefaultOption === 'function' + ? requireReturnsDefaultOption + : () => requireReturnsDefaultOption; + let esmExternalIds; + const isEsmExternal = + typeof esmExternals === 'function' + ? esmExternals + : Array.isArray(esmExternals) + ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id)) + : () => esmExternals; const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths( options.dynamicRequireTargets @@ -47,8 +61,8 @@ export default function commonjs(options = {}) { ? getCommonDir(null, Array.from(dynamicRequireModuleSet).concat(process.cwd())) : null; - const esModulesWithoutDefaultExport = new Set(); const esModulesWithDefaultExport = new Set(); + const esModulesWithNamedExports = new Set(); const ignoreRequire = typeof options.ignore === 'function' @@ -62,20 +76,22 @@ export default function commonjs(options = {}) { const sourceMap = options.sourceMap !== false; function transformAndCheckExports(code, id) { - const { isEsModule, hasDefaultExport, ast } = checkEsModule(this.parse, code, id); - - const isDynamicRequireModule = dynamicRequireModuleSet.has(normalizePathSlashes(id)); - - if (isEsModule && (!isDynamicRequireModule || !options.transformMixedEsModules)) { - (hasDefaultExport ? esModulesWithDefaultExport : esModulesWithoutDefaultExport).add(id); - if (!options.transformMixedEsModules) { - setIsCjsPromise(id, false); - return null; - } + const { isEsModule, hasDefaultExport, hasNamedExports, ast } = checkEsModule( + this.parse, + code, + id + ); + if (hasDefaultExport) { + esModulesWithDefaultExport.add(id); + } + if (hasNamedExports) { + esModulesWithNamedExports.add(id); } - // it is not an ES module AND it does not have CJS-specific elements. - else if (!hasCjsKeywords(code, ignoreGlobal)) { - esModulesWithoutDefaultExport.add(id); + + if ( + !dynamicRequireModuleSet.has(normalizePathSlashes(id)) && + (!hasCjsKeywords(code, ignoreGlobal) || (isEsModule && !options.transformMixedEsModules)) + ) { setIsCjsPromise(id, false); return null; } @@ -95,12 +111,6 @@ export default function commonjs(options = {}) { ); setIsCjsPromise(id, isEsModule ? false : Boolean(transformed)); - - if (!transformed) { - if (!isEsModule || isDynamicRequireModule) esModulesWithoutDefaultExport.add(id); - return null; - } - return transformed; } @@ -128,134 +138,50 @@ export default function commonjs(options = {}) { load(id) { if (id === HELPERS_ID) { - let code = HELPERS; - - // Do not bloat everyone's code with the module manager code - if (isDynamicRequireModulesEnabled) code += HELPERS_DYNAMIC; - else code += HELPER_NON_DYNAMIC; - - return code; + return getHelpersModule(isDynamicRequireModulesEnabled); } - // commonjsHelpers?commonjsRegister if (id.startsWith(HELPERS_ID)) { - return `export {${id.split('?')[1]} as default} from '${HELPERS_ID}';`; + return getSpecificHelperProxy(id); } - // generate proxy modules if (id.endsWith(EXTERNAL_SUFFIX)) { const actualId = getIdFromExternalProxyId(id); - const name = getName(actualId); - - if (actualId === HELPERS_ID || actualId === DYNAMIC_PACKAGES_ID) - // These do not export default - return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`; - - return `import ${name} from ${JSON.stringify(actualId)}; export default ${name};`; + return getUnknownRequireProxy( + actualId, + isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true + ); } if (id === DYNAMIC_PACKAGES_ID) { - let code = `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');`; - for (const dir of dynamicRequireModuleDirPaths) { - let entryPoint = 'index.js'; - - try { - if (existsSync(join(dir, 'package.json'))) { - entryPoint = - JSON.parse(readFileSync(join(dir, 'package.json'), { encoding: 'utf8' })).main || - entryPoint; - } - } catch (ignored) { - // ignored - } - - code += `\ncommonjsRegister(${JSON.stringify( - getVirtualPathForDynamicRequirePath(dir, commonDir) - )}, function (module, exports) { - module.exports = require(${JSON.stringify(normalizePathSlashes(join(dir, entryPoint)))}); -});`; - } - return code; + return getDynamicPackagesModule(dynamicRequireModuleDirPaths, commonDir); } - let actualId = id; - - const isDynamicJson = actualId.startsWith(DYNAMIC_JSON_PREFIX); - if (isDynamicJson) { - actualId = actualId.slice(DYNAMIC_JSON_PREFIX.length); - } - - const normalizedPath = normalizePathSlashes(actualId); - - if (isDynamicJson) { - return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify( - getVirtualPathForDynamicRequirePath(normalizedPath, commonDir) - )}, function (module, exports) { - module.exports = require(${JSON.stringify(normalizedPath)}); -});`; + if (id.startsWith(DYNAMIC_JSON_PREFIX)) { + return getDynamicJsonProxy(id, commonDir); } + const normalizedPath = normalizePathSlashes(id); if (dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json')) { - // Try our best to still export the module fully. - // The commonjs polyfill should take care of circular references. - - return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify( - getVirtualPathForDynamicRequirePath(normalizedPath, commonDir) - )}, function (module, exports) { - ${readFileSync(normalizedPath, { encoding: 'utf8' })} -});`; + return getDynamicRequireProxy(normalizedPath, commonDir); } - if (actualId.endsWith(PROXY_SUFFIX)) { - actualId = getIdFromProxyId(actualId); - const name = getName(actualId); - - return getIsCjsPromise(actualId).then((isCjs) => { - if ( - dynamicRequireModuleSet.has(normalizePathSlashes(actualId)) && - !actualId.endsWith('.json') - ) - return `import {commonjsRequire} from '${HELPERS_ID}'; const ${name} = commonjsRequire(${JSON.stringify( - getVirtualPathForDynamicRequirePath(normalizePathSlashes(actualId), commonDir) - )}); export default (${name} && ${name}['default']) || ${name}`; - else if (isCjs) - return `import { __moduleExports } from ${JSON.stringify( - actualId - )}; export default __moduleExports;`; - else if (esModulesWithoutDefaultExport.has(actualId)) - return `import * as ${name} from ${JSON.stringify(actualId)}; export default ${name};`; - else if (esModulesWithDefaultExport.has(actualId)) { - return `export {default} from ${JSON.stringify(actualId)};`; - } - return `import * as ${name} from ${JSON.stringify( - actualId - )}; import {getCjsExportFromNamespace} from "${HELPERS_ID}"; export default getCjsExportFromNamespace(${name})`; - }); + if (id.endsWith(PROXY_SUFFIX)) { + const actualId = getIdFromProxyId(id); + return getStaticRequireProxy( + actualId, + getRequireReturnsDefault(actualId), + esModulesWithDefaultExport, + esModulesWithNamedExports + ); } if (isDynamicRequireModulesEnabled && this.getModuleInfo(id).isEntry) { - let code; - - try { - code = readFileSync(actualId, { encoding: 'utf8' }); - } catch (ex) { - this.warn(`Failed to read file ${actualId}, dynamic modules might not work correctly`); - return null; - } - - let dynamicImports = Array.from(dynamicRequireModuleSet) - .map((dynamicId) => `require(${JSON.stringify(DYNAMIC_REGISTER_PREFIX + dynamicId)});`) - .join('\n'); - - if (dynamicRequireModuleDirPaths.length) { - dynamicImports += `require(${JSON.stringify( - DYNAMIC_REGISTER_PREFIX + DYNAMIC_PACKAGES_ID - )});`; - } - - code = `${dynamicImports}\n${code}`; - - return code; + return getDynamicPackagesEntryIntro( + id, + dynamicRequireModuleDirPaths, + dynamicRequireModuleSet + ); } return null; diff --git a/packages/commonjs/src/proxies.js b/packages/commonjs/src/proxies.js new file mode 100644 index 000000000..27e2e2922 --- /dev/null +++ b/packages/commonjs/src/proxies.js @@ -0,0 +1,65 @@ +import { readFileSync } from 'fs'; + +import { DYNAMIC_JSON_PREFIX, getVirtualPathForDynamicRequirePath, HELPERS_ID } from './helpers'; +import { getIsCjsPromise } from './is-cjs'; +import { normalizePathSlashes } from './transform'; +import { getName } from './utils'; + +// e.g. id === "commonjsHelpers?commonjsRegister" +export function getSpecificHelperProxy(id) { + return `export {${id.split('?')[1]} as default} from '${HELPERS_ID}';`; +} + +export function getUnknownRequireProxy(id, requireReturnsDefault) { + if (requireReturnsDefault === true || id.endsWith('.json')) { + return `export {default} from ${JSON.stringify(id)};`; + } + const name = getName(id); + const exported = + requireReturnsDefault === 'auto' + ? `import {getDefaultExportFromNamespaceIfNotNamed} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name})` + : requireReturnsDefault === 'preferred' + ? `import {getDefaultExportFromNamespaceIfPresent} from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name})` + : `export default ${name}`; + return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`; +} + +export function getDynamicJsonProxy(id, commonDir) { + const normalizedPath = normalizePathSlashes(id.slice(DYNAMIC_JSON_PREFIX.length)); + return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify( + getVirtualPathForDynamicRequirePath(normalizedPath, commonDir) + )}, function (module, exports) { + module.exports = require(${JSON.stringify(normalizedPath)}); +});`; +} + +export function getDynamicRequireProxy(normalizedPath, commonDir) { + return `const commonjsRegister = require('${HELPERS_ID}?commonjsRegister');\ncommonjsRegister(${JSON.stringify( + getVirtualPathForDynamicRequirePath(normalizedPath, commonDir) + )}, function (module, exports) { + ${readFileSync(normalizedPath, { encoding: 'utf8' })} +});`; +} + +export async function getStaticRequireProxy( + id, + requireReturnsDefault, + esModulesWithDefaultExport, + esModulesWithNamedExports +) { + const name = getName(id); + const isCjs = await getIsCjsPromise(id); + if (isCjs) { + return `import { __moduleExports } from ${JSON.stringify(id)}; export default __moduleExports;`; + } else if (isCjs === null) { + return getUnknownRequireProxy(id, requireReturnsDefault); + } else if ( + requireReturnsDefault !== true && + (!requireReturnsDefault || + !esModulesWithDefaultExport.has(id) || + (esModulesWithNamedExports.has(id) && requireReturnsDefault === 'auto')) + ) { + return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`; + } + return `export {default} from ${JSON.stringify(id)};`; +} diff --git a/packages/commonjs/src/resolve-id.js b/packages/commonjs/src/resolve-id.js index 1ece316b5..02d61e149 100644 --- a/packages/commonjs/src/resolve-id.js +++ b/packages/commonjs/src/resolve-id.js @@ -1,4 +1,5 @@ /* eslint-disable no-param-reassign, no-undefined */ + import { statSync } from 'fs'; import { dirname, resolve, sep } from 'path'; @@ -47,7 +48,8 @@ export default function getResolveId(extensions) { const isProxyModule = importee.endsWith(PROXY_SUFFIX); if (isProxyModule) { importee = getIdFromProxyId(importee); - } else if (importee.startsWith('\0')) { + } + if (importee.startsWith('\0')) { if ( importee.startsWith(HELPERS_ID) || importee === DYNAMIC_PACKAGES_ID || @@ -55,11 +57,9 @@ export default function getResolveId(extensions) { ) { return importee; } - return null; - } - - if (importee.startsWith(DYNAMIC_JSON_PREFIX)) { - return importee; + if (!isProxyModule) { + return null; + } } if (importer && importer.endsWith(PROXY_SUFFIX)) { diff --git a/packages/commonjs/src/transform.js b/packages/commonjs/src/transform.js index 6f823dfee..93e727481 100644 --- a/packages/commonjs/src/transform.js +++ b/packages/commonjs/src/transform.js @@ -1,22 +1,21 @@ /* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */ -import { resolve, dirname } from 'path'; +import { dirname, resolve } from 'path'; + +import { attachScopes, extractAssignedNames, makeLegalIdentifier } from '@rollup/pluginutils'; import { walk } from 'estree-walker'; import MagicString from 'magic-string'; -import { attachScopes, extractAssignedNames, makeLegalIdentifier } from '@rollup/pluginutils'; - import { sync as nodeResolveSync } from 'resolve'; import { flatten, isFalsy, isReference, isTruthy } from './ast-utils'; import { + DYNAMIC_JSON_PREFIX, + DYNAMIC_REGISTER_PREFIX, getProxyId, getVirtualPathForDynamicRequirePath, - HELPERS_ID, - DYNAMIC_REGISTER_PREFIX, - DYNAMIC_JSON_PREFIX + HELPERS_ID } from './helpers'; import { getName } from './utils'; -// TODO can this be async? const reserved = 'process location abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for from function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield'.split( ' ' @@ -28,7 +27,6 @@ const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/ const firstpassGlobal = /\b(?:require|module|exports|global)\b/; const firstpassNoGlobal = /\b(?:require|module|exports)\b/; -const importExportDeclaration = /^(?:Import|Export(?:Named|Default))Declaration/; const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/; function deconflict(scope, globals, identifier) { @@ -66,20 +64,38 @@ export function checkEsModule(parse, code, id) { const ast = tryParse(parse, code, id); let isEsModule = false; + let hasDefaultExport = false; + let hasNamedExports = false; for (const node of ast.body) { - if (node.type === 'ExportDefaultDeclaration') - return { isEsModule: true, hasDefaultExport: true, ast }; - if (node.type === 'ExportNamedDeclaration') { + if (node.type === 'ExportDefaultDeclaration') { + isEsModule = true; + hasDefaultExport = true; + } else if (node.type === 'ExportNamedDeclaration') { isEsModule = true; - for (const specifier of node.specifiers) { - if (specifier.exported.name === 'default') { - return { isEsModule: true, hasDefaultExport: true, ast }; + if (node.declaration) { + hasNamedExports = true; + } else { + for (const specifier of node.specifiers) { + if (specifier.exported.name === 'default') { + hasDefaultExport = true; + } else { + hasNamedExports = true; + } } } - } else if (importExportDeclaration.test(node.type)) isEsModule = true; + } else if (node.type === 'ExportAllDeclaration') { + isEsModule = true; + if (node.exported && node.exported.name === 'default') { + hasDefaultExport = true; + } else { + hasNamedExports = true; + } + } else if (node.type === 'ImportDeclaration') { + isEsModule = true; + } } - return { isEsModule, hasDefaultExport: false, ast }; + return { isEsModule, hasDefaultExport, hasNamedExports, ast }; } function getDefinePropertyCallName(node, targetName) { @@ -186,16 +202,12 @@ export function transformCommonjs( function isStaticRequireStatement(node) { if (!isRequireStatement(node)) return false; - if (hasDynamicArguments(node)) return false; - return true; + return !hasDynamicArguments(node); } function isNodeRequireStatement(parent) { const reservedMethod = ['resolve', 'cache', 'main']; - if (parent && parent.property && reservedMethod.indexOf(parent.property.name) > -1) { - return true; - } - return false; + return !!(parent && parent.property && reservedMethod.indexOf(parent.property.name) > -1); } function isIgnoredRequireStatement(requiredNode) { @@ -565,7 +577,9 @@ export function transformCommonjs( .filter(([, importProxy]) => importProxy) .map(([source]) => { const { name, importsDefault } = required[source]; - return `import ${importsDefault ? `${name} from ` : ``}'${getProxyId(source)}';`; + return `import ${importsDefault ? `${name} from ` : ``}'${ + source.startsWith('\0') ? source : getProxyId(source) + }';`; }) ) .join('\n')}\n\n`; @@ -677,5 +691,5 @@ export function transformCommonjs( code = magicString.toString(); const map = sourceMap ? magicString.generateMap() : null; - return { code, map, syntheticNamedExports: '__moduleExports' }; + return { code, map, syntheticNamedExports: isEsModule ? false : '__moduleExports' }; } diff --git a/packages/commonjs/test/fixtures/function/esm-externals-false/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-false/_config.js new file mode 100644 index 000000000..fece1fe53 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-false/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'always uses the default export when esmExternals is not used', + options: { + external: [ + 'external-cjs-exports', + 'external-cjs-module-exports', + 'external-esm-named', + 'external-esm-mixed', + 'external-esm-default' + ] + }, + pluginOptions: { + esmExternals: false + } +}; diff --git a/packages/commonjs/test/fixtures/function/esm-externals-false/main.js b/packages/commonjs/test/fixtures/function/esm-externals-false/main.js new file mode 100644 index 000000000..d1a5156cf --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-false/main.js @@ -0,0 +1,11 @@ +const externalExports = require('external-cjs-exports'); +const externalModuleExports = require('external-cjs-module-exports'); +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); +t.deepEqual(externalModuleExports, 'bar', 'external module exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, 'bar', 'external default'); diff --git a/packages/commonjs/test/fixtures/function/esm-externals-function/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-function/_config.js new file mode 100644 index 000000000..990ca06e7 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-function/_config.js @@ -0,0 +1,36 @@ +const assert = require('assert'); + +const called = {}; + +module.exports = { + description: 'always uses the default export when esmExternals is not used', + options: { + external: [ + 'external-cjs-exports', + 'external-cjs-module-exports', + 'external-esm-named', + 'external-esm-mixed', + 'external-esm-default' + ], + plugins: [ + { + name: 'test-plugin', + buildEnd() { + assert.deepStrictEqual(called, { + 'external-cjs-exports': 1, + 'external-cjs-module-exports': 1, + 'external-esm-named': 1, + 'external-esm-mixed': 1, + 'external-esm-default': 1 + }); + } + } + ] + }, + pluginOptions: { + esmExternals: (id) => { + called[id] = (called[id] || 0) + 1; + return id === 'external-esm-default'; + } + } +}; diff --git a/packages/commonjs/test/fixtures/function/esm-externals-function/main.js b/packages/commonjs/test/fixtures/function/esm-externals-function/main.js new file mode 100644 index 000000000..88aa332ec --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-function/main.js @@ -0,0 +1,11 @@ +const externalExports = require('external-cjs-exports'); +const externalModuleExports = require('external-cjs-module-exports'); +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); +t.deepEqual(externalModuleExports, 'bar', 'external module exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/esm-externals-list/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-list/_config.js new file mode 100644 index 000000000..dac64cda6 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-list/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'always uses the default export when esmExternals is not used', + options: { + external: [ + 'external-cjs-exports', + 'external-cjs-module-exports', + 'external-esm-named', + 'external-esm-mixed', + 'external-esm-default' + ] + }, + pluginOptions: { + esmExternals: ['external-esm-default'] + } +}; diff --git a/packages/commonjs/test/fixtures/function/esm-externals-list/main.js b/packages/commonjs/test/fixtures/function/esm-externals-list/main.js new file mode 100644 index 000000000..88aa332ec --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-list/main.js @@ -0,0 +1,11 @@ +const externalExports = require('external-cjs-exports'); +const externalModuleExports = require('external-cjs-module-exports'); +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); +t.deepEqual(externalModuleExports, 'bar', 'external module exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js new file mode 100644 index 000000000..608ac5468 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-true/_config.js @@ -0,0 +1,15 @@ +module.exports = { + description: 'always uses the default export when esmExternals is not used', + options: { + external: [ + 'external-cjs-exports', + 'external-cjs-module-exports', + 'external-esm-named', + 'external-esm-mixed', + 'external-esm-default' + ] + }, + pluginOptions: { + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/esm-externals-true/main.js b/packages/commonjs/test/fixtures/function/esm-externals-true/main.js new file mode 100644 index 000000000..ca7736577 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-true/main.js @@ -0,0 +1,11 @@ +const externalExports = require('external-cjs-exports'); +const externalModuleExports = require('external-cjs-module-exports'); +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); +t.deepEqual(externalModuleExports, 'bar', 'external module exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, { default: 'bar', foo: 'foo' }, 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/esm-externals-undefined/_config.js b/packages/commonjs/test/fixtures/function/esm-externals-undefined/_config.js new file mode 100644 index 000000000..8415861e7 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-undefined/_config.js @@ -0,0 +1,12 @@ +module.exports = { + description: 'always uses the default export when esmExternals is not used', + options: { + external: [ + 'external-cjs-exports', + 'external-cjs-module-exports', + 'external-esm-named', + 'external-esm-mixed', + 'external-esm-default' + ] + } +}; diff --git a/packages/commonjs/test/fixtures/function/esm-externals-undefined/main.js b/packages/commonjs/test/fixtures/function/esm-externals-undefined/main.js new file mode 100644 index 000000000..d1a5156cf --- /dev/null +++ b/packages/commonjs/test/fixtures/function/esm-externals-undefined/main.js @@ -0,0 +1,11 @@ +const externalExports = require('external-cjs-exports'); +const externalModuleExports = require('external-cjs-module-exports'); +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalExports, { foo: 'foo' }, 'external exports'); +t.deepEqual(externalModuleExports, 'bar', 'external module exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, 'bar', 'external default'); diff --git a/packages/commonjs/test/fixtures/function/export-default-from/_config.js b/packages/commonjs/test/fixtures/function/export-default-from/_config.js new file mode 100644 index 000000000..5ea8afe99 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/export-default-from/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'handles reexported default exports', + pluginOptions: { + requireReturnsDefault: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/_config.js new file mode 100644 index 000000000..49dbcbf2c --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: + 'returns the default when requiring an ES module and requireReturnsDefault is "auto" if there are no named exports', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: 'auto', + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/main.js new file mode 100644 index 000000000..d778ed674 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/main.js @@ -0,0 +1,16 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const namedExports = require('./named.js'); +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); +const noExports = require('./none.js'); + +t.deepEqual(namedExports, { foo: 'foo' }, 'named exports'); +t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports'); +t.deepEqual(defaultExport, 'bar', 'default export'); +t.deepEqual(noExports, {}, 'no exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed'); +t.deepEqual(externalDefault, 'bar', 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/none.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-auto/none.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/_config.js new file mode 100644 index 000000000..ee92ea3f7 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: + 'returns the namespace when requiring an ES module and requireReturnsDefault is false', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: false, + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/main.js new file mode 100644 index 000000000..658db2cdd --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/main.js @@ -0,0 +1,16 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const namedExports = require('./named.js'); +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); +const noExports = require('./none.js'); + +t.deepEqual(namedExports, { foo: 'foo' }, 'named exports'); +t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports'); +t.deepEqual(defaultExport, { default: 'bar' }, 'default export'); +t.deepEqual(noExports, {}, 'no exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/none.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-false/none.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/_config.js new file mode 100644 index 000000000..69cd937f2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/_config.js @@ -0,0 +1,42 @@ +const assert = require('assert'); + +const called = {}; + +module.exports = { + description: 'only calls a requireReturnsDefault function once per id', + options: { + plugins: [ + { + name: 'test-plugin', + resolveId(id) { + if (id.startsWith('dep')) return id; + return null; + }, + load(id) { + const [prefix, name] = id.split('_'); + if (prefix === 'dep') { + return `export default '${name}'; export const named = 'unused';`; + } + return null; + }, + buildEnd() { + assert.deepStrictEqual(called, { + 'main.js': 1, + 'both.js': 1, + 'other.js': 1 + }); + } + } + ] + }, + pluginOptions: { + requireReturnsDefault: (id) => { + const [prefix, name] = id.split('_'); + if (prefix === 'dep') { + called[name] = (called[name] || 0) + 1; + return 'preferred'; + } + return false; + } + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/main.js new file mode 100644 index 000000000..a146a1e08 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/main.js @@ -0,0 +1,7 @@ +require('./other.js'); + +const main = require('dep_main.js'); +const both = require('dep_both.js'); + +t.deepEqual(main, 'main.js', 'main main'); +t.deepEqual(both, 'both.js', 'main both'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/other.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/other.js new file mode 100644 index 000000000..a8b500f7e --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-called-once/other.js @@ -0,0 +1,5 @@ +const other = require('dep_other.js'); +const both = require('dep_both.js'); + +t.deepEqual(other, 'other.js', 'other other'); +t.deepEqual(both, 'both.js', 'other both'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/_config.js new file mode 100644 index 000000000..7433b2b61 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/_config.js @@ -0,0 +1,14 @@ +module.exports = { + description: 'allows configuring requireReturnsDefault for externals with a function', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: (id) => { + if (id === 'external-esm-mixed') return true; + if (id === 'external-esm-default') return false; + return 'auto'; + }, + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/main.js new file mode 100644 index 000000000..4c42fd531 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function-external/main.js @@ -0,0 +1,7 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +t.deepEqual(externalNamed, { foo: 'foo' }, 'named'); +t.deepEqual(externalMixed, 'bar', 'mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/_config.js new file mode 100644 index 000000000..303593eaa --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/_config.js @@ -0,0 +1,50 @@ +module.exports = { + description: 'allows configuring requireReturnsDefault with a function', + options: { + plugins: [ + { + name: 'test-plugin', + resolveId(id) { + if (id.startsWith('dep')) return id; + return null; + }, + load(id) { + const [name, , type] = id.split('_'); + if (name === 'dep') { + switch (type) { + case 'default': + return "export default 'default';"; + case 'mixed': + return "export default 'default'; export const named = 'named';"; + case 'named': + return "export const named = 'named';"; + default: + throw new Error(`Unexpected dependency type "${type}"`); + } + } + return null; + } + } + ] + }, + pluginOptions: { + requireReturnsDefault: (id) => { + const [name, option] = id.split('_'); + if (name === 'dep') { + switch (option) { + case 'false': + return false; + case 'auto': + return 'auto'; + case 'preferred': + return 'preferred'; + case 'true': + return true; + default: + throw new Error(`Unexpected option "${option}"`); + } + } + throw new Error(`Unexpected import of ${id}`); + } + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/main.js new file mode 100644 index 000000000..69f177c9d --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-function/main.js @@ -0,0 +1,29 @@ +const falseDefault = require('dep_false_default_.js'); +const falseMixed = require('dep_false_mixed_.js'); +const falseNamed = require('dep_false_named_.js'); + +const autoDefault = require('dep_auto_default_.js'); +const autoMixed = require('dep_auto_mixed_.js'); +const autoNamed = require('dep_auto_named_.js'); + +const preferredDefault = require('dep_preferred_default_.js'); +const preferredMixed = require('dep_preferred_mixed_.js'); +const preferredNamed = require('dep_preferred_named_.js'); + +const trueDefault = require('dep_true_default_.js'); +const trueMixed = require('dep_true_mixed_.js'); + +t.deepEqual(falseDefault, { default: 'default' }, 'false default'); +t.deepEqual(falseMixed, { default: 'default', named: 'named' }, 'false mixed'); +t.deepEqual(falseNamed, { named: 'named' }, 'false named'); + +t.deepEqual(autoDefault, 'default', 'auto default'); +t.deepEqual(autoMixed, { default: 'default', named: 'named' }, 'auto mixed'); +t.deepEqual(autoNamed, { named: 'named' }, 'auto named'); + +t.deepEqual(preferredDefault, 'default', 'preferred default'); +t.deepEqual(preferredMixed, 'default', 'preferred mixed'); +t.deepEqual(preferredNamed, { named: 'named' }, 'preferred named'); + +t.deepEqual(trueDefault, 'default', 'true default'); +t.deepEqual(trueMixed, 'default', 'true mixed'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/_config.js new file mode 100644 index 000000000..6cec5f85f --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: + 'returns the default if possible when requiring an ES module and requireReturnsDefault is "preferred"', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: 'preferred', + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/main.js new file mode 100644 index 000000000..7420a52c9 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/main.js @@ -0,0 +1,16 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const namedExports = require('./named.js'); +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); +const noExports = require('./none.js'); + +t.deepEqual(namedExports, { foo: 'foo' }, 'named exports'); +t.deepEqual(mixedExports, 'bar', 'mixed exports'); +t.deepEqual(defaultExport, 'bar', 'default export'); +t.deepEqual(noExports, {}, 'no exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, 'bar', 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/none.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-preferred/none.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/_config.js new file mode 100644 index 000000000..a65484de0 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/_config.js @@ -0,0 +1,11 @@ +module.exports = { + description: + 'always returns the default when requiring an ES module and requireReturnsDefault is "true"', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + requireReturnsDefault: true, + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/main.js new file mode 100644 index 000000000..f1c7ad0b2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/main.js @@ -0,0 +1,12 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); + +t.deepEqual(mixedExports, 'bar', 'mixed exports'); +t.deepEqual(defaultExport, 'bar', 'default export'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, 'bar', 'external mixed'); +t.deepEqual(externalDefault, 'bar', 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-true/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/_config.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/_config.js new file mode 100644 index 000000000..66dd8481d --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/_config.js @@ -0,0 +1,10 @@ +module.exports = { + description: + 'returns the namespace when requiring an ES module and requireReturnsDefault is missing', + options: { + external: ['external-esm-named', 'external-esm-mixed', 'external-esm-default'] + }, + pluginOptions: { + esmExternals: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/default.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/default.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/default.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/main.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/main.js new file mode 100644 index 000000000..658db2cdd --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/main.js @@ -0,0 +1,16 @@ +const externalNamed = require('external-esm-named'); +const externalMixed = require('external-esm-mixed'); +const externalDefault = require('external-esm-default'); + +const namedExports = require('./named.js'); +const mixedExports = require('./mixed.js'); +const defaultExport = require('./default.js'); +const noExports = require('./none.js'); + +t.deepEqual(namedExports, { foo: 'foo' }, 'named exports'); +t.deepEqual(mixedExports, { foo: 'foo', default: 'bar' }, 'mixed exports'); +t.deepEqual(defaultExport, { default: 'bar' }, 'default export'); +t.deepEqual(noExports, {}, 'no exports'); +t.deepEqual(externalNamed, { foo: 'foo' }, 'external named'); +t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed'); +t.deepEqual(externalDefault, { default: 'bar' }, 'external default'); diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/mixed.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/mixed.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/mixed.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/named.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/named.js new file mode 100644 index 000000000..3329a7d97 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/named.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/none.js b/packages/commonjs/test/fixtures/function/import-esm-require-returns-default-undefined/none.js new file mode 100644 index 000000000..e69de29bb diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/_config.js b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/_config.js new file mode 100644 index 000000000..3ca9ce01a --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'treats a namespace that is exported as a binding as having named exports', + pluginOptions: { + requireReturnsDefault: 'auto' + } +}; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/dep.js b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/dep.js new file mode 100644 index 000000000..76e020f89 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/dep.js @@ -0,0 +1,2 @@ +export * as ns from './other.js'; +export default 'default'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/main.js b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/main.js new file mode 100644 index 000000000..a7aecbb07 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/main.js @@ -0,0 +1,3 @@ +const dep = require('./dep.js'); + +t.deepEqual(dep, { default: 'default', ns: { default: 'bar', foo: 'foo' } }); diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/other.js b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/other.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-named-namespace/other.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/_config.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/_config.js new file mode 100644 index 000000000..44b0a4fbc --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: + 'treats an ES module that reexports a namespace and a default export as having named exports', + pluginOptions: { + requireReturnsDefault: 'auto' + } +}; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/dep.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/dep.js new file mode 100644 index 000000000..3f519ec7b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/dep.js @@ -0,0 +1,2 @@ +export * from './other.js'; +export default 'default'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/main.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/main.js new file mode 100644 index 000000000..7e205f40b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/main.js @@ -0,0 +1,3 @@ +const dep = require('./dep.js'); + +t.deepEqual(dep, { default: 'default', foo: 'foo' }); diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/other.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/other.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-and-default/other.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/_config.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/_config.js new file mode 100644 index 000000000..4ca6871e2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'treats a namespace that is exported as default as a default export', + pluginOptions: { + requireReturnsDefault: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/dep.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/dep.js new file mode 100644 index 000000000..7f7fdd80d --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/dep.js @@ -0,0 +1,3 @@ +import * as other from './other.js'; + +export default other; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/main.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/main.js new file mode 100644 index 000000000..3170bc2b2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/main.js @@ -0,0 +1,3 @@ +const dep = require('./dep.js'); + +t.deepEqual(dep, { default: 'bar', foo: 'foo' }); diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/other.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/other.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-named-default/other.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/_config.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/_config.js new file mode 100644 index 000000000..9a2473d8c --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/_config.js @@ -0,0 +1,7 @@ +module.exports = { + description: + 'handles requiring an ES module that reexports a namespace when returning the default is preferred', + pluginOptions: { + requireReturnsDefault: 'preferred' + } +}; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/dep.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/dep.js new file mode 100644 index 000000000..c57cba00e --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/dep.js @@ -0,0 +1 @@ +export * from './other.js'; diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/main.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/main.js new file mode 100644 index 000000000..ba48ec4e6 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/main.js @@ -0,0 +1,3 @@ +const dep = require('./dep.js'); + +t.deepEqual(dep, { foo: 'foo' }); diff --git a/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/other.js b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/other.js new file mode 100644 index 000000000..53a79aab5 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/require-esm-with-namespace-prefer-default/other.js @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export default 'bar'; diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules/_config.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules/_config.js new file mode 100644 index 000000000..03e15e77e --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'transforms mixed modules', + pluginOptions: { + transformMixedEsModules: true + } +}; diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules/dep.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules/dep.js new file mode 100644 index 000000000..5f66b6b89 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules/dep.js @@ -0,0 +1,3 @@ +const other = require('./other.js'); + +export { other }; diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules/main.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules/main.js new file mode 100644 index 000000000..5d9c3a017 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules/main.js @@ -0,0 +1,6 @@ +import { other } from './dep.js'; + +const dep = require('./dep.js'); + +t.is(other, 'other'); +t.deepEqual(dep, { other: 'other' }); diff --git a/packages/commonjs/test/fixtures/function/transform-mixed-modules/other.js b/packages/commonjs/test/fixtures/function/transform-mixed-modules/other.js new file mode 100644 index 000000000..a40bd61c9 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transform-mixed-modules/other.js @@ -0,0 +1 @@ +module.exports = 'other'; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-default/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-default/_config.js new file mode 100644 index 000000000..087629186 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-default/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports the default from a transpiled ES module with only default export', + options: { + external: 'external-esm-default' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-default/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-default/main.js index 836cb7165..69f88336d 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-default/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-default/main.js @@ -1,3 +1,6 @@ +import external from 'external-esm-default'; + import dep from './dep'; t.is(dep, 'default'); +t.is(external, 'bar'); diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/_config.js new file mode 100644 index 000000000..19fe8fa3c --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports default and named exports from a transpiled ES module', + options: { + external: 'external-esm-mixed' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/main.js index e32c46bba..7b84eb8b3 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-mixed/main.js @@ -1,4 +1,8 @@ +import external, { foo } from 'external-esm-mixed'; + import dep, { named } from './dep'; t.is(dep, 'default'); t.is(named, 'named'); +t.is(external, 'bar'); +t.is(foo, 'foo'); diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-named/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-named/_config.js new file mode 100644 index 000000000..e9cfc54d2 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-named/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports named exports from a transpiled ES module', + options: { + external: 'external-esm-named' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-named/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-named/main.js index e3cfa236b..f0586f566 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-named/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-named/main.js @@ -1,3 +1,6 @@ +import { foo } from 'external-esm-named'; + import { named } from './dep'; t.is(named, 'named'); +t.is(foo, 'foo'); diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/_config.js new file mode 100644 index 000000000..8dddfbd76 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports the namespace from a transpiled ES module with only default export', + options: { + external: 'external-esm-default' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/main.js index ff8f117e6..8263cb00a 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-default/main.js @@ -1,5 +1,11 @@ -import * as x from './dep'; +import * as external from 'external-esm-default'; -t.deepEqual(x, { +import * as dep from './dep'; + +t.deepEqual(dep, { default: 'default' }); + +t.deepEqual(external, { + default: 'bar' +}); diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/_config.js new file mode 100644 index 000000000..f8c962532 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports the namespace from a transpiled ES module with mixed exports', + options: { + external: 'external-esm-mixed' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/main.js index f5a66e8c9..75c7f8d8c 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-mixed/main.js @@ -1,6 +1,13 @@ -import * as x from './dep'; +import * as external from 'external-esm-mixed'; -t.deepEqual(x, { +import * as dep from './dep'; + +t.deepEqual(dep, { default: 'default', named: 'named' }); + +t.deepEqual(external, { + default: 'bar', + foo: 'foo' +}); diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/_config.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/_config.js new file mode 100644 index 000000000..6fb63a61c --- /dev/null +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'imports the namespace from a transpiled ES module with named exports', + options: { + external: 'external-esm-named' + } +}; diff --git a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/main.js b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/main.js index 0aca723ef..a275e5964 100644 --- a/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/main.js +++ b/packages/commonjs/test/fixtures/function/transpiled-esm-namespace-named/main.js @@ -1,6 +1,8 @@ -import * as x from './dep'; +import * as external from 'external-esm-named'; -t.deepEqual(x, { +import * as dep from './dep'; + +t.deepEqual(dep, { named: 'named', // Technically, this should ideally not exist, or if we cannot avoid it due // to runtime default export detection, it should probably be undefined. We @@ -9,3 +11,7 @@ t.deepEqual(x, { // been updated default: { named: 'named' } }); + +t.deepEqual(external, { + foo: 'foo' +}); diff --git a/packages/commonjs/test/node_modules/external-cjs-exports/index.js b/packages/commonjs/test/node_modules/external-cjs-exports/index.js new file mode 100644 index 000000000..94ecacb72 --- /dev/null +++ b/packages/commonjs/test/node_modules/external-cjs-exports/index.js @@ -0,0 +1 @@ +exports.foo = 'foo'; diff --git a/packages/commonjs/test/node_modules/external-cjs-module-exports/index.js b/packages/commonjs/test/node_modules/external-cjs-module-exports/index.js new file mode 100644 index 000000000..cb1c2c01e --- /dev/null +++ b/packages/commonjs/test/node_modules/external-cjs-module-exports/index.js @@ -0,0 +1 @@ +module.exports = 'bar'; diff --git a/packages/commonjs/test/node_modules/external-esm-default/index.js b/packages/commonjs/test/node_modules/external-esm-default/index.js new file mode 100644 index 000000000..3b59bb067 --- /dev/null +++ b/packages/commonjs/test/node_modules/external-esm-default/index.js @@ -0,0 +1,2 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = 'bar'; diff --git a/packages/commonjs/test/node_modules/external-esm-mixed/index.js b/packages/commonjs/test/node_modules/external-esm-mixed/index.js new file mode 100644 index 000000000..6ec4d58a8 --- /dev/null +++ b/packages/commonjs/test/node_modules/external-esm-mixed/index.js @@ -0,0 +1,3 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.foo = 'foo'; +exports.default = 'bar'; diff --git a/packages/commonjs/test/node_modules/external-esm-named/index.js b/packages/commonjs/test/node_modules/external-esm-named/index.js new file mode 100644 index 000000000..b64b8cf38 --- /dev/null +++ b/packages/commonjs/test/node_modules/external-esm-named/index.js @@ -0,0 +1,2 @@ +Object.defineProperty(exports, '__esModule', { value: true }); +exports.foo = 'foo'; diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index a4c9234e5..913a5d27c 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -2,7 +2,7 @@ The actual snapshot is saved in `function.js.snap`. -Generated by [AVA](https://ava.li). +Generated by [AVA](https://avajs.dev). ## assign-properties-to-default-export @@ -780,7 +780,7 @@ Generated by [AVA](https://ava.li). ␊ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ ␊ - var path = _interopDefault(require('path'));␊ + var Path = _interopDefault(require('path'));␊ ␊ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ ␊ @@ -931,7 +931,7 @@ Generated by [AVA](https://ava.li). ␊ const basePath = `${process.cwd()}/fixtures/function/dynamic-require-absolute-paths`;␊ ␊ - t.is(commonjsRequire(path.resolve(`${basePath}/submodule.js`),"/$$rollup_base$$/fixtures/function/dynamic-require-absolute-paths"), 'submodule');␊ + t.is(commonjsRequire(Path.resolve(`${basePath}/submodule.js`),"/$$rollup_base$$/fixtures/function/dynamic-require-absolute-paths"), 'submodule');␊ ␊ var main = {␊ ␊ @@ -1851,10 +1851,6 @@ Generated by [AVA](https://ava.li). ␊ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ ␊ - function getCjsExportFromNamespace (n) {␊ - return n && n['default'] || n;␊ - }␊ - ␊ function commonjsRegister (path, loader) {␊ DYNAMIC_REQUIRE_LOADERS[path] = loader;␊ }␊ @@ -1992,18 +1988,10 @@ Generated by [AVA](https://ava.li). commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊ ␊ var value = "present";␊ - var dynamic = {␊ + var require$$0 = {␊ value: value␊ };␊ ␊ - var dynamic$1 = /*#__PURE__*/Object.freeze({␊ - __proto__: null,␊ - value: value,␊ - 'default': dynamic␊ - });␊ - ␊ - var require$$0 = getCjsExportFromNamespace(dynamic$1);␊ - ␊ commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-json/dynamic.json", function (module, exports) {␊ module.exports = require$$0;␊ });␊ @@ -2229,10 +2217,6 @@ Generated by [AVA](https://ava.li). ␊ var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ ␊ - function getCjsExportFromNamespace (n) {␊ - return n && n['default'] || n;␊ - }␊ - ␊ function commonjsRegister (path, loader) {␊ DYNAMIC_REQUIRE_LOADERS[path] = loader;␊ }␊ @@ -2375,18 +2359,10 @@ Generated by [AVA](https://ava.li). });␊ ␊ var main = "./entry.js";␊ - var _package = {␊ + var require$$0 = {␊ main: main␊ };␊ ␊ - var _package$1 = /*#__PURE__*/Object.freeze({␊ - __proto__: null,␊ - main: main,␊ - 'default': _package␊ - });␊ - ␊ - var require$$0 = getCjsExportFromNamespace(_package$1);␊ - ␊ commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-package-sub/node_modules/custom-module/package.json", function (module, exports) {␊ module.exports = require$$0;␊ });␊ @@ -2803,6 +2779,149 @@ Generated by [AVA](https://ava.li). `, } +## esm-externals-false + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalExports = _interopDefault(require('external-cjs-exports'));␊ + var externalModuleExports = _interopDefault(require('external-cjs-module-exports'));␊ + var externalNamed = _interopDefault(require('external-esm-named'));␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalDefault = _interopDefault(require('external-esm-default'));␊ + ␊ + t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ + t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalDefault, 'bar', 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## esm-externals-function + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalExports = _interopDefault(require('external-cjs-exports'));␊ + var externalModuleExports = _interopDefault(require('external-cjs-module-exports'));␊ + var externalNamed = _interopDefault(require('external-esm-named'));␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ + t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## esm-externals-list + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalExports = _interopDefault(require('external-cjs-exports'));␊ + var externalModuleExports = _interopDefault(require('external-cjs-module-exports'));␊ + var externalNamed = _interopDefault(require('external-esm-named'));␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ + t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## esm-externals-true + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalCjsExports = require('external-cjs-exports');␊ + var externalCjsModuleExports = require('external-cjs-module-exports');␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + t.deepEqual(externalCjsExports, { foo: 'foo' }, 'external exports');␊ + t.deepEqual(externalCjsModuleExports, 'bar', 'external module exports');␊ + t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalEsmMixed, { default: 'bar', foo: 'foo' }, 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## esm-externals-undefined + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalExports = _interopDefault(require('external-cjs-exports'));␊ + var externalModuleExports = _interopDefault(require('external-cjs-module-exports'));␊ + var externalNamed = _interopDefault(require('external-esm-named'));␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalDefault = _interopDefault(require('external-esm-default'));␊ + ␊ + t.deepEqual(externalExports, { foo: 'foo' }, 'external exports');␊ + t.deepEqual(externalModuleExports, 'bar', 'external module exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalDefault, 'bar', 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + ## export-default-from > Snapshot 1 @@ -2965,6 +3084,398 @@ Generated by [AVA](https://ava.li). `, } +## import-esm-require-returns-default-auto + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + const foo = 'foo';␊ + ␊ + var named = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + const foo$1 = 'foo';␊ + var mixed = 'bar';␊ + ␊ + var mixed$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo$1,␊ + 'default': mixed␊ + });␊ + ␊ + var defaultExport = 'bar';␊ + ␊ + var none = /*#__PURE__*/Object.freeze({␊ + __proto__: null␊ + });␊ + ␊ + function getDefaultExportFromNamespaceIfNotNamed (n) {␊ + return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(externalEsmNamed);␊ + ␊ + var externalMixed = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(externalEsmMixed);␊ + ␊ + var externalDefault = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(externalEsmDefault);␊ + ␊ + t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(defaultExport, 'bar', 'default export');␊ + t.deepEqual(none, {}, 'no exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalDefault, 'bar', 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-false + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + const foo = 'foo';␊ + ␊ + var named = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + const foo$1 = 'foo';␊ + var mixed = 'bar';␊ + ␊ + var mixed$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo$1,␊ + 'default': mixed␊ + });␊ + ␊ + var _default = 'bar';␊ + ␊ + var _default$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': _default␊ + });␊ + ␊ + var none = /*#__PURE__*/Object.freeze({␊ + __proto__: null␊ + });␊ + ␊ + t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(_default$1, { default: 'bar' }, 'default export');␊ + t.deepEqual(none, {}, 'no exports');␊ + t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalEsmMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-function + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var dep_false_default_ = 'default';␊ + ␊ + var dep_false_default_$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': dep_false_default_␊ + });␊ + ␊ + var dep_false_mixed_ = 'default'; const named = 'named';␊ + ␊ + var dep_false_mixed_$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': dep_false_mixed_,␊ + named: named␊ + });␊ + ␊ + const named$1 = 'named';␊ + ␊ + var dep_false_named_ = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + named: named$1␊ + });␊ + ␊ + var autoDefault = 'default';␊ + ␊ + var dep_auto_mixed_ = 'default'; const named$2 = 'named';␊ + ␊ + var dep_auto_mixed_$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': dep_auto_mixed_,␊ + named: named$2␊ + });␊ + ␊ + const named$3 = 'named';␊ + ␊ + var dep_auto_named_ = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + named: named$3␊ + });␊ + ␊ + var preferredDefault = 'default';␊ + ␊ + var preferredMixed = 'default';␊ + ␊ + const named$4 = 'named';␊ + ␊ + var dep_preferred_named_ = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + named: named$4␊ + });␊ + ␊ + var trueDefault = 'default';␊ + ␊ + var trueMixed = 'default';␊ + ␊ + t.deepEqual(dep_false_default_$1, { default: 'default' }, 'false default');␊ + t.deepEqual(dep_false_mixed_$1, { default: 'default', named: 'named' }, 'false mixed');␊ + t.deepEqual(dep_false_named_, { named: 'named' }, 'false named');␊ + ␊ + t.deepEqual(autoDefault, 'default', 'auto default');␊ + t.deepEqual(dep_auto_mixed_$1, { default: 'default', named: 'named' }, 'auto mixed');␊ + t.deepEqual(dep_auto_named_, { named: 'named' }, 'auto named');␊ + ␊ + t.deepEqual(preferredDefault, 'default', 'preferred default');␊ + t.deepEqual(preferredMixed, 'default', 'preferred mixed');␊ + t.deepEqual(dep_preferred_named_, { named: 'named' }, 'preferred named');␊ + ␊ + t.deepEqual(trueDefault, 'default', 'true default');␊ + t.deepEqual(trueMixed, 'default', 'true mixed');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-function-called-once + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var other = 'other.js';␊ + ␊ + var both = 'both.js';␊ + ␊ + t.deepEqual(other, 'other.js', 'other other');␊ + t.deepEqual(both, 'both.js', 'other both');␊ + ␊ + var main = 'main.js';␊ + ␊ + t.deepEqual(main, 'main.js', 'main main');␊ + t.deepEqual(both, 'both.js', 'main both');␊ + ␊ + var main_1 = {␊ + ␊ + };␊ + ␊ + module.exports = main_1;␊ + `, + } + +## import-esm-require-returns-default-function-external + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + function getDefaultExportFromNamespaceIfNotNamed (n) {␊ + return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(externalEsmNamed);␊ + ␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'named');␊ + t.deepEqual(externalMixed, 'bar', 'mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-preferred + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + const foo = 'foo';␊ + ␊ + var named = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + var mixedExports = 'bar';␊ + ␊ + var defaultExport = 'bar';␊ + ␊ + var none = /*#__PURE__*/Object.freeze({␊ + __proto__: null␊ + });␊ + ␊ + function getDefaultExportFromNamespaceIfPresent (n) {␊ + return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;␊ + }␊ + ␊ + var externalNamed = /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(externalEsmNamed);␊ + ␊ + var externalMixed = /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(externalEsmMixed);␊ + ␊ + var externalDefault = /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(externalEsmDefault);␊ + ␊ + t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixedExports, 'bar', 'mixed exports');␊ + t.deepEqual(defaultExport, 'bar', 'default export');␊ + t.deepEqual(none, {}, 'no exports');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalDefault, 'bar', 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-true + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var externalNamed = _interopDefault(require('external-esm-named'));␊ + var externalMixed = _interopDefault(require('external-esm-mixed'));␊ + var externalDefault = _interopDefault(require('external-esm-default'));␊ + ␊ + var mixedExports = 'bar';␊ + ␊ + var defaultExport = 'bar';␊ + ␊ + t.deepEqual(mixedExports, 'bar', 'mixed exports');␊ + t.deepEqual(defaultExport, 'bar', 'default export');␊ + t.deepEqual(externalNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalMixed, 'bar', 'external mixed');␊ + t.deepEqual(externalDefault, 'bar', 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## import-esm-require-returns-default-undefined + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var externalEsmNamed = require('external-esm-named');␊ + var externalEsmMixed = require('external-esm-mixed');␊ + var externalEsmDefault = require('external-esm-default');␊ + ␊ + const foo = 'foo';␊ + ␊ + var named = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + const foo$1 = 'foo';␊ + var mixed = 'bar';␊ + ␊ + var mixed$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo$1,␊ + 'default': mixed␊ + });␊ + ␊ + var _default = 'bar';␊ + ␊ + var _default$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': _default␊ + });␊ + ␊ + var none = /*#__PURE__*/Object.freeze({␊ + __proto__: null␊ + });␊ + ␊ + t.deepEqual(named, { foo: 'foo' }, 'named exports');␊ + t.deepEqual(mixed$1, { foo: 'foo', default: 'bar' }, 'mixed exports');␊ + t.deepEqual(_default$1, { default: 'bar' }, 'default export');␊ + t.deepEqual(none, {}, 'no exports');␊ + t.deepEqual(externalEsmNamed, { foo: 'foo' }, 'external named');␊ + t.deepEqual(externalEsmMixed, { foo: 'foo', default: 'bar' }, 'external mixed');␊ + t.deepEqual(externalEsmDefault, { default: 'bar' }, 'external default');␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + ## index > Snapshot 1 @@ -3335,6 +3846,117 @@ Generated by [AVA](https://ava.li). `, } +## require-esm-with-named-namespace + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + const foo = 'foo';␊ + var other = 'bar';␊ + ␊ + var other$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo,␊ + 'default': other␊ + });␊ + ␊ + var dep = 'default';␊ + ␊ + var dep$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': dep,␊ + ns: other$1␊ + });␊ + ␊ + t.deepEqual(dep$1, { default: 'default', ns: { default: 'bar', foo: 'foo' } });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## require-esm-with-namespace-and-default + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + const foo = 'foo';␊ + ␊ + var dep = 'default';␊ + ␊ + var dep$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + 'default': dep,␊ + foo: foo␊ + });␊ + ␊ + t.deepEqual(dep$1, { default: 'default', foo: 'foo' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## require-esm-with-namespace-named-default + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + const foo = 'foo';␊ + var other = 'bar';␊ + ␊ + var other$1 = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo,␊ + 'default': other␊ + });␊ + ␊ + t.deepEqual(other$1, { default: 'bar', foo: 'foo' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + +## require-esm-with-namespace-prefer-default + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + const foo = 'foo';␊ + ␊ + var dep = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + foo: foo␊ + });␊ + ␊ + t.deepEqual(dep, { foo: 'foo' });␊ + ␊ + var main = {␊ + ␊ + };␊ + ␊ + module.exports = main;␊ + `, + } + ## resolve-is-cjs-extension > Snapshot 1 @@ -3349,13 +3971,7 @@ Generated by [AVA](https://ava.li). result: result␊ });␊ ␊ - function getCjsExportFromNamespace (n) {␊ - return n && n['default'] || n;␊ - }␊ - ␊ - var require$$0 = getCjsExportFromNamespace(second);␊ - ␊ - t.is(require$$0.result, 'second');␊ + t.is(second.result, 'second');␊ ␊ var main = {␊ ␊ @@ -3379,13 +3995,7 @@ Generated by [AVA](https://ava.li). result: result␊ });␊ ␊ - function getCjsExportFromNamespace (n) {␊ - return n && n['default'] || n;␊ - }␊ - ␊ - var require$$0 = getCjsExportFromNamespace(second);␊ - ␊ - t.is(require$$0.result, 'second');␊ + t.is(second.result, 'second');␊ ␊ var main = {␊ ␊ @@ -3532,6 +4142,25 @@ Generated by [AVA](https://ava.li). `, } +## transform-mixed-modules + +> Snapshot 1 + + { + 'main.js': `'use strict';␊ + ␊ + var other = 'other';␊ + ␊ + var dep = /*#__PURE__*/Object.freeze({␊ + __proto__: null,␊ + other: other␊ + });␊ + ␊ + t.is(other, 'other');␊ + t.deepEqual(dep, { other: 'other' });␊ + `, + } + ## transpiled-esm-default > Snapshot 1 @@ -3539,6 +4168,10 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var external = _interopDefault(require('external-esm-default'));␊ + ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ }␊ @@ -3565,6 +4198,7 @@ Generated by [AVA](https://ava.li). var dep$1 = /*@__PURE__*/getDefaultExportFromCjs(dep);␊ ␊ t.is(dep$1, 'default');␊ + t.is(external, 'bar');␊ `, } @@ -3749,6 +4383,11 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }␊ + ␊ + var external = require('external-esm-mixed');␊ + var external__default = _interopDefault(external);␊ + ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ }␊ @@ -3777,6 +4416,8 @@ Generated by [AVA](https://ava.li). ␊ t.is(dep$1, 'default');␊ t.is(dep.named, 'named');␊ + t.is(external__default, 'bar');␊ + t.is(external.foo, 'foo');␊ `, } @@ -3787,6 +4428,8 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + var externalEsmNamed = require('external-esm-named');␊ + ␊ function createCommonjsModule(fn, basedir, module) {␊ return module = {␊ path: basedir,␊ @@ -3807,6 +4450,7 @@ Generated by [AVA](https://ava.li). });␊ ␊ t.is(dep.named, 'named');␊ + t.is(externalEsmNamed.foo, 'foo');␊ `, } @@ -3817,6 +4461,8 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + var external = require('external-esm-default');␊ + ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ }␊ @@ -3842,13 +4488,17 @@ Generated by [AVA](https://ava.li). ␊ var dep$1 = /*@__PURE__*/getDefaultExportFromCjs(dep);␊ ␊ - var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ + var dep$2 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ 'default': dep$1␊ }));␊ ␊ - t.deepEqual(x, {␊ + t.deepEqual(dep$2, {␊ default: 'default'␊ });␊ + ␊ + t.deepEqual(external, {␊ + default: 'bar'␊ + });␊ `, } @@ -3859,6 +4509,8 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + var external = require('external-esm-mixed');␊ + ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ }␊ @@ -3885,14 +4537,19 @@ Generated by [AVA](https://ava.li). ␊ var dep$1 = /*@__PURE__*/getDefaultExportFromCjs(dep);␊ ␊ - var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ + var dep$2 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ 'default': dep$1␊ }));␊ ␊ - t.deepEqual(x, {␊ + t.deepEqual(dep$2, {␊ default: 'default',␊ named: 'named'␊ });␊ + ␊ + t.deepEqual(external, {␊ + default: 'bar',␊ + foo: 'foo'␊ + });␊ `, } @@ -3903,6 +4560,8 @@ Generated by [AVA](https://ava.li). { 'main.js': `'use strict';␊ ␊ + var external = require('external-esm-named');␊ + ␊ function getDefaultExportFromCjs (x) {␊ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊ }␊ @@ -3928,11 +4587,11 @@ Generated by [AVA](https://ava.li). ␊ var dep$1 = /*@__PURE__*/getDefaultExportFromCjs(dep);␊ ␊ - var x = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ + var dep$2 = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), dep, {␊ 'default': dep$1␊ }));␊ ␊ - t.deepEqual(x, {␊ + t.deepEqual(dep$2, {␊ named: 'named',␊ // Technically, this should ideally not exist, or if we cannot avoid it due␊ // to runtime default export detection, it should probably be undefined. We␊ @@ -3941,6 +4600,10 @@ Generated by [AVA](https://ava.li). // been updated␊ default: { named: 'named' }␊ });␊ + ␊ + t.deepEqual(external, {␊ + foo: 'foo'␊ + });␊ `, } diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 3fba41138..244c8fc65 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ diff --git a/packages/commonjs/test/test.js b/packages/commonjs/test/test.js index d32cca7e0..53a6289db 100644 --- a/packages/commonjs/test/test.js +++ b/packages/commonjs/test/test.js @@ -315,7 +315,7 @@ test('deconflicts reserved keywords', async (t) => { plugins: [commonjs()] }); - const reservedProp = (await executeBundle(bundle, t, { exports: 'named' })).exports.delete; + const reservedProp = (await executeBundle(bundle, t)).exports.delete; t.is(reservedProp, 'foo'); }); @@ -357,7 +357,7 @@ test('rewrites top-level defines', async (t) => { define.amd = true; - const { exports } = await executeBundle(bundle, { context: { define } }); + const { exports } = await executeBundle(bundle, t, { context: { define } }); t.is(exports, 42); }); @@ -445,6 +445,7 @@ test('creates an error with a code frame when parsing fails', async (t) => { } }); +// Virtual modules are treated as "requireReturnsDefault: 'always'" to avoid interop test('ignores virtual modules', async (t) => { const bundle = await rollup({ input: 'fixtures/samples/ignore-virtual-modules/main.js', @@ -509,7 +510,7 @@ test('produces optimized code when importing esm with a known default export', a const bundle = await rollup({ input: 'main.js', plugins: [ - commonjs(), + commonjs({ requireReturnsDefault: true }), { load(id) { if (id === 'main.js') { diff --git a/packages/data-uri/package.json b/packages/data-uri/package.json index 0329bbc7c..1b7616180 100644 --- a/packages/data-uri/package.json +++ b/packages/data-uri/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/dsv/package.json b/packages/dsv/package.json index ba03ba82c..978627eeb 100755 --- a/packages/dsv/package.json +++ b/packages/dsv/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/dynamic-import-vars/package.json b/packages/dynamic-import-vars/package.json index d64a9eff0..8d59428a0 100644 --- a/packages/dynamic-import-vars/package.json +++ b/packages/dynamic-import-vars/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/html/package.json b/packages/html/package.json index 2695a87d7..54eaa5a9d 100644 --- a/packages/html/package.json +++ b/packages/html/package.json @@ -20,7 +20,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write *.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write *.md", "lint:js": "eslint --fix --cache lib test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prepublishOnly": "pnpm run lint", diff --git a/packages/image/package.json b/packages/image/package.json index 4dcd4e9eb..de33ad529 100644 --- a/packages/image/package.json +++ b/packages/image/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write *.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write *.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/inject/package.json b/packages/inject/package.json index 96b70c714..f951c9de1 100644 --- a/packages/inject/package.json +++ b/packages/inject/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose && pnpm run test:ts", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/json/package.json b/packages/json/package.json index 6fb2c3c86..cc2032f2e 100755 --- a/packages/json/package.json +++ b/packages/json/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose && pnpm run test:ts", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:json && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:json": "prettier --write \"test/fixtures/!(garbage)/*.json\"", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", diff --git a/packages/legacy/package.json b/packages/legacy/package.json index 33debfefe..504576b26 100644 --- a/packages/legacy/package.json +++ b/packages/legacy/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/multi-entry/package.json b/packages/multi-entry/package.json index 7d551ef00..5716dbce7 100755 --- a/packages/multi-entry/package.json +++ b/packages/multi-entry/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/node-resolve/package.json b/packages/node-resolve/package.json index 5bd97a8fe..b51e556b7 100644 --- a/packages/node-resolve/package.json +++ b/packages/node-resolve/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose && pnpm run test:ts", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/pluginutils/package.json b/packages/pluginutils/package.json index 0093e27a6..ea06453cb 100644 --- a/packages/pluginutils/package.json +++ b/packages/pluginutils/package.json @@ -24,7 +24,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/replace/package.json b/packages/replace/package.json index b07a42fed..202cd946e 100644 --- a/packages/replace/package.json +++ b/packages/replace/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose && pnpm run test:ts", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/run/package.json b/packages/run/package.json index 61f9e861b..3df23985d 100644 --- a/packages/run/package.json +++ b/packages/run/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:json && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test types --ext .js,.ts", "lint:json": "prettier --write tsconfig.json", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", diff --git a/packages/run/src/index.ts b/packages/run/src/index.ts index 39f6806a0..20d9d018b 100644 --- a/packages/run/src/index.ts +++ b/packages/run/src/index.ts @@ -17,7 +17,7 @@ export default function run(opts: RollupRunOptions = {}): Plugin { name: 'run', buildStart(options) { - let inputs = options.input!; + let inputs = options.input; if (typeof inputs === 'string') { inputs = [inputs]; diff --git a/packages/strip/package.json b/packages/strip/package.json index 04f9f7e7e..04c8860c3 100644 --- a/packages/strip/package.json +++ b/packages/strip/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "pretest": "pnpm run build", diff --git a/packages/sucrase/package.json b/packages/sucrase/package.json index f41e198e2..424299f03 100644 --- a/packages/sucrase/package.json +++ b/packages/sucrase/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/typescript/package.json b/packages/typescript/package.json index 3b3e64d52..f255e8316 100644 --- a/packages/typescript/package.json +++ b/packages/typescript/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose --serial", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:json && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:json": "prettier --write **/tsconfig.json", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", diff --git a/packages/url/package.json b/packages/url/package.json index 4e23ae948..5487714b0 100644 --- a/packages/url/package.json +++ b/packages/url/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/virtual/package.json b/packages/virtual/package.json index a1319a6bd..da8cabf62 100755 --- a/packages/virtual/package.json +++ b/packages/virtual/package.json @@ -19,7 +19,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/wasm/package.json b/packages/wasm/package.json index e8ee19f94..0ff060a36 100644 --- a/packages/wasm/package.json +++ b/packages/wasm/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/packages/yaml/package.json b/packages/yaml/package.json index a9d6dba65..a53f06e75 100755 --- a/packages/yaml/package.json +++ b/packages/yaml/package.json @@ -22,7 +22,7 @@ "ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}", "ci:test": "pnpm run test -- --verbose", "lint": "pnpm run lint:js && pnpm run lint:docs && pnpm run lint:package", - "lint:docs": "prettier --single-quote --arrow-parens avoid --write README.md", + "lint:docs": "prettier --single-quote --arrow-parens avoid --trailing-comma none --write README.md", "lint:js": "eslint --fix --cache src test --ext .js,.ts", "lint:package": "prettier --write package.json --plugin=prettier-plugin-package", "prebuild": "del-cli dist", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7baba2c32..07567329f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -145,7 +145,6 @@ importers: devDependencies: '@rollup/plugin-json': 4.1.0_rollup@2.23.0 '@rollup/plugin-node-resolve': 8.4.0_rollup@2.23.0 - acorn: 7.3.1 locate-character: 2.0.5 require-relative: 0.8.7 rollup: 2.23.0 @@ -157,7 +156,6 @@ importers: '@rollup/plugin-json': ^4.1.0 '@rollup/plugin-node-resolve': ^8.4.0 '@rollup/pluginutils': ^3.1.0 - acorn: ^7.3.1 commondir: ^1.0.1 estree-walker: ^2.0.1 glob: ^7.1.6 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 18ec407ef..dee51e928 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,2 +1,2 @@ packages: - - 'packages/*' + - "packages/*"