Skip to content

Commit ddad9f2

Browse files
authoredJul 24, 2020
fix: do not handle non standard script types
1 parent afcd2c9 commit ddad9f2

11 files changed

+2116
-2299
lines changed
 

‎package-lock.json

+1,944-2,238
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+11-12
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,32 @@
4949
"schema-utils": "^2.7.0"
5050
},
5151
"devDependencies": {
52-
"@babel/cli": "^7.10.1",
53-
"@babel/core": "^7.10.2",
54-
"@babel/preset-env": "^7.10.2",
55-
"@commitlint/cli": "^8.3.5",
56-
"@commitlint/config-conventional": "^8.3.4",
52+
"@babel/cli": "^7.10.5",
53+
"@babel/core": "^7.10.5",
54+
"@babel/preset-env": "^7.10.4",
55+
"@commitlint/cli": "^9.1.1",
56+
"@commitlint/config-conventional": "^9.1.1",
5757
"@webpack-contrib/defaults": "^6.3.0",
5858
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
59-
"babel-jest": "^26.0.1",
59+
"babel-jest": "^26.1.0",
6060
"cross-env": "^7.0.2",
6161
"del": "^5.1.0",
6262
"del-cli": "^3.0.1",
6363
"es-check": "^5.1.0",
64-
"eslint": "^7.2.0",
64+
"eslint": "^7.5.0",
6565
"eslint-config-prettier": "^6.11.0",
66-
"eslint-plugin-import": "^2.20.2",
66+
"eslint-plugin-import": "^2.22.0",
6767
"file-loader": "^6.0.0",
6868
"handlebars": "^4.7.6",
6969
"husky": "^4.2.5",
70-
"jest": "^26.0.1",
71-
"jest-junit": "^10.0.0",
70+
"jest": "^26.1.0",
7271
"lint-staged": "^10.2.11",
7372
"memfs": "^3.2.0",
7473
"npm-run-all": "^4.1.5",
75-
"posthtml": "^0.13.0",
74+
"posthtml": "^0.13.1",
7675
"posthtml-webp": "^1.5.0",
7776
"prettier": "^2.0.5",
78-
"standard-version": "^8.0.0",
77+
"standard-version": "^8.0.2",
7978
"webpack": "^4.43.0"
8079
},
8180
"keywords": [

‎src/plugins/source-plugin.js

+28-12
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,21 @@ const defaultAttributes = [
7272
tag: 'script',
7373
attribute: 'src',
7474
type: 'src',
75-
// TODO type
76-
// https://github.com/prettier/prettier/blob/b01591770a2407513af31b59377e87d0892a66a9/src/language-html/utils.js#L367
75+
filter: (tag, attribute, attributes) => {
76+
if (attributes.type) {
77+
const type = getAttributeValue(attributes, 'type').trim().toLowerCase();
78+
79+
if (
80+
type !== 'module' &&
81+
type !== 'text/javascript' &&
82+
type !== 'application/javascript'
83+
) {
84+
return false;
85+
}
86+
}
87+
88+
return true;
89+
},
7790
},
7891
{
7992
tag: 'source',
@@ -139,16 +152,19 @@ export default (options) =>
139152
isUrlRequest(value, root)
140153
);
141154
const getAttribute = (tag, attribute, attributes, resourcePath) => {
142-
return attributeList.find(
143-
(element) =>
144-
(typeof element.tag === 'undefined' ||
145-
(typeof element.tag !== 'undefined' &&
146-
element.tag.toLowerCase() === tag.toLowerCase())) &&
147-
element.attribute.toLowerCase() === attribute.toLowerCase() &&
148-
(element.filter
149-
? element.filter(tag, attribute, attributes, resourcePath)
150-
: true)
151-
);
155+
return attributeList.find((element) => {
156+
const foundTag =
157+
typeof element.tag === 'undefined' ||
158+
(typeof element.tag !== 'undefined' &&
159+
element.tag.toLowerCase() === tag.toLowerCase());
160+
const foundAttribute =
161+
element.attribute.toLowerCase() === attribute.toLowerCase();
162+
const isNotFiltered = element.filter
163+
? element.filter(tag, attribute, attributes, resourcePath)
164+
: true;
165+
166+
return foundTag && foundAttribute && isNotFiltered;
167+
});
152168
};
153169
const { resourcePath } = options;
154170
const imports = new Map();

‎test/__snapshots__/attributes-option.test.js.snap

+83-23
Large diffs are not rendered by default.

‎test/__snapshots__/esModule-option.test.js.snap

+15-3
Large diffs are not rendered by default.

‎test/__snapshots__/loader.test.js.snap

+5-1
Large diffs are not rendered by default.

‎test/__snapshots__/minimize-option.test.js.snap

+21-9
Large diffs are not rendered by default.

‎test/attributes-option.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe("'attributes' option", () => {
205205
expect(getErrors(stats)).toMatchSnapshot('errors');
206206
});
207207

208-
it('should handle all src attributes in all HTML tags except img (testing filter option) tag is undefined', async () => {
208+
it('should handle all src attributes in all HTML tags except img tag (testing filter option)', async () => {
209209
const compiler = getCompiler('simple.js', {
210210
attributes: {
211211
list: [

‎test/fixtures/fallback.file.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"color" : "red"
3+
}

‎test/fixtures/fallback.file.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function test() {}

‎test/fixtures/simple.html

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ <h2>An Ordered HTML List</h2>
221221

222222
<script type="module" src="module.file.js"></script>
223223
<script nomodule src="fallback.file.js"></script>
224+
<script type="text/javascript" src="fallback.file.js"></script>
225+
<script type="application/javascript" src="fallback.file.js"></script>
226+
<script type="application/json" src="fallback.file.json"></script>
227+
<script type="text/x-handlebars-template" src="preprocessor.hbs"></script>
224228
<script type="module">
225229
function test() {}
226230
</script>

0 commit comments

Comments
 (0)
Please sign in to comment.