Skip to content

Commit b974ecb

Browse files
fix: support ES6 syntax in script tags when minimize (#219)
1 parent 301c26f commit b974ecb

File tree

4 files changed

+45
-77
lines changed

4 files changed

+45
-77
lines changed

package-lock.json

Lines changed: 37 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"es6-templates": "^0.2.3",
4545
"fastparse": "^1.1.2",
46-
"html-minifier": "^4.0.0",
46+
"html-minifier-terser": "^5.0.2",
4747
"loader-utils": "^1.2.3",
4848
"schema-utils": "^2.6.4"
4949
},

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { parse } from 'url';
22

33
import { compile } from 'es6-templates';
4-
import { minify } from 'html-minifier';
4+
import { minify } from 'html-minifier-terser';
55
import { getOptions, isUrlRequest, urlToRequest } from 'loader-utils';
66

77
import validateOptions from 'schema-utils';

test/minimize-option.test.js

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('"minimize" option', () => {
8989
);
9090
});
9191

92-
it('should preserve comments', () => {
92+
it('should support options for minimizer', () => {
9393
const result = loader.call(
9494
{
9595
query: {
@@ -115,56 +115,19 @@ describe('"minimize" option', () => {
115115
);
116116
});
117117

118-
it('should preserve comments and white spaces when minimizing', () => {
118+
it('should support ES6 syntax', () => {
119119
const result = loader.call(
120120
{
121121
query: {
122-
minimize: {
123-
conservativeCollapse: true,
124-
removeAttributeQuotes: true,
125-
keepClosingSlash: true,
126-
minifyJS: true,
127-
minifyCSS: true,
128-
removeScriptTypeAttributes: true,
129-
removeStyleLinkTypeAttributes: true,
130-
useShortDoctype: true,
131-
removeComments: false,
132-
collapseWhitespace: false,
133-
},
134-
},
135-
},
136-
'<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p> <!-- comment --> <img src="image.png" />'
137-
);
138-
139-
expect(result).toBe(
140-
`${GET_URL_CODE}module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p> <!-- comment --> <img src=" + __url__(require("./image.png")) + " />";`
141-
);
142-
});
143-
144-
it('should treat attributes as case sensitive', () => {
145-
const result = loader.call(
146-
{
147-
query: {
148-
minimize: {
149-
removeComments: true,
150-
collapseWhitespace: true,
151-
conservativeCollapse: true,
152-
removeAttributeQuotes: true,
153-
keepClosingSlash: true,
154-
minifyJS: true,
155-
minifyCSS: true,
156-
removeScriptTypeAttributes: true,
157-
removeStyleLinkTypeAttributes: true,
158-
useShortDoctype: true,
159-
caseSensitive: true,
160-
},
122+
minimize: true,
161123
},
162124
},
163-
'<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src="image.png" />'
125+
// eslint-disable-next-line no-template-curly-in-string
126+
'<!-- comment --><h1>My First Heading</h1>\n\n<p>My first paragraph.</p> <script> console.log(1 + 2 + `${3 + 3}`) </script>'
164127
);
165128

166129
expect(result).toBe(
167-
`${GET_URL_CODE}module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=" + __url__(require("./image.png")) + " />";`
130+
`${GET_URL_CODE}module.exports = "<h1>My First Heading</h1> <p>My first paragraph.</p> <script>console.log(\\"36\\")</script>";`
168131
);
169132
});
170133
});

0 commit comments

Comments
 (0)