Skip to content

Commit

Permalink
unsimplify dynamicExport
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 20, 2023
1 parent a4a0e21 commit 5556ebd
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
f: requireContext.bind(null, module),
i: esmImport.bind(null, module),
s: esm.bind(null, module.exports),
j: dynamicExport.bind(null, module.exports),
j: dynamicExport.bind(null, module, module.exports),
v: exportValue.bind(null, module),
n: exportNamespace.bind(null, module),
m: module,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
f: requireContext.bind(null, module),
i: esmImport.bind(null, module),
s: esmExport.bind(null, module, module.exports),
j: dynamicExport.bind(null, module.exports),
j: dynamicExport.bind(null, module, module.exports),
v: exportValue.bind(null, module),
n: exportNamespace.bind(null, module),
m: module,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ function externalRequire(
}
externalRequire.resolve = (
id: string,
options?:
| {
paths?: string[] | undefined;
options?: {
paths?: string[];
}
| undefined
) => {
return require.resolve(id, options);
};
72 changes: 38 additions & 34 deletions crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,50 @@ function esmExport(
esm(exports, getters);
}

function ensureDynamicExports(module: Module, exports: Exports) {
let reexportedObjects = module[REEXPORTED_OBJECTS];

if (!reexportedObjects) {
reexportedObjects = module[REEXPORTED_OBJECTS] = [];
module.exports = module.namespaceObject = new Proxy(exports, {
get(target, prop) {
if (
hasOwnProperty.call(target, prop) ||
prop === "default" ||
prop === "__esModule"
) {
return Reflect.get(target, prop);
}
for (const obj of reexportedObjects!) {
const value = Reflect.get(obj, prop);
if (value !== undefined) return value;
}
return undefined;
},
ownKeys(target) {
const keys = Reflect.ownKeys(target);
for (const obj of reexportedObjects!) {
for (const key of Reflect.ownKeys(obj)) {
if (key !== "default" && !keys.includes(key)) keys.push(key);
}
}
return keys;
},
});
}
}

/**
* Dynamically exports properties from an object
*/
function dynamicExport(
module: Module,
exports: Exports,
object: Record<string, any>
) {
const keys = Reflect.ownKeys(exports);
ensureDynamicExports(module, exports);

for (const key of Reflect.ownKeys(object)) {
if (key !== "default" && !keys.includes(key)) {
defineProp(exports, key, {
get: createGetter(object, key),
enumerable: true,
});
}
}
module[REEXPORTED_OBJECTS]!.push(object);
}

function exportValue(module: Module, value: any) {
Expand Down Expand Up @@ -251,31 +278,6 @@ function isAsyncModuleExt<T extends {}>(obj: T): obj is AsyncModuleExt & T {
return turbopackQueues in obj;
}

function maybeWrapAsyncModulePromise<T, U>(
promise: Promise<T>,
then: (val: T) => U | PromiseLike<U>
): typeof promise extends AsyncModulePromise
? AsyncModulePromise<U>
: Promise<U> {
const newPromise = promise.then(then);

if (isAsyncModuleExt(promise)) {
Object.assign(newPromise, {
get [turbopackExports]() {
return promise[turbopackExports];
},
get [turbopackQueues]() {
return promise[turbopackQueues];
},
get [turbopackError]() {
return promise[turbopackError];
},
} satisfies AsyncModuleExt);
}

return newPromise as any;
}

function createPromise<T>() {
let resolve: (value: T | PromiseLike<T>) => void;
let reject: (reason?: any) => void;
Expand Down Expand Up @@ -371,6 +373,8 @@ function asyncModule(
: undefined;

const depQueues: Set<AsyncQueue> = new Set();

ensureDynamicExports(module, module.exports);
const exports = module.exports;

const { resolve, reject, promise: rawPromise } = createPromise<Exports>();
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ similar = "2.2.0"
turbo-tasks = { workspace = true }
turbo-tasks-fs = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbopack-core = { workspace = true }
turbopack-cli-utils = { workspace = true }
turbopack-core = { workspace = true }

[build-dependencies]
turbo-tasks-build = { workspace = true }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
warning - [analyze] [project]/crates/turbopack-tests/tests/execution/turbopack/async-modules/export-all/input/exports.js unexpected export *
export * used with module [project]/crates/turbopack-tests/tests/execution/turbopack/async-modules/export-all/input/exports.js (ecmascript) which is a CommonJS module with exports only available at runtime
List all export names manually (`export { a, b, c } from "...") or rewrite the module to ESM, to avoid the additional runtime code.`

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

0 comments on commit 5556ebd

Please sign in to comment.