diff --git a/README.md b/README.md index d8e32c4..20e5cc2 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#comma ### `template` -Type: `Function`
+Type: `Function`\ Default: `undefined`\ Returns: `String` @@ -94,9 +94,6 @@ async function build() { 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) ## Status - -This plugin is in an early state. As such not everything that is supported yet, the options are laregely undocumented and may change. - ### (Rudimentarily) supported - Importing JS via ` + + diff --git a/test/rewrite-url/fixtures/index.html b/test/rewrite-url/fixtures/index.html new file mode 100644 index 0000000..13c140d --- /dev/null +++ b/test/rewrite-url/fixtures/index.html @@ -0,0 +1,8 @@ + + + + +
+ + + diff --git a/test/rewrite-url/test.js b/test/rewrite-url/test.js new file mode 100644 index 0000000..1bea26f --- /dev/null +++ b/test/rewrite-url/test.js @@ -0,0 +1,44 @@ +import {resolve, join, dirname} from "node:path"; +import * as path from "node:path"; +import test from "ava"; +import { rollup } from "rollup"; +import {debugPrintOutput, getCode} from "../util/test.js"; + +import html from "../../src/index.ts"; + +const output = { + dir: 'output', // Output all files + format: 'es', // iifi and cjs should be added to tests + sourcemap: true,// Test if #sourcemapUrl is not accidentally included in the html-output +}; + +import {fileURLToPath} from "node:url"; +const __dirname = dirname(fileURLToPath(import.meta.url)); +process.chdir(join(__dirname, 'fixtures')); + + +test.serial('rewrite-url', async (t) => { + const bundle = await rollup({ + input: { + ['index']: 'index.html', + ['admin/index']: resolve(__dirname,'fixtures','admin/index.html'), + ['admin/app']: resolve(__dirname,'fixtures','admin/app.js'), + }, + plugins: [ + html({ + rewriteUrl(relative, {rootPath, from}){ + return `/${rootPath}`; + } + }), + ] + }); + const code = await getCode(bundle, output, true); + debugPrintOutput('rewrite-url',code); + t.snapshot(code); +}); + +// TODO various parameters +// - format: cjs, iifi, ... +// - sourcemap: inline, false, (and the various exotic sourcemap options) +// Watch mode tests would be its own dir +// ... diff --git a/types/index.d.ts b/types/index.d.ts index c900df1..0813177 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -15,6 +15,11 @@ export interface RollupHtmlTransformContext { // files: Record; } +export interface RewriteUrlCallbackContext { + from: string; + rootPath: string; +} +export type RewriteUrlCallback = (relative: string, context: RewriteUrlCallbackContext) => string|Promise; export type TransformCallback = (source: string, transformContext: RollupHtmlTransformContext) => string|Promise; export interface RollupHtmlOptions { @@ -23,6 +28,7 @@ export interface RollupHtmlOptions { * Follows the same logic as rollup's [entryFileNames](https://rollupjs.org/configuration-options/#output-entryfilenames). */ htmlFileNames?: string|((chunkInfo: PreRenderedChunk) => string); + /** * Transform a source file passed into this plugin to HTML. For example: a handlebars transform * ``` @@ -33,6 +39,17 @@ export interface RollupHtmlOptions { */ transform?: TransformCallback; + /** + * Optional callback to rewrite how resources are referenced in the output HTML. + * For example to rewrite urls to as paths from the root of your website: + * ``` + * rewriteUrl(relative, {rootPath, from}){ + * return `/${rootPath}`; + * } + * ``` + */ + rewriteUrl?: RewriteUrlCallback; + /** * Detect which references (, ) to resolve from a HTML node. * This rarely needs to be overloaded, but can be used to support non-native attributes used by custom-elements. @@ -49,7 +66,14 @@ export interface RollupHtmlOptions { * Return a falsey value to skip this reference. Return true to resolve as is. (or string to transform the id) */ resolve?: ResolveCallback; + + /** + * [Pattern](https://github.com/micromatch/picomatch#globbing-features) to include + */ include?: FilterPattern; + /** + * [Pattern](https://github.com/micromatch/picomatch#globbing-features) to exclude + */ exclude?: FilterPattern }