diff --git a/package.json b/package.json index 98f550f..f6b23cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-html-entry2", - "version": "0.0.4", + "version": "0.0.5", "description": "Teaches rollup how to deal with HTML, allows to use HTML-files as entry-points.", "license": "MIT", "repository": { diff --git a/src/index.ts b/src/index.ts index d79d421..b43502c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,11 +66,17 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { let htmlModules = new Map();// todo clean this per new build? let virtualSources = new Map(); let addedEntries = new Map(); + let entryNames = new Map(); const pluginName = 'html2'; return { name: pluginName,// TODO: Need a better name, original plugin was just named `html` and might still make sense to use in conjunction with this one + buildStart(options){ + entryNames = new Map(Object.entries(typeof(options.input)==='object'?options.input:{[options.input]:[options.input]}) + .map(([k,v])=>[v,k]) + ); + }, resolveId: { async handler(specifier: string, importer: string | undefined, @@ -146,12 +152,6 @@ 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', }); @@ -160,8 +160,16 @@ export default function html(opts: RollupHtmlOptions = {}): Plugin { } const selfInfo = this.getModuleInfo(id); + + let entryName: string|undefined = undefined; + const parentName = entryNames.get(id)??selfInfo?.meta[pluginName].name; + if(type==='entryChunk'){ + entryName= posix.join(posix.dirname(parentName),sourceId); + entryName = entryName.slice(0,-(posix.extname(entryName).length)); // Cut off the extension (TODO, is this wise?) + } + const importName = (source && selfInfo?.meta[pluginName].name) - ? makeInlineId(selfInfo?.meta[pluginName].name, node, extname(sourceId)) + ? makeInlineId(parentName, node, extname(sourceId)) : entryName; const htmlImport: HtmlImport = { diff --git a/test/multi-entry/snapshots/test.js.md b/test/multi-entry/snapshots/test.js.md index d84adc4..462b5cb 100644 --- a/test/multi-entry/snapshots/test.js.md +++ b/test/multi-entry/snapshots/test.js.md @@ -39,6 +39,12 @@ Generated by [AVA](https://avajs.dev). 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.body.script0.js-15dfaff3.js.map', + map: undefined, + source: '{"version":3,"file":"index.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: undefined, fileName: 'admin/index.html', @@ -54,19 +60,13 @@ Generated by [AVA](https://avajs.dev). }␊ ␊ bootstrap(document.getElementById('root'), adminDeps());␊ - //# sourceMappingURL=index.html.body.script0.js-15dfaff3.js.map␊ + //# sourceMappingURL=index.body.script0.js-15dfaff3.js.map␊ ␊ ␊ ␊ ␊ `, }, - { - code: undefined, - fileName: 'admin/index.html.body.script0.js-15dfaff3.js.map', - map: undefined, - 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 = [])=>{␊ el.innerHtml = \`␊ diff --git a/test/multi-entry/snapshots/test.js.snap b/test/multi-entry/snapshots/test.js.snap index d43ca79..c12e1c9 100644 Binary files a/test/multi-entry/snapshots/test.js.snap and b/test/multi-entry/snapshots/test.js.snap differ diff --git a/test/multi-entry/test.js b/test/multi-entry/test.js index f450631..a465f5d 100644 --- a/test/multi-entry/test.js +++ b/test/multi-entry/test.js @@ -1,4 +1,4 @@ -import {join, dirname} from "node:path"; +import {resolve, join, dirname} from "node:path"; import test from "ava"; import { rollup } from "rollup"; @@ -21,7 +21,7 @@ test.serial('multi-entry', async (t) => { const bundle = await rollup({ input: { ['index']: 'index.html', - ['admin/index']: 'admin/index.html' + ['admin/index']: resolve(__dirname,'fixtures','admin/index.html'), }, plugins: [ html({