Skip to content

Commit

Permalink
Change option
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 19, 2021
1 parent 599c8b9 commit b514add
Show file tree
Hide file tree
Showing 27 changed files with 206 additions and 61 deletions.
68 changes: 52 additions & 16 deletions packages/commonjs/README.md
Expand Up @@ -34,9 +34,9 @@ export default {
input: 'src/index.js',
output: {
dir: 'output',
format: 'cjs'
format: 'cjs',
},
plugins: [commonjs()]
plugins: [commonjs()],
};
```

Expand Down Expand Up @@ -66,8 +66,8 @@ 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',
],
});
```

Expand Down Expand Up @@ -174,12 +174,48 @@ If you set `esmExternals` to `true`, this plugins assumes that all external depe

You can also supply an array of ids to be treated as ES modules, or a function that will be passed each external id to determine if it is an ES module.

### `nodeDefaultImport`
### `defaultIsModuleExports`

Type: `boolean`<br>
Default: `false`
Type: `boolean | "auto"`<br>
Default: `"auto"`

Controls what is the default export when importing a CommonJS file from an ES module.

When `true`, match the Node.js behavior when importing the `default` import from a CommonJS module, making it return the value of `module.exports`.
- `true`: The value of the default export is `module.exports`. This currently matches the behavior of Node.js when importing a CommonJS file.
```js
// mod.cjs
exports.default = 3;
```
```js
import foo from './mod.cjs';
console.log(foo); // { default: 3 }
```
- `false`: The value of the default export is `exports.default`.
```js
// mod.cjs
exports.default = 3;
```
```js
import foo from './mod.cjs';
console.log(foo); // 3
```
- `"auto"`: The value of the default export is `exports.default` if the CommonJS file has an `exports.__esModule === true` property; otherwise it's `module.exports`. This makes it possible to import
the default export of ES modules compiled to CommonJS as if they were not compiled.
```js
// mod.cjs
exports.default = 3;
```
```js
// mod-compiled.cjs
exports.__esModule = true;
exports.default = 3;
```
```js
import foo from './mod.cjs';
import bar from './mod-compiled.cjs';
console.log(foo); // { default: 3 }
console.log(bar); // 3
```

### `requireReturnsDefault`

Expand Down Expand Up @@ -214,7 +250,7 @@ This is in line with how other bundlers handle this situation and is also the mo

var dep$1 = /*#__PURE__*/ Object.freeze({
__proto__: null,
default: dep
default: dep,
});

