Skip to content

Commit

Permalink
fix: do not handle non standard script types
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jul 24, 2020
1 parent afcd2c9 commit ddad9f2
Show file tree
Hide file tree
Showing 11 changed files with 2,116 additions and 2,299 deletions.
4,182 changes: 1,944 additions & 2,238 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions package.json
Expand Up @@ -49,33 +49,32 @@
"schema-utils": "^2.7.0"
},
"devDependencies": {
"@babel/cli": "^7.10.1",
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@commitlint/cli": "^9.1.1",
"@commitlint/config-conventional": "^9.1.1",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.0.1",
"babel-jest": "^26.1.0",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.1",
"es-check": "^5.1.0",
"eslint": "^7.2.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-import": "^2.22.0",
"file-loader": "^6.0.0",
"handlebars": "^4.7.6",
"husky": "^4.2.5",
"jest": "^26.0.1",
"jest-junit": "^10.0.0",
"jest": "^26.1.0",
"lint-staged": "^10.2.11",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"posthtml": "^0.13.0",
"posthtml": "^0.13.1",
"posthtml-webp": "^1.5.0",
"prettier": "^2.0.5",
"standard-version": "^8.0.0",
"standard-version": "^8.0.2",
"webpack": "^4.43.0"
},
"keywords": [
Expand Down
40 changes: 28 additions & 12 deletions src/plugins/source-plugin.js
Expand Up @@ -72,8 +72,21 @@ const defaultAttributes = [
tag: 'script',
attribute: 'src',
type: 'src',
// TODO type
// https://github.com/prettier/prettier/blob/b01591770a2407513af31b59377e87d0892a66a9/src/language-html/utils.js#L367
filter: (tag, attribute, attributes) => {
if (attributes.type) {
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();

if (
type !== 'module' &&
type !== 'text/javascript' &&
type !== 'application/javascript'
) {
return false;
}
}

return true;
},
},
{
tag: 'source',
Expand Down Expand Up @@ -139,16 +152,19 @@ export default (options) =>
isUrlRequest(value, root)
);
const getAttribute = (tag, attribute, attributes, resourcePath) => {
return attributeList.find(
(element) =>
(typeof element.tag === 'undefined' ||
(typeof element.tag !== 'undefined' &&
element.tag.toLowerCase() === tag.toLowerCase())) &&
element.attribute.toLowerCase() === attribute.toLowerCase() &&
(element.filter
? element.filter(tag, attribute, attributes, resourcePath)
: true)
);
return attributeList.find((element) => {
const foundTag =
typeof element.tag === 'undefined' ||
(typeof element.tag !== 'undefined' &&
element.tag.toLowerCase() === tag.toLowerCase());
const foundAttribute =
element.attribute.toLowerCase() === attribute.toLowerCase();
const isNotFiltered = element.filter
? element.filter(tag, attribute, attributes, resourcePath)
: true;

return foundTag && foundAttribute && isNotFiltered;
});
};
const { resourcePath } = options;
const imports = new Map();
Expand Down
106 changes: 83 additions & 23 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/attributes-option.test.js
Expand Up @@ -205,7 +205,7 @@ describe("'attributes' option", () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should handle all src attributes in all HTML tags except img (testing filter option) tag is undefined', async () => {
it('should handle all src attributes in all HTML tags except img tag (testing filter option)', async () => {
const compiler = getCompiler('simple.js', {
attributes: {
list: [
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/fallback.file.json
@@ -0,0 +1,3 @@
{
"color" : "red"
}
1 change: 1 addition & 0 deletions test/fixtures/fallback.file.ts
@@ -0,0 +1 @@
function test() {}
4 changes: 4 additions & 0 deletions test/fixtures/simple.html
Expand Up @@ -221,6 +221,10 @@ <h2>An Ordered HTML List</h2>

<script type="module" src="module.file.js"></script>
<script nomodule src="fallback.file.js"></script>
<script type="text/javascript" src="fallback.file.js"></script>
<script type="application/javascript" src="fallback.file.js"></script>
<script type="application/json" src="fallback.file.json"></script>
<script type="text/x-handlebars-template" src="preprocessor.hbs"></script>
<script type="module">
function test() {}
</script>
Expand Down

0 comments on commit ddad9f2

Please sign in to comment.