Skip to content

Commit

Permalink
feat(commonjs): make defaultIsModuleExports as funtion to config defa…
Browse files Browse the repository at this point in the history
…ultIsModuleExports for each source (#1052)
  • Loading branch information
morlay authored Apr 15, 2022
1 parent d28d19a commit 3392e6e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/commonjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default function commonjs(options = {}) {
ignoreGlobal,
ignoreDynamicRequires,
requireReturnsDefault: requireReturnsDefaultOption,
defaultIsModuleExports: defaultIsModuleExportsOption,
esmExternals
} = options;
const getRequireReturnsDefault =
Expand All @@ -60,8 +61,11 @@ export default function commonjs(options = {}) {
: Array.isArray(esmExternals)
? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
: () => esmExternals;
const defaultIsModuleExports =
typeof options.defaultIsModuleExports === 'boolean' ? options.defaultIsModuleExports : 'auto';
const getDefaultIsModuleExports =
typeof defaultIsModuleExportsOption === 'function'
? defaultIsModuleExportsOption
: () =>
typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';

const { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
options.dynamicRequireTargets
Expand Down Expand Up @@ -150,7 +154,7 @@ export default function commonjs(options = {}) {
disableWrap,
commonDir,
ast,
defaultIsModuleExports
getDefaultIsModuleExports(id)
);
}

Expand Down
8 changes: 8 additions & 0 deletions packages/commonjs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FilterPattern } from '@rollup/pluginutils';
import { Plugin } from 'rollup';

type RequireReturnsDefaultOption = boolean | 'auto' | 'preferred' | 'namespace';
type DefaultIsModuleExportsOption = boolean | 'auto';

interface RollupCommonJSOptions {
/**
Expand Down Expand Up @@ -161,6 +162,13 @@ interface RollupCommonJSOptions {
requireReturnsDefault?:
| RequireReturnsDefaultOption
| ((id: string) => RequireReturnsDefaultOption);

/**
* @default "auto"
*/
defaultIsModuleExports?:
| DefaultIsModuleExportsOption
| ((id: string) => DefaultIsModuleExportsOption);
/**
* Some modules contain dynamic `require` calls, or require modules that
* contain circular dependencies, which are not handled well by static
Expand Down

0 comments on commit 3392e6e

Please sign in to comment.