Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(commonjs): Add support for circular dependencies #658

Merged
merged 22 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5ec8891
feat(commonjs): Basic support for circular dependencies
lukastaegert Nov 24, 2020
0e41148
refactor(commonjs): Inline __esModule handling into export generation
lukastaegert Nov 24, 2020
a81058d
feat(commonjs): Generate simpler code when replacing module.exports
lukastaegert Nov 25, 2020
a3433fa
feat(commonjs): Handle side-effect free modules with updated Rollup
lukastaegert Jan 5, 2021
861d1ef
feat(commonjs): Generate simplified code when module is not used
lukastaegert Jan 21, 2021
5ac99ff
feat(commonjs): Do not use module when wrapping if not needed
lukastaegert Jan 23, 2021
5976ab3
chore(commonjs): Clean up usage of helpers
lukastaegert Jan 23, 2021
7ae3bab
feat(commonjs): Inline name export assignments
lukastaegert Jan 24, 2021
2d784b8
feat(commonjs): Do not wrap for nested module.exports assignments
lukastaegert Jan 24, 2021
b084933
feat(commonjs): Wrap if accessing exports while reassigning module.ex…
lukastaegert Jan 26, 2021
1bdaddc
chore(commonjs): Add test for module.exports mutations
lukastaegert Jan 26, 2021
a151e8d
feat(commonjs): Do not wrap for double exports assignments
lukastaegert Jan 28, 2021
e3c8f26
fix(commonjs): Deconflict nested module.exports assignments
lukastaegert Jan 28, 2021
ba6ffee
chore(commonjs): Simplify assignment tracking
lukastaegert Jan 29, 2021
1fcae9d
feat(commonjs): Do not wrap for nested named export assignments
lukastaegert Jan 29, 2021
f4f98bb
chore(commonjs): Clean up remaining TODOs
lukastaegert Jan 29, 2021
0a15527
fix(commonjs): Make sure require expressions return snapshots
lukastaegert Feb 1, 2021
fa817d5
fix(commonjs): Snapshot default export
lukastaegert Feb 1, 2021
ce489a1
chore(commonjs): Unwrapped wrapper helper functions
danielgindi Feb 15, 2021
e57b207
Merge branch 'master' into feat/commonjs-circular-dependencies
lukastaegert May 7, 2021
31d6298
chore: update dependencies
shellscape May 7, 2021
733147d
chore: change audit level to high amid unfixable moderate
shellscape May 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"preinstall": "node scripts/disallow-npm.js",
"pub": "node scripts/pub.js",
"publish": "node scripts/publish.js",
"security": "pnpm audit --audit-level=moderate"
"security": "pnpm audit --audit-level=high"
},
"dependencies": {
"conventional-commits-parser": "^3.1.0",
Expand Down
24 changes: 12 additions & 12 deletions packages/commonjs/README.md
Original file line number Diff line number Diff line change
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 @@ -250,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 @@ -281,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 @@ -357,9 +357,9 @@ export default {
output: {
file: 'bundle.js',
format: 'iife',
name: 'MyModule'
name: 'MyModule',
},
plugins: [resolve(), commonjs()]
plugins: [resolve(), commonjs()],
};
```

Expand All @@ -369,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 @@ -392,11 +392,11 @@ function cjsDetectionPlugin() {
moduleParsed({
id,
meta: {
commonjs: { isCommonJS }
}
commonjs: { isCommonJS },
},
}) {
console.log(`File ${id} is CommonJS: ${isCommonJS}`);
}
},
};
}
```
Expand Down
4 changes: 2 additions & 2 deletions packages/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"require"
],
"peerDependencies": {
"rollup": "^2.30.0"
"rollup": "^2.38.3"
},
"dependencies": {
"@rollup/pluginutils": "^3.1.0",
Expand All @@ -62,7 +62,7 @@
"@rollup/plugin-node-resolve": "^8.4.0",
"locate-character": "^2.0.5",
"require-relative": "^0.8.7",
"rollup": "^2.30.0",
"rollup": "^2.39.0",
"shx": "^0.3.2",
"source-map": "^0.7.3",
"source-map-support": "^0.5.19",
Expand Down
14 changes: 1 addition & 13 deletions packages/commonjs/src/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ export function isDefineCompiledEsm(node) {
}

function getDefinePropertyCallName(node, targetName) {
const targetNames = targetName.split('.');

const {
callee: { object, property }
} = node;
if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
if (node.arguments.length !== 3) return;

const targetNames = targetName.split('.');
const [target, key, value] = node.arguments;
if (targetNames.length === 1) {
if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
Expand All @@ -105,17 +104,6 @@ function getDefinePropertyCallName(node, targetName) {
return { key: key.value, value: valueProperty.value };
}

export function isLocallyShadowed(name, scope) {
while (scope.parent) {
if (scope.declarations[name]) {
return true;
}
// eslint-disable-next-line no-param-reassign
scope = scope.parent;
}
return false;
}

export function isShorthandProperty(parent) {
return parent && parent.type === 'Property' && parent.shorthand;
}
27 changes: 5 additions & 22 deletions packages/commonjs/src/dynamic-packages-manager.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { existsSync, readFileSync } from 'fs';
import { join } from 'path';

import {
DYNAMIC_PACKAGES_ID,
DYNAMIC_REGISTER_SUFFIX,
HELPERS_ID,
isWrappedId,
unwrapId,
wrapId
} from './helpers';
import { DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX, HELPERS_ID, wrapId } from './helpers';
import { getVirtualPathForDynamicRequirePath, normalizePathSlashes } from './utils';

export function getPackageEntryPoint(dirPath) {
Expand Down Expand Up @@ -47,28 +40,18 @@ export function getDynamicPackagesEntryIntro(
) {
let dynamicImports = Array.from(
dynamicRequireModuleSet,
(dynamicId) => `require(${JSON.stringify(wrapModuleRegisterProxy(dynamicId))});`
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
).join('\n');

if (dynamicRequireModuleDirPaths.length) {
dynamicImports += `require(${JSON.stringify(wrapModuleRegisterProxy(DYNAMIC_PACKAGES_ID))});`;
dynamicImports += `require(${JSON.stringify(
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
)});`;
}

return dynamicImports;
}

export function wrapModuleRegisterProxy(id) {
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
}

export function unwrapModuleRegisterProxy(id) {
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
}

export function isModuleRegisterProxy(id) {
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
}

export function isDynamicModuleImport(id, dynamicRequireModuleSet) {
const normalizedPath = normalizePathSlashes(id);
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');
Expand Down
Loading