Fix duplicate module loading: exclude direct script tags from import map#101
Fix duplicate module loading: exclude direct script tags from import map#101hihumanzone with Copilot wants to merge 2 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…gs from import map When an HTML file contains <script type="module" src="main.js">, the src is rewritten to a blob URL. Previously, the same file was also included in the generated import map, causing it to load twice. Now, files that are directly loaded via script tags are tracked and excluded from the import map. Co-authored-by: hihumanzone <101509991+hihumanzone@users.noreply.github.com>
|
I have a conceptual question. If a file is loaded directly via the rewritten If it cannot be imported by other modules, then keeping both the rewritten script tag and the generated import map might actually serve a purpose — is that correct? |
Great question — the answer is: no, the script-tag version and the import map version were never the same module instance, so removing the import map entry for the directly-loaded file does not break cross-module imports. Here's why. Every call to
Browsers cache ES modules by URL. Because X ≠ Y, they are always two entirely separate module instances. Before the fix:
So the import map entry for a directly-loaded script never enabled other modules to reach that same instance. The only thing it was doing was loading the file a second time. Removing it from the import map (this fix) is therefore safe: other modules that import |
Modules referenced via
<script type="module" src="main.js">are loaded twice — once from the rewritten script tag (blob URL) and again from the generated import map. Functionally harmless but wasteful.Fix: Track which modules are already loaded as direct
<script>entries duringreplaceScriptTags, then exclude them from the import map.replaceScriptTags— accepts optionaldirectModuleScriptsSet, records each resolved module path when rewriting a script tagbuildModuleImportMap/buildModuleImportMapScript— accepts optionalexcludePathsSet, skips matching entriesreplaceAssetReferences,injectConsoleScript— thread the Set through the call chaingenerateFullDocumentPreview,generateMultiFilePreview— create the Set and pass it through both asset replacement and import map generationreplaceDownloadLinks— creates a per-sub-page Set for linked HTML filesAll new parameters are optional with no behavioral change when omitted.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.