Skip to content

Commit ce6be9c

Browse files
committed
fix: unable to match assets when filename contains special characters
1 parent 1278526 commit ce6be9c

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

__tests__/HtmlInlineScriptPlugin.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import inlineWebWorkerConfig from './cases/inline-web-worker/webpack.config';
1212
import ignoreScriptsConfig from './cases/ignore-scripts/webpack.config';
1313
import ignoreHtmlsConfig from './cases/ignore-htmls/webpack.config';
1414
import ignoreScriptsAndHtmlsConfig from './cases/ignore-scripts-and-htmls/webpack.config';
15+
import filenameWithSpecialCharactersConfig from './cases/filename-with-special-characters/webpack.config';
1516

1617
describe('HtmlInlineScriptPlugin', () => {
1718
it('should build simple webpack config without error', async () => {
@@ -202,6 +203,38 @@ describe('HtmlInlineScriptPlugin', () => {
202203
await webpackPromise;
203204
});
204205

206+
it('should inline filename with spacial characters without error', async () => {
207+
const webpackPromise = new Promise((resolve) => {
208+
const compiler = webpack(filenameWithSpecialCharactersConfig);
209+
210+
compiler.run((error, stats) => {
211+
expect(error).toBeNull();
212+
213+
const statsErrors = stats?.compilation.errors;
214+
expect(statsErrors?.length).toBe(0);
215+
216+
const result = fs.readFileSync(
217+
path.join(__dirname, 'cases/filename-with-special-characters/dist/index.html'),
218+
'utf8',
219+
);
220+
221+
const expected = fs.readFileSync(
222+
path.join(__dirname, 'cases/filename-with-special-characters/expected/index.html'),
223+
'utf8',
224+
);
225+
expect(result).toBe(expected);
226+
227+
const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/filename-with-special-characters/expected/'));
228+
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/filename-with-special-characters/dist/'));
229+
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());
230+
231+
resolve(true);
232+
});
233+
});
234+
235+
await webpackPromise;
236+
});
237+
205238
it('should respect plugin options on script matching pattern', async () => {
206239
const webpackPromise = new Promise((resolve) => {
207240
const compiler = webpack(ignoreScriptsConfig);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><meta name="language" content="English"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="minimum-scale=1,initial-scale=1,width=device-width,shrink-to-fit=no"/><title>webpack test</title><script defer="defer">console.log("Hello world");</script></head><body><p>This is minimal code to demonstrate webpack usage</p></body></html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+
<meta name="language" content="English" />
7+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
8+
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no" />
9+
<title>webpack test</title>
10+
</head>
11+
<body>
12+
<p>This is minimal code to demonstrate webpack usage</p>
13+
</body>
14+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line no-console
2+
console.log('Hello world');
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from 'path';
2+
import type { Configuration } from 'webpack';
3+
import HtmlWebpackPlugin from 'html-webpack-plugin';
4+
import Self from '../../../dist';
5+
6+
const config: Configuration = {
7+
mode: 'production',
8+
entry: path.join(__dirname, './fixtures/index.js'),
9+
output: {
10+
path: path.join(__dirname, './dist'),
11+
filename: '[name]@production.js'
12+
},
13+
plugins: [
14+
new HtmlWebpackPlugin({
15+
template: path.resolve(__dirname, './fixtures/index.html')
16+
}),
17+
new Self()
18+
]
19+
};
20+
21+
export default config;

src/HtmlInlineScriptPlugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
6363
return tag;
6464
}
6565

66-
const scriptName = (tag.attributes.src as string).replace(publicPath, '');
66+
// Decoded is needed for special characters in filename like '@' since they will be escaped
67+
const scriptName = decodeURIComponent((tag.attributes.src as string).replace(publicPath, ''));
6768

6869
if (!this.isFileNeedsToBeInlined(scriptName)) {
6970
return tag;

0 commit comments

Comments
 (0)