Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding "scriptingEnabled" option to source set #448

Merged
merged 1 commit into from Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -90,6 +90,7 @@ type sources =
value: string,
resourcePath: string
) => boolean;
scriptingEnabled?: boolean;
};
```

Expand Down Expand Up @@ -447,6 +448,43 @@ module.exports = {
};
```

#### `scriptingEnabled`

Type:

```ts
type scriptingEnabled = boolean;
```

Default: `true`

By default, the parser in `html-loader` interprets content inside `<noscript>` tags as `#text`, so processing of content inside this tag will be ignored.

In order to enable processing inside `<noscript>` for content recognition by the parser as `#AST`, set this parameter to: `false`

Additional information: [scriptingEnabled](https://parse5.js.org/interfaces/parse5.ParserOptions.html#scriptingEnabled)

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.html$/i,
loader: "html-loader",
options: {
sources: {
// Enables processing inside the <noscript> tag
scriptingEnabled: false,
},
},
},
],
},
};
```

### `preprocessor`

Type:
Expand Down
3 changes: 3 additions & 0 deletions src/options.json
Expand Up @@ -56,6 +56,9 @@
},
"urlFilter": {
"instanceof": "Function"
},
"scriptingEnabled": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/sources-plugin.js
Expand Up @@ -10,7 +10,10 @@ import {
export default (options) =>
function process(html) {
const sources = [];
const document = parse(html, { sourceCodeLocationInfo: true });
const document = parse(html, {
sourceCodeLocationInfo: true,
scriptingEnabled: options.sources.scriptingEnabled,
});

let needIgnore = false;

Expand Down
9 changes: 8 additions & 1 deletion src/utils.js
Expand Up @@ -1122,7 +1122,14 @@ function getSourcesOption(rawOptions) {

const sources = normalizeSourcesList(rawOptions.sources.list);

return { list: sources, urlFilter: rawOptions.sources.urlFilter };
return {
list: sources,
urlFilter: rawOptions.sources.urlFilter,
scriptingEnabled:
typeof rawOptions.sources.scriptingEnabled === "undefined"
? true
: rawOptions.sources.scriptingEnabled,
};
}

export function normalizeOptions(rawOptions, loaderContext) {
Expand Down
33 changes: 27 additions & 6 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

51 changes: 39 additions & 12 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

158 changes: 126 additions & 32 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions test/__snapshots__/validate-options.test.js.snap
Expand Up @@ -29,13 +29,13 @@ exports[`validate options should throw an error on the "preprocessor" option wit
exports[`validate options should throw an error on the "sources" option with "[]" value 1`] = `
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
- options.sources should be one of these:
boolean | object { list?, urlFilter? }
boolean | object { list?, urlFilter?, scriptingEnabled? }
-> By default every loadable attributes (for example - <img src='image.png'>) is imported (const img = require('./image.png'). You may need to specify loaders for images in your configuration.
-> Read more at https://github.com/webpack-contrib/html-loader#sources
Details:
* options.sources should be a boolean.
* options.sources should be an object:
object { list?, urlFilter? }"
object { list?, urlFilter?, scriptingEnabled? }"
`;

exports[`validate options should throw an error on the "sources" option with "{"list":[]}" value 1`] = `
Expand Down Expand Up @@ -64,10 +64,15 @@ exports[`validate options should throw an error on the "sources" option with "{"
\\"src\\" | \\"srcset\\""
`;

exports[`validate options should throw an error on the "sources" option with "{"scriptingEnabled":"true"}" value 1`] = `
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
- options.sources.scriptingEnabled should be a boolean."
`;

exports[`validate options should throw an error on the "sources" option with "{"unknown":true}" value 1`] = `
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
- options.sources has an unknown property 'unknown'. These properties are valid:
object { list?, urlFilter? }"
object { list?, urlFilter?, scriptingEnabled? }"
`;

exports[`validate options should throw an error on the "sources" option with "{"urlFilter":false}" value 1`] = `
Expand All @@ -78,13 +83,13 @@ exports[`validate options should throw an error on the "sources" option with "{"
exports[`validate options should throw an error on the "sources" 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.sources should be one of these:
boolean | object { list?, urlFilter? }
boolean | object { list?, urlFilter?, scriptingEnabled? }
-> By default every loadable attributes (for example - <img src='image.png'>) is imported (const img = require('./image.png'). You may need to specify loaders for images in your configuration.
-> Read more at https://github.com/webpack-contrib/html-loader#sources
Details:
* options.sources should be a boolean.
* options.sources should be an object:
object { list?, urlFilter? }"
object { list?, urlFilter?, scriptingEnabled? }"
`;

exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
Expand Down
Binary file added test/fixtures/noscript.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion test/fixtures/simple.html
Expand Up @@ -447,4 +447,8 @@ <h2>An Ordered HTML List</h2>
<template type="some_type">
<div class="will-not-be-parse"></div>
</template>
</div>
</div>

<noscript>
<img src="./noscript.png" alt="noscript content" />
</noscript>
1 change: 1 addition & 0 deletions test/sources-option.test.js
Expand Up @@ -297,6 +297,7 @@ describe("'sources' option", () => {

return true;
},
scriptingEnabled: false,
},
});
const stats = await compile(compiler);
Expand Down
3 changes: 3 additions & 0 deletions test/validate-options.test.js
Expand Up @@ -58,6 +58,8 @@ describe("validate options", () => {
],
},
{ urlFilter: () => true },
{ scriptingEnabled: true },
{ scriptingEnabled: false },
{
list: [
{
Expand Down Expand Up @@ -122,6 +124,7 @@ describe("validate options", () => {
],
},
{ urlFilter: false },
{ scriptingEnabled: "true" },
{ unknown: true },
],
},
Expand Down