console.log(dep$1.default);
Expand Down Expand Up @@ -245,7 +281,7 @@ For these situations, you can change Rollup's behaviour either globally or per m
enumerable: true,
get: function () {
return n[k];
}
},
}
);
});
Expand Down Expand Up @@ -321,9 +357,9 @@ export default {
output: {
file: 'bundle.js',
format: 'iife',
name: 'MyModule'
name: 'MyModule',
},
plugins: [resolve(), commonjs()]
plugins: [resolve(), commonjs()],
};
```

Expand All @@ -333,7 +369,7 @@ Symlinks are common in monorepos and are also created by the `npm link` command.

```js
commonjs({
include: /node_modules/
include: /node_modules/,
});
```

Expand All @@ -356,11 +392,11 @@ function cjsDetectionPlugin() {
moduleParsed({
id,
meta: {
commonjs: { isCommonJS }
}
commonjs: { isCommonJS },
},
}) {
console.log(`File ${id} is CommonJS: ${isCommonJS}`);
}
},
};
}
```
Expand Down
37 changes: 23 additions & 14 deletions packages/commonjs/src/generate-exports.js
Expand Up @@ -21,7 +21,7 @@ export function rewriteExportsAndGetExportsBlock(
code,
uses,
HELPERS_NAME,
nodeDefaultImport
defaultIsModuleExports
) {
const namedExportDeclarations = [`export { ${moduleName} as __moduleExports };`];
const moduleExportsPropertyAssignments = [];
Expand Down Expand Up @@ -76,20 +76,29 @@ export function rewriteExportsAndGetExportsBlock(

// Generate default export
const defaultExport = [];
if (isRestorableCompiledEsm) {
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
} else if (
(wrapped || deconflictedDefaultExportName) &&
(defineCompiledEsmExpressions.length > 0 ||
(!nodeDefaultImport && code.indexOf('__esModule') >= 0))
) {
// eslint-disable-next-line no-param-reassign
uses.commonjsHelpers = true;
defaultExport.push(
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
);
} else {
if (defaultIsModuleExports === 'auto') {
if (isRestorableCompiledEsm) {
defaultExport.push(`export default ${deconflictedDefaultExportName || moduleName};`);
} else if (
(wrapped || deconflictedDefaultExportName) &&
(defineCompiledEsmExpressions.length > 0 || code.includes('__esModule'))
) {
// eslint-disable-next-line no-param-reassign
uses.commonjsHelpers = true;
defaultExport.push(
`export default /*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${moduleName});`
);
} else {
defaultExport.push(`export default ${moduleName};`);
}
} else if (defaultIsModuleExports === true) {
defaultExport.push(`export default ${moduleName};`);
} else if (defaultIsModuleExports === false) {
if (deconflictedDefaultExportName) {
defaultExport.push(`export default ${deconflictedDefaultExportName};`);
} else {
defaultExport.push(`export default ${moduleName}.default;`);
}
}

return `\n\n${defaultExport
Expand Down
4 changes: 3 additions & 1 deletion packages/commonjs/src/index.js
Expand Up @@ -59,6 +59,8 @@ 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 { dynamicRequireModuleSet, dynamicRequireModuleDirPaths } = getDynamicRequirePaths(
options.dynamicRequireTargets
Expand Down Expand Up @@ -146,7 +148,7 @@ export default function commonjs(options = {}) {
disableWrap,
commonDir,
ast,
options.nodeDefaultImport
defaultIsModuleExports
);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/commonjs/src/transform-commonjs.js
Expand Up @@ -54,7 +54,7 @@ export default function transformCommonjs(
disableWrap,
commonDir,
astCache,
nodeDefaultImport
defaultIsModuleExports
) {
const ast = astCache || tryParse(parse, code, id);
const magicString = new MagicString(code);
Expand Down Expand Up @@ -138,7 +138,7 @@ export default function transformCommonjs(
// we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
if (programDepth > 3) {
shouldWrap = true;
} else if (!nodeDefaultImport && exportName === KEY_COMPILED_ESM) {
} else if (exportName === KEY_COMPILED_ESM) {
defineCompiledEsmExpressions.push(parent);
} else if (flattened.keypath === 'module.exports') {
topLevelModuleExportsAssignments.push(node);
Expand Down Expand Up @@ -472,7 +472,7 @@ export default function transformCommonjs(
code,
uses,
HELPERS_NAME,
nodeDefaultImport
defaultIsModuleExports
);

const importBlock = rewriteRequireExpressionsAndGetImportBlock(
Expand Down
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: 'auto'
}
};
@@ -0,0 +1,15 @@
const _default = 2;
const named = 3;

const input = /*#__PURE__*/ Object.defineProperty(
{
default: _default,
named
},
'__esModule',
{ value: true }
);

export default _default;
export { input as __moduleExports };
export { named };
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: 'auto'
}
};
@@ -0,0 +1,11 @@
const _default = 2;
const named = 3;

const input = {
default: _default,
named
};

export default input;
export { input as __moduleExports };
export { named };
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: false
}
};
@@ -0,0 +1,3 @@
exports.__esModule = true;
exports.default = 2;
exports.named = 3;
@@ -0,0 +1,15 @@
const _default = 2;
const named = 3;

const input = /*#__PURE__*/ Object.defineProperty(
{
default: _default,
named
},
'__esModule',
{ value: true }
);

export default _default;
export { input as __moduleExports };
export { named };
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: false
}
};
@@ -0,0 +1,2 @@
exports.default = 2;
exports.named = 3;
@@ -0,0 +1,11 @@
const _default = 2;
const named = 3;

const input = {
default: _default,
named
};

export default _default;
export { input as __moduleExports };
export { named };
@@ -0,0 +1,5 @@
module.exports = {
options: {
defaultIsModuleExports: false
}
};
@@ -0,0 +1 @@
exports.named = 3;
@@ -0,0 +1,9 @@
const named = 3;

const input = {
named
};

export default input.default;
export { input as __moduleExports };
export { named };
@@ -1,5 +1,5 @@
module.exports = {
options: {
nodeDefaultImport: true
defaultIsModuleExports: true
}
};
@@ -0,0 +1,3 @@
exports.__esModule = true;
exports.default = 2;
exports.named = 3;
@@ -0,0 +1,15 @@
const _default = 2;
const named = 3;

const input = /*#__PURE__*/ Object.defineProperty(
{
default: _default,
named
},
'__esModule',
{ value: true }
);

export default input;
export { input as __moduleExports };
export { named };
@@ -1,5 +1,5 @@
module.exports = {
options: {
nodeDefaultImport: true
defaultIsModuleExports: true
}
};
@@ -0,0 +1,2 @@
exports.default = 2;
exports.named = 3;
@@ -0,0 +1,11 @@
const _default = 2;
const named = 3;

const input = {
default: _default,
named
};

export default input;
export { input as __moduleExports };
export { named };

This file was deleted.

0 comments on commit b514add

Please sign in to comment.