diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4bf7a80..19245e3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,90 +1,2 @@
-# Remove me:
-TODO: This started as a fork of, but is now something different entirely. Changelog is no longer relevant (neither is the [README.md](README.md))
-rollup-plugin-css was used in the initial tests, but it hasnt been update in **7** years. Remove this
-
-# @rollup/plugin-html ChangeLog
-
-## v1.0.2
-
-_2023-01-20_
-
-### Bugfixes
-
-- fix: types should come first in exports [#1403](https://github.com/rollup/plugins/pull/1403)
-
-## v1.0.1
-
-_2022-10-21_
-
-### Updates
-
-- chore: update rollup dependencies ([3038271](https://github.com/rollup/plugins/commit/303827191ede6b2e4eade96c6968ed16a587683f))
-
-## v1.0.0
-
-_2022-10-09_
-
-### Breaking Changes
-
-- fix: prepare for Rollup 3 [#1294](https://github.com/rollup/plugins/pull/1294)
-
-### Bugfixes
-
-- fix: function signature of html updated [#975](https://github.com/rollup/plugins/pull/975)
-
-## v0.2.3
-
-_2021-02-14_
-
-### Bugfixes
-
-- fix: package.json files (#802)
-
-## v0.2.2
-
-_2021-01-29_
-
-### Updates
-
-- chore: run build before publish (ce65c12)
-- chore: upgrade TypeScript (#713)
-- refactor: refactor to typescript (#634)
-- chore: update dependencies (6c8614c)
-
-## v0.2.1
-
-_2021-01-29_
-
-### Updates
-
-- chore: run build before publish (ce65c12)
-- chore: upgrade TypeScript (#713)
-- refactor: refactor to typescript (#634)
-- chore: update dependencies (6c8614c)
-
-## v0.2.0
-
-_2020-05-02_
-
-### Features
-
-- feat: support custom meta element creation (#308)
-
-### Updates
-
-- test: update snapshots for rollup v2 (c09509f)
-
-## v0.1.1
-
-_2020-01-04_
-
-### Updates
-
-- docs: fix tag (#89)
-- test: change tests to serial to avoid weird snapshot conflicts (a492ce7)
-
-## 0.1.0
-
-_2019-11-29_
-
-- First Release
+# 0.0.1
+Initial private release
diff --git a/README.md b/README.md
index 7ad3daa..4fc2035 100644
--- a/README.md
+++ b/README.md
@@ -1,33 +1,41 @@
-# Work-in-progress changelog
-
-
-
-
-[npm]: https://img.shields.io/npm/v/@rollup/plugin-html
-[npm-url]: https://www.npmjs.com/package/@rollup/plugin-html
-[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-html
-[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-html
+[npm]: https://img.shields.io/npm/v/rollup-plugin-html-entry2
+[npm-url]: https://www.npmjs.com/package/rollup-plugin-html-entry2
+[size]: https://packagephobia.now.sh/badge?p=rollup-plugin-html-entry2
+[size-url]: https://packagephobia.now.sh/result?p=rollup-plugin-html-entry2
+[handlebars]: https://www.npmjs.com/package/handlebars
+
[![npm][npm]][npm-url]
[![size][size]][size-url]
[](https://liberamanifesto.com)
-# @rollup/plugin-html
+# rollup-plugin-html-entry2
+| :warning: WARNING |
+|:-------------------------------------------------------------------|
+| **Experimental-stage** plugin. Expect bugs and missing features... |
-π£ A Rollup plugin which creates HTML files to serve Rollup bundles.
-Please see [Supported Output Formats](#supported-output-formats) for information about using this plugin with output formats other than `esm` (`es`), `iife`, and `umd`.
+A(nother) rollup plugin that tries to teach Rollup to start from an HTML entry, and the use of (multiple) HTML files in general.
+The goal is to include assets referenced by the HTML file into the build-process as to copy/inline where appropriate and
+optionally optimize them. Without having to seperatly copy them to the output directory.
+
+When building web-applications a HTML-file is simply the logical entry point into your application. \
+Inspired (and forked) by the original [@rollup/plugin-html](https://www.npmjs.com/package/@rollup/plugin-html),
+this plugin will also allow you to transform the source files by any HTML-templating engine such as [handlebars].
+
+
+Please see [Supported Output Formats](#supported-output-formats) for information about using this plugin with output formats other than `esm` (`es`).
## Requirements
-This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v14.0.0+) and Rollup v1.20.0+.
+This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v18.0.0+) and Rollup v3.?.?+.
## Install
Using npm:
```console
-npm install @rollup/plugin-html --save-dev
+npm install rollup-plugin-html-entry2 --save-dev
```
## Usage
@@ -35,131 +43,85 @@ npm install @rollup/plugin-html --save-dev
Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
```js
-const html = require('@rollup/plugin-html');
+import html from 'rollup-plugin-html-entry2';
-module.exports = {
- input: 'src/index.js',
+export default {
+ input: 'src/index.html',
output: {
dir: 'output',
- format: 'cjs'
},
plugins: [html()]
};
```
+!! To use 'import x from y' syntax you might need to set `"type": "module"` in your `package.json`.
+[Javascript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) are the _preferred_ way of writing modern Javascript.
+Due to the early stage development of this plugin, old-style CommonJS modules are completely ignored for now.
+
Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
-Once run successfully, an HTML file should be written to the bundle output destination.
-
## Options
-### `attributes`
-
-Type: `Object`
-Default: `{ html: { lang: 'en' }, link: null, script: null }`
-
-Specifies additional attributes for `html`, `link`, and `script` elements. For each property, provide an object with key-value pairs that represent an HTML element attribute name and value. By default, the `html` element is rendered with an attribute of `lang="en"`.
-
-_Note: If using the `es` / `esm` output format, `{ type: 'module'}` is automatically added to `attributes.script`._
-
-### `fileName`
-
-Type: `String`
-Default: `'index.html'`
-
-### `meta`
-
-Type: `Array[...object]`
-Default: `[{ charset: 'utf-8' }]`
-
-Specifies attributes used to create `` elements. For each array item, provide an object with key-value pairs that represent `` element attribute names and values.
-
-Specifies the name of the HTML to emit.
-
-### `publicPath`
-
-Type: `String`
-Default: `''`
-
-Specifies a path to prepend to all bundle assets (files) in the HTML output.
-
### `template`
Type: `Function`
-Default: `internal function`
+Default: `undefined`\
Returns: `String`
-Specifies a function that provides the rendered source for the HTML output. The function should be in the form of:
+Specifies a transform to be applied before parsing the HTML, this allows you to transform the sourcefile with a templating engine such as [handlebars] first.
-```js
-const template = ({ attributes, bundle, files, publicPath, title }) => { ... }
-```
+```javascript
+import {rollup} from "rollup";
+import handlebars from "handlebars";
+import html from "rollup-plugin-html-entry2";
-- `attributes`: Corresponds to the `attributes` option passed to the plugin
-- `bundle`: An `Object` containing key-value pairs of [`AssetInfo` or `ChunkInfo`](https://rollupjs.org/guide/en/#generatebundle)
-- `files`: An `Array` of `AssetInfo` or `ChunkInfo` containing any entry (`isEntry: true`) files, and any asset (`isAsset: true`) files in the bundle that will be emitted
-- `publicPath`: Corresponds to the `publicPath` option passed to the plugin
-- `title`: Corresponds to the `title` option passed to the plugin
-
-By default this is handled internally and produces HTML in the following format:
-
-```html
-
-
-
- ${metas}
- ${title}
- ${links}
-
-
- ${scripts}
-
-
-```
-
-Where `${links}` represents all `
-Default: `'Rollup Bundle'`
-
-Specifies the HTML document title.
-
-## Exports
-
-### `makeHtmlAttributes(attributes)`
-
-Parameters: `attributes`, Type: `Object`
-Returns: `String`
-
-Consumes an object with key-value pairs that represent an HTML element attribute name and value. The function returns all pairs as a space-separated string of valid HTML element attributes. e.g.
-
-```js
-const { makeHtmlAttributes } = require('@rollup/plugin-html');
-
-makeHtmlAttributes({ lang: 'en', 'data-batcave': 'secret' });
-// -> 'lang="en" data-batcave="secret"'
+async function build() {
+ await rollup({
+ input: 'index.hbs',
+ plugins: [
+ html({
+ transform(src) {
+ return handlebars.compile(src)({a: 'a'})
+ }
+ })
+ ]
+ });
+}
```
## Supported Output Formats
-By default, this plugin supports the `esm` (`es`), `iife`, and `umd` [output formats](https://rollupjs.org/guide/en/#outputformat), as those are most commonly used as browser bundles. Other formats can be used, but will require using the [`template`](#template) option to specify a custom template function which renders the unique requirements of other formats.
+By default, this plugin supports the `esm` (`es`). Any other format is currently untested as this plugin is in an early state, see [#status](#status)
-### `amd`
+## Status
-Will likely require use of RequireJS semantics, which allows only for a single entry ``.
-## Attribution
+### Not (yet) supported
+- Inline scripts (i.e ``)
+- Plugins importing CSS files
+- CommonJS (cjs) and IIFI output formats. (Is UMD actually ever used?)
+- Overriding which tags to ignore/include
+- Other (various) plugins such as those for HMR etc
+- ...
-This plugin was inspired by and is based upon [mini-html-webpack-plugin](https://github.com/styleguidist/mini-html-webpack-plugin) by Juho VepsΓ€lΓ€inen and Artem Sapegin, with permission.
+# Contibuting
-## Meta
+You can be helpful by testing, proving helpful feedback, expanding the documentation, responding to issues/questions being reported, resolving the many ToDo`s in the code, implementating features...\
+[Get in touch](mailto:rollup-plugin-html-entry2@cerxes.net) or just dive into [the code](https://git.cerxes.net/rollup-apps/plugin-html) or [issues](https://git.cerxes.net/rollup-apps/plugin-html/issues)
-[CONTRIBUTING](/.github/CONTRIBUTING.md)
-
-[LICENSE (MIT)](/LICENSE)
+# Notes
+## git.cerxes.net
+Once publicly released, the intent is to move the GIT-repository to github. Until that day though, it exists privately on this gitea server and corresponding npm-registry [npm.cerxes.net](https://npm.cerxes.net).\
+TODO: change the links once this happens
+## Prior work
+ - [Vite](https://vitejs.dev) seems to have already [done work])(https://github.com/vitejs/vite/blob/main/packages/vite/src/node/plugins/html.ts) to handle HTML in rollup.
+ - [rollup-plugin-html-entry](https://www.npmjs.com/package/rollup-plugin-html-entry) seems to be **dead**. Last version from 2020, there have been many changes to rollup`s plugin capabilities since then
+ - [@rollup/plugin-html](https://www.npmjs.com/package/@rollup/plugin-html) is where this project was originally forked from. Its main focus was to generate an HTML to serve the resulting bundle. Which is different from supporting HTML as entry point since it did not resolve assets used in the HTML. Besides the project setup, not much of the original has been kept...
+ - [@web/rollup-plugin-html](https://www.npmjs.com/package/@web/rollup-plugin-html) a plugin with similar intentions as this one (in active development anno 2023).
diff --git a/package.json b/package.json
index 82c93f7..f6c9247 100644
--- a/package.json
+++ b/package.json
@@ -1,15 +1,15 @@
{
- "name": "@rollup-apps/plugin-html",
+ "name": "rollup-plugin-html-entry2",
"version": "0.0.1",
- "description": "Creates HTML files to serve Rollup bundles",
+ "description": "Teaches rollup how to deal with HTML, allows to use HTML-files as entry-points.",
"license": "MIT",
"repository": {
- "url": "rollup/plugins",
- "directory": "packages/html"
+ "type": "git",
+ "url": "https://git.cerxes.net/rollup-apps/plugin-html.git"
},
- "author": "Andrew Powell ",
- "homepage": "https://github.com/rollup/plugins/tree/master/packages/html#readme",
- "bugs": "https://github.com/rollup/plugins/issues",
+ "author": "Miel Truyen ",
+ "homepage": "https://git.cerxes.net/rollup-apps/plugin-html",
+ "bugs": "https://git.cerxes.net/rollup-apps/plugin-html/issues",
"type": "module",
"engines": {
"node": ">=18"
@@ -27,11 +27,11 @@
"scripts": {
"build": "rollup -c",
"prerelease": "pnpm build",
- "test": "ava",
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
"ci:lint": "pnpm build && pnpm lint-staged",
"ci:test": "pnpm test -- --verbose",
- "dev-test": "ava --match='handlebars*' --update-snapshots"
+ "run-test": "ava",
+ "save-test": "ava --update-snapshots"
},
"files": [
"dist",
@@ -77,7 +77,10 @@
"@babel/preset-typescript": "^7.21.4",
"nyc": "^15.1.0",
"lint-staged": "^13.2.1",
- "handlebars": "^4.7.7"
+ "handlebars": "^4.7.7",
+ "@rollup/plugin-url": "^8.0.1",
+ "chalk": "^5.2.0",
+ "rollup-plugin-livereload": "^2.0.5"
},
"types": "./types/index.d.ts",
"ava": {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 16e0d24..24b5f54 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -27,12 +27,18 @@ devDependencies:
'@rollup/plugin-typescript':
specifier: ^11.1.0
version: 11.1.0(rollup@3.20.3)(tslib@2.5.0)(typescript@5.0.4)
+ '@rollup/plugin-url':
+ specifier: ^8.0.1
+ version: 8.0.1(rollup@3.20.3)
'@types/node':
specifier: ^18.15.11
version: 18.15.11
ava:
specifier: ^5.2.0
version: 5.2.0
+ chalk:
+ specifier: ^5.2.0
+ version: 5.2.0
del-cli:
specifier: ^5.0.0
version: 5.0.0
@@ -54,6 +60,9 @@ devDependencies:
rollup-plugin-delete:
specifier: ^2.0.0
version: 2.0.0
+ rollup-plugin-livereload:
+ specifier: ^2.0.5
+ version: 2.0.5
rollup-plugin-postcss:
specifier: ^4.0.2
version: 4.0.2(postcss@8.4.22)(ts-node@10.9.1)
@@ -560,6 +569,21 @@ packages:
typescript: 5.0.4
dev: true
+ /@rollup/plugin-url@8.0.1(rollup@3.20.3):
+ resolution: {integrity: sha512-8ajztphXb5e19dk3Iwjtm2eSYJR8jFQubZ8pJ1GG2MBMM7/qUedLnZAN+Vt4jqbcT/m27jfjIBocvrzV0giNRw==}
+ engines: {node: '>=14.0.0'}
+ peerDependencies:
+ rollup: ^1.20.0||^2.0.0||^3.0.0
+ peerDependenciesMeta:
+ rollup:
+ optional: true
+ dependencies:
+ '@rollup/pluginutils': 5.0.2(rollup@3.20.3)
+ make-dir: 3.1.0
+ mime: 3.0.0
+ rollup: 3.20.3
+ dev: true
+
/@rollup/pluginutils@5.0.2(rollup@3.20.3):
resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==}
engines: {node: '>=14.0.0'}
@@ -2023,6 +2047,24 @@ packages:
wrap-ansi: 7.0.0
dev: true
+ /livereload-js@3.4.1:
+ resolution: {integrity: sha512-5MP0uUeVCec89ZbNOT/i97Mc+q3SxXmiUGhRFOTmhrGPn//uWVQdCvcLJDy64MSBR5MidFdOR7B9viumoavy6g==}
+ dev: true
+
+ /livereload@0.9.3:
+ resolution: {integrity: sha512-q7Z71n3i4X0R9xthAryBdNGVGAO2R5X+/xXpmKeuPMrteg+W2U8VusTKV3YiJbXZwKsOlFlHe+go6uSNjfxrZw==}
+ engines: {node: '>=8.0.0'}
+ hasBin: true
+ dependencies:
+ chokidar: 3.5.3
+ livereload-js: 3.4.1
+ opts: 2.0.2
+ ws: 7.5.9
+ transitivePeerDependencies:
+ - bufferutil
+ - utf-8-validate
+ dev: true
+
/load-json-file@7.0.1:
resolution: {integrity: sha512-Gnxj3ev3mB5TkVBGad0JM6dmLiQL+o0t23JPBZ9sd+yvSLk05mFoqKBw5N8gbbkU4TNXyqCgIrl/VM17OgUIgQ==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
@@ -2186,6 +2228,12 @@ packages:
picomatch: 2.3.1
dev: true
+ /mime@3.0.0:
+ resolution: {integrity: sha512-jSCU7/VB1loIWBZe14aEYHU/+1UMEHoaO7qxCOVJOw9GgH72VAWppxNcjU+x9a2k3GSIBXNKxXQFqRvvZ7vr3A==}
+ engines: {node: '>=10.0.0'}
+ hasBin: true
+ dev: true
+
/mimic-fn@2.1.0:
resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==}
engines: {node: '>=6'}
@@ -2347,6 +2395,10 @@ packages:
mimic-fn: 4.0.0
dev: true
+ /opts@2.0.2:
+ resolution: {integrity: sha512-k41FwbcLnlgnFh69f4qdUfvDQ+5vaSDnVPFI/y5XuhKRq97EnVVneO9F1ESVCdiVu4fCS2L8usX3mU331hB7pg==}
+ dev: true
+
/p-defer@1.0.0:
resolution: {integrity: sha512-wB3wfAxZpk2AzOfUMJNL+d36xothRSyj8EXOa4f6GMqYDN9BJaaSISbsk+wS9abmnebVw95C2Kb5t85UmpCxuw==}
engines: {node: '>=4'}
@@ -3066,6 +3118,16 @@ packages:
del: 5.1.0
dev: true
+ /rollup-plugin-livereload@2.0.5:
+ resolution: {integrity: sha512-vqQZ/UQowTW7VoiKEM5ouNW90wE5/GZLfdWuR0ELxyKOJUIaj+uismPZZaICU4DnWPVjnpCDDxEqwU7pcKY/PA==}
+ engines: {node: '>=8.3'}
+ dependencies:
+ livereload: 0.9.3
+ transitivePeerDependencies:
+ - bufferutil
+ - utf-8-validate
+ dev: true
+
/rollup-plugin-postcss@4.0.2(postcss@8.4.22)(ts-node@10.9.1):
resolution: {integrity: sha512-05EaY6zvZdmvPUDi3uCcAQoESDcYnv8ogJJQRp6V5kZ6J6P7uAVJlrTZcaaA20wTH527YTnKfkAoPxWI/jPp4w==}
engines: {node: '>=10'}
@@ -3577,6 +3639,19 @@ packages:
signal-exit: 3.0.7
dev: true
+ /ws@7.5.9:
+ resolution: {integrity: sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==}
+ engines: {node: '>=8.3.0'}
+ peerDependencies:
+ bufferutil: ^4.0.1
+ utf-8-validate: ^5.0.2
+ peerDependenciesMeta:
+ bufferutil:
+ optional: true
+ utf-8-validate:
+ optional: true
+ dev: true
+
/y18n@4.0.3:
resolution: {integrity: sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==}
dev: true
diff --git a/rollup.config.mjs b/rollup.config.mjs
index 4da599d..634d11c 100644
--- a/rollup.config.mjs
+++ b/rollup.config.mjs
@@ -30,13 +30,14 @@ export function createConfig({ pkg, external = [] }) {
},
strictDeprecations: true,
output: [
- {
- format: 'cjs',
- file: pkg.main,
- exports: 'named',
- footer: 'module.exports = Object.assign(exports.default, exports);',
- sourcemap: true
- },
+ // TODO: cjs output not supported for now
+ // {
+ // format: 'cjs',
+ // file: pkg.main,
+ // exports: 'named',
+ // footer: 'module.exports = Object.assign(exports.default, exports);',
+ // sourcemap: true
+ // },
{
format: 'es',
file: pkg.module,
@@ -57,7 +58,6 @@ export function createConfig({ pkg, external = [] }) {
...commonOpts,
babelHelpers: "bundled",
}),
- // typescript({ sourceMap: true })]
]
};
}
diff --git a/src/index.ts b/src/index.ts
index 1586014..2be9aa1 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -20,7 +20,7 @@ import type {
} from '../types/index.d.ts';
import {createFilter} from '@rollup/pluginutils';
import {parse as parseHtml, serialize as serializeHtml, DefaultTreeAdapterMap} from "parse5";
-// import {Script, SourceTextModule, createContext} from "node:vm";
+import {readFile} from "node:fs/promises"
const getFiles = (bundle: OutputBundle): Record => {
const result = {} as ReturnType;
@@ -80,20 +80,26 @@ const defaults: RollupHtmlOptions = {
// Internal type
type HtmlImport = {
- id: string,
- rollupResolved: ResolvedId|null,
- node: DefaultTreeAdapterMap['element'],
- reference: LoadReference,
- referenceId: string|null,
- index: number,
+ id: string;
+ rollupResolved: ResolvedId|null;
+ node: DefaultTreeAdapterMap['element'];
+ reference: LoadReference;
+ referenceId: string|null;
+ index: number;
}
type HtmlModule = {
// TODO might want to impose an own unique id, in case this changes after multiple builds
- id: string,
+ id: string;
+ name: string;
+ importers: Set,
resolved: HtmlImport[];
- document: DefaultTreeAdapterMap['document'],
+ assetId?: string|null;
+ document?: DefaultTreeAdapterMap['document'];
}
+const modulePrefix = `// `;
+const moduleSuffix = `// `;
+
export default function html(opts: RollupHtmlOptions = {}): Plugin {
const {
publicPath,
@@ -111,22 +117,167 @@ 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, {});
- let handledHtmls = new Map();// todo clean this per new build?
+ let htmlModules = new Map();// todo clean this per new build?
return {
name: 'html2',// TODO: Need a better name, original plugin was just named `html` and might still make sense to use in conjunction with this one
+ resolveId: {
+ async handler(specifier: string,
+ importer: string | undefined,
+ options: { assertions: Record }){
+ if(!filter(specifier)) return;
+
+ // Let it be resolved like others (node_modules, project aliases, ..)
+ const resolved = await this.resolve(specifier, importer, {
+ skipSelf: true,
+ ...options,
+ });
+
+ if(resolved){
+ const moduleId = resolved.id;
+ const moduleExt = extname(resolved.id);
+ const moduleName = specifier.replace(new RegExp(`${moduleExt}\$`),''); // strip extension of the name if any
+ const htmlModule : HtmlModule = htmlModules.get(moduleId) ?? {
+ id: resolved.id,
+ name: moduleName,
+ resolved: [],
+ assetId: null,
+ importers: new Set(),
+ };
+ htmlModule.importers.add(importer);
+
+ htmlModules.set(htmlModule.id, htmlModule);
+ // TODO: trigger special handling when imported from a JS file (in which case we want might want to export a module returning the HTML, instead of HTML directly)
+ }
+ }
+ },
load: {
async handler(id: string) {
if(!filter(id)) return;
- // We'll be transforming this, but it appears there is no need for us to load it. Rollup will do this
+
+ // Load
+ const htmlModule = htmlModules.get(id);
+ if(htmlModule) {
+ const contents = await readFile(id, {encoding: "utf-8"});
+
+ const htmlSrc = transform ? await transform(contents, {
+ id,
+ }) : contents;
+
+ // Parse document and store it (TODO: check for watch mode, we should check if it needs reparsing or not)
+ const document = htmlModule.document ?? parseHtml(htmlSrc);
+ if(!htmlModule.document){
+ htmlModule.document = document;
+ }
+
+ // Figure out which references to load from this HTML by iterating all nodes (looking for src or href attributes)
+ let loadResults: { reference: LoadReference, node: DefaultTreeAdapterMap['element'] }[] = [];
+ if (document.childNodes) {
+ let nodeQueue = document.childNodes;
+ do {
+ const nextQueue: DefaultTreeAdapterMap['childNode'][][] = [];
+ await Promise.all(nodeQueue.map(async (node) => {
+ const el = (node);
+ let toLoad: LoadResult | undefined = undefined;
+ if (el.attrs) {
+ toLoad = load ? await load({
+ node: el,
+ sourceId: id
+ }) : [];
+ }
+
+ if (toLoad) {
+ const loadIds: LoadReference[] = (toLoad instanceof Array) ? toLoad : [toLoad];
+ for (const loadId of loadIds) {
+ loadResults.push({
+ reference: loadId,
+ node: el,
+ })
+ }
+ }
+
+ if (toLoad !== false) {
+ let asParent = (node);
+ if (asParent.childNodes) {
+ nextQueue.push(asParent.childNodes);
+ }
+ }
+ }));
+ nodeQueue = nextQueue.flat();
+ } while (nodeQueue.length > 0);
+ }
+
+ // Figure out what to resolve (todo, an id can actually be loaded in multiple times, something we might want to keep in mind)
+ await Promise.all(loadResults.map(async ({reference, node}, index) => {
+ const refId = reference.get();
+ const selfResolvedId = resolve ? resolve(refId, {
+ sourceId: id,
+ node,
+ }) : refId;
+ const resolvedId: string = selfResolvedId === true ? refId : (selfResolvedId);
+ if (resolvedId) {
+ const isEntry = !!/.*\.(js|jsx|ts|tsx)$/i.exec(resolvedId); // TODO: for scripts (via src-tag, not those inlined) entry=true. But it should not check via the id (rather how it is imported from html)
+ const rollupResolved = await this.resolve(resolvedId, id, {
+ skipSelf: true,
+ isEntry: isEntry,
+ });
+
+ // TODO: should we test/check if this is refused for resolving here. i.e. external?
+ const htmlImport: HtmlImport = {
+ id: resolvedId,
+ rollupResolved,
+ node,
+ reference,
+ referenceId:
+ // This was triggering resources being marked as entry, and thus their injected loader modules to be outputed to their own files (ie icon.js to load icon.svg)
+ // Should be able to resolve the final HTML from the exported module instead (which though would ideally mean interpreting it as a browser would... )
+ // TODO: however, probably need to uncomment this for
+