Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce489a1

Browse files
danielgindilukastaegert
authored andcommitted
chore(commonjs): Unwrapped wrapper helper functions
1 parent fa817d5 commit ce489a1

File tree

9 files changed

+66
-79
lines changed

9 files changed

+66
-79
lines changed

β€Žpackages/commonjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@rollup/plugin-node-resolve": "^8.4.0",
6464
"locate-character": "^2.0.5",
6565
"require-relative": "^0.8.7",
66-
"rollup": "^2.38.3",
66+
"rollup": "^2.39.0",
6767
"shx": "^0.3.2",
6868
"source-map": "^0.7.3",
6969
"source-map-support": "^0.5.19",

β€Žpackages/commonjs/src/dynamic-packages-manager.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { existsSync, readFileSync } from 'fs';
22
import { join } from 'path';
33

4-
import {
5-
DYNAMIC_PACKAGES_ID,
6-
DYNAMIC_REGISTER_SUFFIX,
7-
HELPERS_ID,
8-
isWrappedId,
9-
unwrapId,
10-
wrapId
11-
} from './helpers';
4+
import { DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX, HELPERS_ID, wrapId } from './helpers';
125
import { getVirtualPathForDynamicRequirePath, normalizePathSlashes } from './utils';
136

147
export function getPackageEntryPoint(dirPath) {
@@ -47,28 +40,18 @@ export function getDynamicPackagesEntryIntro(
4740
) {
4841
let dynamicImports = Array.from(
4942
dynamicRequireModuleSet,
50-
(dynamicId) => `require(${JSON.stringify(wrapModuleRegisterProxy(dynamicId))});`
43+
(dynamicId) => `require(${JSON.stringify(wrapId(dynamicId, DYNAMIC_REGISTER_SUFFIX))});`
5144
).join('\n');
5245

5346
if (dynamicRequireModuleDirPaths.length) {
54-
dynamicImports += `require(${JSON.stringify(wrapModuleRegisterProxy(DYNAMIC_PACKAGES_ID))});`;
47+
dynamicImports += `require(${JSON.stringify(
48+
wrapId(DYNAMIC_PACKAGES_ID, DYNAMIC_REGISTER_SUFFIX)
49+
)});`;
5550
}
5651

5752
return dynamicImports;
5853
}
5954

60-
export function wrapModuleRegisterProxy(id) {
61-
return wrapId(id, DYNAMIC_REGISTER_SUFFIX);
62-
}
63-
64-
export function unwrapModuleRegisterProxy(id) {
65-
return unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
66-
}
67-
68-
export function isModuleRegisterProxy(id) {
69-
return isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
70-
}
71-
7255
export function isDynamicModuleImport(id, dynamicRequireModuleSet) {
7356
const normalizedPath = normalizePathSlashes(id);
7457
return dynamicRequireModuleSet.has(normalizedPath) && !normalizedPath.endsWith('.json');

β€Žpackages/commonjs/src/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import analyzeTopLevelStatements from './analyze-top-level-statements';
1010
import {
1111
getDynamicPackagesEntryIntro,
1212
getDynamicPackagesModule,
13-
isDynamicModuleImport,
14-
isModuleRegisterProxy,
15-
unwrapModuleRegisterProxy
13+
isDynamicModuleImport
1614
} from './dynamic-packages-manager';
1715
import getDynamicRequirePaths from './dynamic-require-paths';
1816
import {
1917
DYNAMIC_JSON_PREFIX,
2018
DYNAMIC_PACKAGES_ID,
19+
DYNAMIC_REGISTER_SUFFIX,
2120
EXPORTS_SUFFIX,
2221
EXTERNAL_SUFFIX,
2322
getHelpersModule,
@@ -123,12 +122,11 @@ export default function commonjs(options = {}) {
123122
return { meta: { commonjs: { isCommonJS: false } } };
124123
}
125124

126-
let disableWrap = false;
127-
128125
// avoid wrapping as this is a commonjsRegister call
129-
if (isModuleRegisterProxy(id)) {
130-
disableWrap = true;
131-
id = unwrapModuleRegisterProxy(id);
126+
const disableWrap = isWrappedId(id, DYNAMIC_REGISTER_SUFFIX);
127+
if (disableWrap) {
128+
// eslint-disable-next-line no-param-reassign
129+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
132130
}
133131

134132
return transformCommonjs(
@@ -224,9 +222,9 @@ export default function commonjs(options = {}) {
224222
return `export default require(${JSON.stringify(normalizePathSlashes(id))});`;
225223
}
226224

227-
if (isModuleRegisterProxy(id)) {
225+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
228226
return getDynamicRequireProxy(
229-
normalizePathSlashes(unwrapModuleRegisterProxy(id)),
227+
normalizePathSlashes(unwrapId(id, DYNAMIC_REGISTER_SUFFIX)),
230228
commonDir
231229
);
232230
}
@@ -247,8 +245,8 @@ export default function commonjs(options = {}) {
247245
transform(code, rawId) {
248246
let id = rawId;
249247

250-
if (isModuleRegisterProxy(id)) {
251-
id = unwrapModuleRegisterProxy(id);
248+
if (isWrappedId(id, DYNAMIC_REGISTER_SUFFIX)) {
249+
id = unwrapId(id, DYNAMIC_REGISTER_SUFFIX);
252250
}
253251

254252
const extName = extname(id);

β€Žpackages/commonjs/src/resolve-id.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { dirname, resolve, sep } from 'path';
66
import {
77
DYNAMIC_JSON_PREFIX,
88
DYNAMIC_PACKAGES_ID,
9+
DYNAMIC_REGISTER_SUFFIX,
910
EXPORTS_SUFFIX,
1011
EXTERNAL_SUFFIX,
1112
HELPERS_ID,
@@ -16,11 +17,6 @@ import {
1617
unwrapId,
1718
wrapId
1819
} from './helpers';
19-
import {
20-
isModuleRegisterProxy,
21-
unwrapModuleRegisterProxy,
22-
wrapModuleRegisterProxy
23-
} from './dynamic-packages-manager';
2420

2521
function getCandidatesForExtension(resolved, extension) {
2622
return [resolved + extension, `${resolved}${sep}index${extension}`];
@@ -54,14 +50,15 @@ export default function getResolveId(extensions) {
5450
}
5551

5652
return function resolveId(importee, rawImporter) {
57-
const importer =
58-
rawImporter && isModuleRegisterProxy(rawImporter)
59-
? unwrapModuleRegisterProxy(rawImporter)
60-
: rawImporter;
61-
6253
if (isWrappedId(importee, MODULE_SUFFIX) || isWrappedId(importee, EXPORTS_SUFFIX)) {
6354
return importee;
6455
}
56+
57+
const importer =
58+
rawImporter && isWrappedId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
59+
? unwrapId(rawImporter, DYNAMIC_REGISTER_SUFFIX)
60+
: rawImporter;
61+
6562
// Except for exports, proxies are only importing resolved ids,
6663
// no need to resolve again
6764
if (importer && isWrappedId(importer, PROXY_SUFFIX)) {
@@ -77,9 +74,9 @@ export default function getResolveId(extensions) {
7774
} else if (isRequiredModule) {
7875
importee = unwrapId(importee, REQUIRE_SUFFIX);
7976

80-
isModuleRegistration = isModuleRegisterProxy(importee);
77+
isModuleRegistration = isWrappedId(importee, DYNAMIC_REGISTER_SUFFIX);
8178
if (isModuleRegistration) {
82-
importee = unwrapModuleRegisterProxy(importee);
79+
importee = unwrapId(importee, DYNAMIC_REGISTER_SUFFIX);
8380
}
8481
}
8582

@@ -106,7 +103,7 @@ export default function getResolveId(extensions) {
106103
resolved.id = wrapId(resolved.id, resolved.external ? EXTERNAL_SUFFIX : PROXY_SUFFIX);
107104
resolved.external = false;
108105
} else if (resolved && isModuleRegistration) {
109-
resolved.id = wrapModuleRegisterProxy(resolved.id);
106+
resolved.id = wrapId(resolved.id, DYNAMIC_REGISTER_SUFFIX);
110107
} else if (!resolved && (isProxyModule || isRequiredModule)) {
111108
return { id: wrapId(importee, EXTERNAL_SUFFIX), external: false };
112109
}

β€Žpackages/commonjs/src/transform-commonjs.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ import {
2626
isRequireStatement,
2727
isStaticRequireStatement
2828
} from './generate-imports';
29-
import { DYNAMIC_JSON_PREFIX } from './helpers';
29+
import {
30+
DYNAMIC_JSON_PREFIX,
31+
DYNAMIC_REGISTER_SUFFIX,
32+
isWrappedId,
33+
unwrapId,
34+
wrapId
35+
} from './helpers';
3036
import { tryParse } from './parse';
3137
import { deconflict, getName, getVirtualPathForDynamicRequirePath } from './utils';
32-
import {
33-
isModuleRegisterProxy,
34-
unwrapModuleRegisterProxy,
35-
wrapModuleRegisterProxy
36-
} from './dynamic-packages-manager';
3738

3839
const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
3940

@@ -230,13 +231,13 @@ export default function transformCommonjs(
230231
}
231232

232233
let sourceId = getRequireStringArg(node);
233-
const isDynamicRegister = isModuleRegisterProxy(sourceId);
234+
const isDynamicRegister = isWrappedId(sourceId, DYNAMIC_REGISTER_SUFFIX);
234235
if (isDynamicRegister) {
235-
sourceId = unwrapModuleRegisterProxy(sourceId);
236+
sourceId = unwrapId(sourceId, DYNAMIC_REGISTER_SUFFIX);
236237
if (sourceId.endsWith('.json')) {
237238
sourceId = DYNAMIC_JSON_PREFIX + sourceId;
238239
}
239-
dynamicRegisterSources.add(wrapModuleRegisterProxy(sourceId));
240+
dynamicRegisterSources.add(wrapId(sourceId, DYNAMIC_REGISTER_SUFFIX));
240241
} else {
241242
if (
242243
!sourceId.endsWith('.json') &&
@@ -332,7 +333,6 @@ export default function transformCommonjs(
332333
storeName: true
333334
});
334335
}
335-
336336
usesDynamicRequire = true;
337337
return;
338338
case 'module':
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var input = /*#__PURE__*/Object.defineProperty({
1+
import * as commonjsHelpers from "_commonjsHelpers.js";
2+
import { __exports as input } from "\u0000fixtures/form/compiled-esm-define-exports-empty/input.js?commonjs-exports"
23

3-
}, '__esModule', {value: true});
4+
Object.defineProperty(input, "__esModule", { value: true });
45

5-
export default input;
6-
export { input as __moduleExports };
6+
export { input as __moduleExports, input as default };

β€Žpackages/commonjs/test/snapshots/function.js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6371,7 +6371,7 @@ Generated by [AVA](https://avajs.dev).
63716371
␊
63726372
/* eslint-disable */␊
63736373
␊
6374-
var main = b ;␊
6374+
var main = b ;␊
63756375
␊
63766376
module.exports = main;␊
63776377
`,
Binary file not shown.

β€Žpnpm-lock.yaml

Lines changed: 25 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)