diff --git a/dist/index.mjs b/dist/index.mjs index 5c3f6291168987c56b816428080e6f1fe9de7107..abaf6290fe9454ae036a81eacbe7dc3be2fdfbc3 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -499,16 +499,43 @@ ${sourceMap} }), mergeMap(async ({ target, code, deps }) => { await lexer.init; - const [imports] = lexer.parse(code, fileName); + const [imports, exports] = lexer.parse(code, fileName); const depSet = new Set(deps); const magic = new MagicString(code); - for (const i of imports) + for (const i of imports) { if (i.n) { depSet.add(i.n); const fileName2 = getFileName({ type: "module", id: i.n }); const fullImport = code.substring(i.s, i.e); - magic.overwrite(i.s, i.e, fullImport.replace(i.n, `/${fileName2}`)); + const hmrTimestamp = fullImport.match(/\bt=\d{13}&?\b/); + magic.overwrite( + i.s, + i.e, + fullImport.replace( + i.n, + `/${fileName2}${hmrTimestamp ? `?${hmrTimestamp[0]}` : ""}` + ) + ); + } + } + for (const e of exports) { + if (e.n === "default") { + const regex = /\s+['"](.*)['"]/y; + regex.lastIndex = e.e; + const fullExport = regex.exec(code)?.[1]; + if (!fullExport) + continue; + const start = regex.lastIndex - fullExport.length - 1; + const end = regex.lastIndex - 1; + if (fullExport.startsWith("/node_modules")) { + magic.overwrite( + start, + end, + `http://localhost:5173${fullExport}` + ); + } } + } return { target, source: magic.toString(), deps: [...depSet] }; }) ); @@ -1229,10 +1256,14 @@ const pluginHMR = () => { handleHotUpdate({ modules, server }) { const { root } = server.config; const relFiles = /* @__PURE__ */ new Set(); - for (const m of modules) + function getRelFile(file) { + return file.startsWith(root) ? file.slice(server.config.root.length) : file; + } + for (const m of modules) { if (m.id?.startsWith(root)) { relFiles.add(m.id.slice(server.config.root.length)); } + } if (inputManifestFiles.background.length) { const background = prefix$1("/", inputManifestFiles.background[0]); if (relFiles.has(background) || modules.some(isImporter(join(server.config.root, background)))) { @@ -1244,7 +1275,14 @@ const pluginHMR = () => { for (const [key, script] of contentScripts) if (key === script.id) { if (relFiles.has(script.id) || modules.some(isImporter(join(server.config.root, script.id)))) { - relFiles.forEach((relFile) => update(relFile)); + modules.filter((mod) => mod.id?.startsWith(root)).forEach((mod) => { + update(getRelFile(mod.id)); + if (mod.file?.endsWith(".scss")) { + mod.importers.forEach((imp) => { + update(getRelFile(imp.id)); + }); + } + }); } } } @@ -1882,7 +1920,7 @@ const pluginWebAccessibleResources = () => { if (contentScripts.size > 0) { const viteManifest = parseJsonAsset( bundle, - "manifest.json" + ".vite/manifest.json" ); const viteFiles = /* @__PURE__ */ new Map(); for (const [, file] of Object.entries(viteManifest)) diff --git a/package.json b/package.json index e0c47ae66ff399ad3a78abf38d8d93d1f038c55d..f84eb09ffbb5c41094935dd06e04ffe831e2d05a 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "connect-injector": "^0.4.4", "convert-source-map": "^1.7.0", "debug": "^4.3.3", - "es-module-lexer": "^0.10.0", + "es-module-lexer": "^1.4.1", "fast-glob": "^3.2.11", "fs-extra": "^10.0.1", "jsesc": "^3.0.2",