Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions crates/pack-core/js/src/umd/runtime-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@ function loadChunkByUrl(
}
browserContextPrototype.L = loadChunkByUrl;

const loadedScripts = new Map<string, Promise<void>>();

/**
* Load an external script by creating a <script> tag.
* This is used for script externals that need to be loaded from CDN or other external sources.
*/
function loadScript(
this: TurbopackBrowserBaseContext<Module>,
scriptUrl: string,
): Promise<void> {
// Return cached promise if script is already loading or loaded
let promise = loadedScripts.get(scriptUrl);
if (promise) {
return promise;
}

promise = new Promise<void>((resolve, reject) => {
const script = document.createElement("script");
script.src = scriptUrl;
script.onload = () => resolve();
script.onerror = () =>
reject(new Error(`Failed to load script: ${scriptUrl}`));
Comment thread
fireairforce marked this conversation as resolved.
Resolved
document.head.appendChild(script);
});

loadedScripts.set(scriptUrl, promise);
return promise;
}
browserContextPrototype.S = loadScript;

// Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrlInternal(
sourceType: SourceType,
Expand Down
2 changes: 2 additions & 0 deletions crates/pack-core/js/src/umd/runtime-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type DynamicExport = (
) => void;

type LoadChunkByUrl = (chunkUrl: ChunkUrl) => Promise<any> | undefined;
type LoadScript = (scriptUrl: string) => Promise<void>;

type ModuleCache<M> = Record<ModuleId, M>;
// TODO properly type values here
Expand Down Expand Up @@ -119,6 +120,7 @@ interface TurbopackBaseContext<M> {
n: ExportNamespace;
m: Module;
L: LoadChunkByUrl;
S: LoadScript;
c: ModuleCache<M>;
M: ModuleFactories;
g: typeof globalThis;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"config": {
"entry": [
{
"import": "input/index.ts",
"name": "main"
}
],
"externals": {
"lodash": "script _@https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const func = async () => {
// @ts-ignore
const _ = await import('lodash');
console.log(Object.keys(_.default.omit({ a: 1 }, 'a')).length === 0);
};
func();
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([typeof document === "object" ? document.currentScript : undefined,
20, ((__turbopack_context__) => {
"use strict";

return __turbopack_context__.a(async (__turbopack_handle_async_dependencies__, __turbopack_async_result__) => { try {

const mod = await (async () => {
if (typeof globalThis["_"] !== 'undefined') {
return globalThis["_"];
}
await __turbopack_context__.S("https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js");
if (typeof globalThis["_"] !== 'undefined') {
return globalThis["_"];
}
const error = new Error('Loading script failed.\n(missing: "https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js")');
error.name = 'ScriptExternalLoadError';
error.type = 'missing';
error.request = "https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js";
throw error;
})();

__turbopack_context__.v(mod);
__turbopack_async_result__();
} catch(e) { __turbopack_async_result__(e); } }, true);}),
]);

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
typeof document === "object" ? document.currentScript : undefined,
{"otherChunks":["_root-of-the-server___fa01a871.js"],"runtimeModuleIds":[6]}
]);
// Dummy runtime

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

return __turbopack_context__.a(async (__turbopack_handle_async_dependencies__, __turbopack_async_result__) => { try {

let mod;
try {
await __turbopack_context__.L("https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js");
if (typeof global["_"] === 'undefined') {
throw new Error('Variable "_" is not available on global object after loading "https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js"');
const mod = await (async () => {
if (typeof globalThis["_"] !== 'undefined') {
return globalThis["_"];
}
mod = global["_"];
} catch (error) {
throw new Error('Failed to load external URL module "_@https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js": ' + (error.message || error));
}
await __turbopack_context__.S("https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js");
if (typeof globalThis["_"] !== 'undefined') {
return globalThis["_"];
}
const error = new Error('Loading script failed.\n(missing: "https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js")');
error.name = 'ScriptExternalLoadError';
error.type = 'missing';
error.request = "https://gw.alipayobjects.com/os/lib/lodash/4.17.21/lodash.min.js";
throw error;
})();

__turbopack_context__.v(mod);
__turbopack_async_result__();
Expand Down
21 changes: 21 additions & 0 deletions crates/pack-tests/tests/snapshot/public_path/basic/output/main.js

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.

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.

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

Large diffs are not rendered by default.

Loading
Loading