diff --git a/.cspell.json b/.cspell.json index 67e53a6e..3797e9d7 100644 --- a/.cspell.json +++ b/.cspell.json @@ -36,9 +36,10 @@ "vspace", "jsbeautify", "Gitter", - "commitlint" + "commitlint", + "postprocessor", + "eslintcache" ], - "ignorePaths": [ "CHANGELOG.md", "package.json", diff --git a/.gitignore b/.gitignore index 76df9327..32ba562b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ logs *.log npm-debug.log* .eslintcache +.cspellcache /coverage /dist /local diff --git a/README.md b/README.md index 5a0279eb..d1130dee 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ module.exports = { - **[`sources`](#sources)** - **[`preprocessor`](#preprocessor)** +- **[`postprocessor`](#postprocessor)** - **[`minimize`](#minimize)** - **[`esModule`](#esmodule)** @@ -490,10 +491,7 @@ module.exports = { Type: ```ts -type preprocessor = ( - content: string | Buffer, - loaderContext: LoaderContext, -) => HTMLElement; +type preprocessor = (content: string, loaderContext: LoaderContext) => string; ``` Default: `undefined` @@ -591,6 +589,85 @@ module.exports = { }; ``` +### `postprocessor` + +Type: + +```ts +type postprocessor = (content: string, loaderContext: LoaderContext) => string; +``` + +Default: `undefined` + +Allows post-processing of content after replacing all attributes (like `src`/`srcset`/etc). + +**file.html** + +```html + + + + +
{{firstname}} {{lastname}}
\\n \\n{{firstname}} {{lastname}}
+ +Alexander Krasnoyarov
\\nAlexander Krasnoyarov
@@ -21,11 +21,11 @@ exports[`'process' option should work with Async "preprocessor" Function option: " `; -exports[`'process' option should work with Async "preprocessor" Function option: warnings 1`] = `[]`; +exports[`'preprocess' option should work with async "preprocessor" function option: warnings 1`] = `[]`; -exports[`'process' option should work with the "preprocessor" option #2: errors 1`] = `[]`; +exports[`'preprocess' option should work with the "preprocessor" option #2: errors 1`] = `[]`; -exports[`'process' option should work with the "preprocessor" option #2: module 1`] = ` +exports[`'preprocess' option should work with the "preprocessor" option #2: module 1`] = ` "// Imports import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js"; var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png.webp", import.meta.url); @@ -38,16 +38,16 @@ var code = " " `; -exports[`'process' option should work with the "preprocessor" option #2: warnings 1`] = `[]`; +exports[`'preprocess' option should work with the "preprocessor" option #2: warnings 1`] = `[]`; -exports[`'process' option should work with the "preprocessor" option: errors 1`] = `[]`; +exports[`'preprocess' option should work with the "preprocessor" option: errors 1`] = `[]`; -exports[`'process' option should work with the "preprocessor" option: module 1`] = ` +exports[`'preprocess' option should work with the "preprocessor" option: module 1`] = ` "// Imports import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js"; var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png", import.meta.url); @@ -58,7 +58,7 @@ var code = "Alexander Krasnoyarov
\\nAlexander Krasnoyarov
@@ -66,11 +66,11 @@ exports[`'process' option should work with the "preprocessor" option: result 1`] " `; -exports[`'process' option should work with the "preprocessor" option: warnings 1`] = `[]`; +exports[`'preprocess' option should work with the "preprocessor" option: warnings 1`] = `[]`; -exports[`'process' option should work with the Async "preprocessor" Function option #2: errors 1`] = `[]`; +exports[`'preprocess' option should work with the async "preprocessor" function option #2: errors 1`] = `[]`; -exports[`'process' option should work with the Async "preprocessor" Function option #2: module 1`] = ` +exports[`'preprocess' option should work with the async "preprocessor" function option #2: module 1`] = ` "// Imports import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from "../../src/runtime/getUrl.js"; var ___HTML_LOADER_IMPORT_0___ = new URL("./image.png.webp", import.meta.url); @@ -83,9 +83,9 @@ var code = " " `; -exports[`'process' option should work with the Async "preprocessor" Function option #2: warnings 1`] = `[]`; +exports[`'preprocess' option should work with the async "preprocessor" function option #2: warnings 1`] = `[]`; diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index 3cae344e..9db8c393 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -95,47 +95,47 @@ exports[`validate options should throw an error on the "sources" option with "tr exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = ` "Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema. - options has an unknown property 'unknown'. These properties are valid: - object { preprocessor?, sources?, minimize?, esModule? }" + object { preprocessor?, postprocessor?, sources?, minimize?, esModule? }" `; diff --git a/test/fixtures/gallery.html b/test/fixtures/gallery.html new file mode 100644 index 00000000..279b5da4 --- /dev/null +++ b/test/fixtures/gallery.html @@ -0,0 +1 @@ +