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 0103569..d43ca79 100644 Binary files a/test/multi-entry/snapshots/test.js.snap and b/test/multi-entry/snapshots/test.js.snap differ