Skip to content

Commit

Permalink
improvement for async module runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Apr 17, 2024
1 parent cf914ee commit 47b73f5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 41 deletions.
26 changes: 16 additions & 10 deletions crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts
Expand Up @@ -147,11 +147,7 @@ function exportValue(module: Module, value: any) {
}

function exportNamespace(module: Module, namespace: any) {
if (isAsyncModuleExt(module.exports)) {
module.exports[turbopackExports] = namespace;
} else {
module.exports = module.namespaceObject = namespace;
}
module.exports = module.namespaceObject = namespace;
}

function createGetter(obj: Record<string | symbol, any>, key: string | symbol) {
Expand Down Expand Up @@ -397,21 +393,31 @@ function asyncModule(

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

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

const { resolve, reject, promise: rawPromise } = createPromise<Exports>();

const promise: AsyncModulePromise = Object.assign(rawPromise, {
[turbopackExports]: exports,
[turbopackExports]: module.exports,
[turbopackQueues]: (fn) => {
queue && fn(queue);
depQueues.forEach(fn);
promise["catch"](() => {});
},
} satisfies AsyncModuleExt);

module.exports = module.namespaceObject = promise;
const attributes: PropertyDescriptor = {
get(): any {
return promise;
},
set(v: any) {
// Calling `esmExport` leads to this.
if (v !== promise) {
promise[turbopackExports] = v;
}
},
};

Object.defineProperty(module, "exports", attributes);
Object.defineProperty(module, "namespaceObject", attributes);

function handleAsyncDependencies(deps: Dep[]) {
const currentDeps = wrapDeps(deps);
Expand Down
6 changes: 6 additions & 0 deletions crates/turbopack-tests/tests/execution.rs
Expand Up @@ -155,6 +155,12 @@ fn get_messages(js_results: JsResult) -> Vec<String> {
async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsResult> {
register();

// Clean up old output files.
let output_path = resource.join("output");
if output_path.exists() {
std::fs::remove_dir_all(&output_path)?;
}

let tt = TurboTasks::new(MemoryBackend::default());
tt.run_once(async move {
let resource_str = resource.to_str().unwrap();
Expand Down

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 47b73f5

Please sign in to comment.