-
-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chunk prefetch and preload functionality with EJS templates
- Loading branch information
1 parent
7075fd2
commit 3a406fa
Showing
32 changed files
with
606 additions
and
586 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
crates/rspack_plugin_runtime/src/runtime_module/runtime/auto_public_path.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var scriptUrl; | ||
<% if (script_type == "module") { %> | ||
if (typeof <%- IMPORT_META_NAME %>.url === "string") scriptUrl = <%- IMPORT_META_NAME %>.url | ||
<% } else { %> | ||
if (<%- GLOBAL %>.importScripts) scriptUrl = <%- GLOBAL %>.location + ""; | ||
var document = <%- GLOBAL %>.document; | ||
if (!scriptUrl && document) { | ||
// Technically we could use `document.currentScript instanceof window.HTMLScriptElement`, | ||
// but an attacker could try to inject `<script>HTMLScriptElement = HTMLImageElement</script>` | ||
// and use `<img name="currentScript" src="https://attacker.controlled.server/"></img>` | ||
if (document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT') scriptUrl = document.currentScript.src; | ||
if (!scriptUrl) { | ||
var scripts = document.getElementsByTagName("script"); | ||
if (scripts.length) { | ||
var i = scripts.length - 1; | ||
while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src; | ||
} | ||
} | ||
} | ||
<% } %> | ||
// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration", | ||
// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.', | ||
if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser"); | ||
scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/"); | ||
<%- ASSIGN %> |
6 changes: 6 additions & 0 deletions
6
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_prefetch_preload_function.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<%- RUNTIME_HANDLERS %> = {}; | ||
<%- RUNTIME_FUNCTION %> = <%- basicFunction("chunkId") %> { | ||
Object.keys(<%- RUNTIME_HANDLERS %>).map(function (key) { | ||
<%- RUNTIME_HANDLERS %>[key](chunkId); | ||
}); | ||
} |
6 changes: 0 additions & 6 deletions
6
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_prefetch_preload_function.js
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_prefetch_startup.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%- ON_CHUNKS_LOADED %>(0, <%- GROUP_CHUNK_IDS %>, <%- basicFunction("") %> { | ||
<%- BODY %> | ||
}, 5); |
7 changes: 7 additions & 0 deletions
7
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_prefetch_trigger.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var chunkToChildrenMap = <%- CHUNK_MAP %>; | ||
<%- ENSURE_CHUNK_HANDLERS %>.prefetch = <%- basicFunction("chunkId, promises") %> { | ||
Promise.all(promises).then(<%- basicFunction("") %> { | ||
var chunks = chunkToChildrenMap[chunkId]; | ||
Array.isArray(chunks) && chunks.map(<%- PREFETCH_CHUNK %>); | ||
}); | ||
}; |
7 changes: 0 additions & 7 deletions
7
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_prefetch_trigger.js
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_preload_trigger.ejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var chunkToChildrenMap = <%- CHUNK_MAP %>; | ||
<%- ENSURE_CHUNK_HANDLERS %>.preload = <%- basicFunction("chunkId") %> { | ||
var chunks = chunkToChildrenMap[chunkId]; | ||
Array.isArray(chunks) && chunks.map(<%- PRELOAD_CHUNK %>); | ||
}; |
5 changes: 0 additions & 5 deletions
5
crates/rspack_plugin_runtime/src/runtime_module/runtime/chunk_preload_trigger.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.