From 9bf026f0c34b53e55793b98bdf32bed3bc790a53 Mon Sep 17 00:00:00 2001 From: Miel Truyen Date: Sat, 20 May 2023 23:14:59 +0200 Subject: [PATCH] 0.0.3: fix entryNames of sources files included by html in a subdir (they didnt keep their relative address...) --- src/index.ts | 18 +++++++++- test/multi-entry/fixtures/admin/batman.js | 2 ++ test/multi-entry/fixtures/admin/index.html | 1 + test/multi-entry/snapshots/test.js.md | 37 +++++++++++++++++++-- test/multi-entry/snapshots/test.js.snap | Bin 1427 -> 1713 bytes 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 test/multi-entry/fixtures/admin/batman.js diff --git a/src/index.ts b/src/index.ts index 8885d0e..d79d421 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,8 +61,11 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { if(publicPath){ throw new Error("TODO, do something with the public path or throw it out of the options. this is just to stop typescript complaining")} let filter = createFilter(include, exclude, {}); + + // TODO, we need to clear all these properly at sme point to avoid odd bugs in watch mode let htmlModules = new Map();// todo clean this per new build? let virtualSources = new Map(); + let addedEntries = new Map(); const pluginName = 'html2'; return { @@ -143,6 +146,11 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { if(source){ virtualSources.set(sourceId, source); } + let entryName: string|undefined = undefined; + if(type==='entryChunk'){ + entryName= posix.join(posix.dirname(htmlModule.name),sourceId); + entryName = entryName.slice(0,-(posix.extname(entryName).length)); // Cut off the extension (TODO, is this wise?) + } const resolved = await this.resolve(sourceId, id, { isEntry: type==='entryChunk', @@ -152,7 +160,9 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { } const selfInfo = this.getModuleInfo(id); - const importName = (source && selfInfo?.meta[pluginName].name) ? makeInlineId(selfInfo?.meta[pluginName].name, node, extname(sourceId)) : undefined; + const importName = (source && selfInfo?.meta[pluginName].name) + ? makeInlineId(selfInfo?.meta[pluginName].name, node, extname(sourceId)) + : entryName; const htmlImport: HtmlImport = { id: sourceId, @@ -171,6 +181,9 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { placeholder: `html-import-${crypto.randomBytes(32).toString('base64')}`, index: htmlImports.length, } + // if(entryName){ + // addedEntries.set(resolved.id, entryName);// (we could do this using meta?) + // } htmlImports.push(htmlImport); return htmlImport.placeholder; } @@ -224,12 +237,15 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { ...options, entryFileNames: (chunkInfo)=>{ const htmlModule = chunkInfo.facadeModuleId ? htmlModules.get(chunkInfo.facadeModuleId!) : null; + const addedEntry = chunkInfo.facadeModuleId ? addedEntries.get(chunkInfo.facadeModuleId!) : null; const defaultOption = options.entryFileNames ?? "[name]-[hash].js";// This default is copied from the docs. TODO: don't like overwrite it this way, can we remove the need for this or fetch the true default? if(htmlModule){ let fileName = typeof (htmlFileNames) === 'string' ? htmlFileNames : (<(chunkInfo:PreRenderedChunk)=>string>htmlFileNames)(chunkInfo); if(fileName) { return fileName; } + }else if(addedEntry){ + return addedEntry; } return typeof (defaultOption) === 'string' ? defaultOption : (<(chunkInfo:PreRenderedChunk)=>string>defaultOption)(chunkInfo); }, diff --git a/test/multi-entry/fixtures/admin/batman.js b/test/multi-entry/fixtures/admin/batman.js new file mode 100644 index 0000000..413d5d3 --- /dev/null +++ b/test/multi-entry/fixtures/admin/batman.js @@ -0,0 +1,2 @@ +export const b = ()=>'batman'; +console.log(b()); diff --git a/test/multi-entry/fixtures/admin/index.html b/test/multi-entry/fixtures/admin/index.html index 79d7fea..098235e 100644 --- a/test/multi-entry/fixtures/admin/index.html +++ b/test/multi-entry/fixtures/admin/index.html @@ -8,5 +8,6 @@ import {adminDeps} from "../app/admin-deps.js"; bootstrap(document.getElementById('root'), adminDeps()); + diff --git a/test/multi-entry/snapshots/test.js.md b/test/multi-entry/snapshots/test.js.md index e2a246c..d84adc4 100644 --- a/test/multi-entry/snapshots/test.js.md +++ b/test/multi-entry/snapshots/test.js.md @@ -9,6 +9,36 @@ Generated by [AVA](https://avajs.dev). > Snapshot 1 [ + { + code: `const b = ()=>'batman';␊ + console.log(b());␊ + ␊ + export { b };␊ + //# sourceMappingURL=batman-c7fa228c.js.map␊ + `, + fileName: 'admin/batman-c7fa228c.js', + map: SourceMap { + file: 'batman-c7fa228c.js', + mappings: 'AAAY,MAAC,CAAC,GAAG,IAAI,SAAS;AAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;;;;', + names: [], + sources: [ + '../../admin/batman.js', + ], + sourcesContent: [ + `export const b = ()=>'batman';␊ + console.log(b());␊ + `, + ], + version: 3, + }, + source: undefined, + }, + { + code: undefined, + fileName: 'admin/batman-c7fa228c.js.map', + map: undefined, + source: '{"version":3,"file":"batman-c7fa228c.js","sources":["../../admin/batman.js"],"sourcesContent":["export const b = ()=>\'batman\';\\nconsole.log(b());\\n"],"names":[],"mappings":"AAAY,MAAC,CAAC,GAAG,IAAI,SAAS;AAC9B,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;;;;"}', + }, { code: undefined, fileName: 'admin/index.html', @@ -24,17 +54,18 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ bootstrap(document.getElementById('root'), adminDeps());␊ - //# sourceMappingURL=index.html.body.script.js-15dfaff3.js.map␊ + //# sourceMappingURL=index.html.body.script0.js-15dfaff3.js.map␊ ␊ + ␊ ␊ ␊ `, }, { code: undefined, - fileName: 'admin/index.html.body.script.js-15dfaff3.js.map', + fileName: 'admin/index.html.body.script0.js-15dfaff3.js.map', map: undefined, - source: '{"version":3,"file":"index.html.body.script.js-15dfaff3.js","sources":["../../app/admin-deps.js","../../admin/index.html.body.script.js"],"sourcesContent":["export function adminDeps(){\\n return \\"robin!\\";\\n}\\n","\\n import {bootstrap} from \\"../app/app.js\\"\\n import {adminDeps} from \\"../app/admin-deps.js\\";\\n bootstrap(document.getElementById(\'root\'), adminDeps());\\n "],"names":[],"mappings":";;AAAO,SAAS,SAAS,EAAE;AAC3B,IAAI,OAAO,QAAQ,CAAC;AACpB;;ACCY,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC"}', + source: '{"version":3,"file":"index.html.body.script0.js-15dfaff3.js","sources":["../../app/admin-deps.js","../../admin/index.html.body.script0.js"],"sourcesContent":["export function adminDeps(){\\n return \\"robin!\\";\\n}\\n","\\n import {bootstrap} from \\"../app/app.js\\"\\n import {adminDeps} from \\"../app/admin-deps.js\\";\\n bootstrap(document.getElementById(\'root\'), adminDeps());\\n "],"names":[],"mappings":";;AAAO,SAAS,SAAS,EAAE;AAC3B,IAAI,OAAO,QAAQ,CAAC;AACpB;;ACCY,SAAS,CAAC,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC"}', }, { code: `const bootstrap = (el,deps = [])=>{␊ diff --git a/test/multi-entry/snapshots/test.js.snap b/test/multi-entry/snapshots/test.js.snap index 010356908959422d65322175356b8ae3e55ee338..d43ca7961b06c5cb775af484acf960b0856d3f35 100644 GIT binary patch literal 1713 zcmV;i22S}wRzVg3?yj4L$P$$hCj=4)4yYW80C7P=J#g;cn|O)V3J8pFmc{A#chUZQ{&^~ zh+#noVn!I|Q&%BSKhS^1Ax9V>V-8uSXse=@F0!;=uakUTzm}+H{<@V{MS~4kFjQ0ONx`4KXYlYKHj_$E!%CNgFPxrDVAD)_86Rp;7RD2@#C)(j|5z|t1ja!`0adc8{ zkqb}4f5&H5)PqWo|Ch=`>~K7FOgEXX?7mm6r<8_KjMS zsTel4w+&L_R9$X>L(7WloejnQ#5cB*Gyf-Y3fYFn3 znq)&{U}vE(62r=5cbu6_VmC|;g9%kddxk5rO_uevW@KGzsNnwmHDa&S2_=`eSLIBa zTAa@2{Q9yz4s2&v^K|)ncUiMw+05JtxhB?X^X)^tRB#8wn_!tzDJqr&yVI?J1%=Lz zgAb@z4jm`6!8qA#MF)+eg;?Za?r-AA18x}ZA)k@KSuT_R==avVYc7b4D?#%Gme7hU zfrE6=gp82m-0}9O9nfCTwIW&I+GZODm?88K^Ck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