Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions __tests__/HtmlInlineScriptPlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import inlineWebWorkerConfig from './cases/inline-web-worker/webpack.config';
import ignoreScriptsConfig from './cases/ignore-scripts/webpack.config';
import ignoreHtmlsConfig from './cases/ignore-htmls/webpack.config';
import ignoreScriptsAndHtmlsConfig from './cases/ignore-scripts-and-htmls/webpack.config';
import filenameWithSpecialCharactersConfig from './cases/filename-with-special-characters/webpack.config';

describe('HtmlInlineScriptPlugin', () => {
it('should build simple webpack config without error', async () => {
Expand Down Expand Up @@ -202,6 +203,38 @@ describe('HtmlInlineScriptPlugin', () => {
await webpackPromise;
});

it('should inline filename with spacial characters without error', async () => {
const webpackPromise = new Promise((resolve) => {
const compiler = webpack(filenameWithSpecialCharactersConfig);

compiler.run((error, stats) => {
expect(error).toBeNull();

const statsErrors = stats?.compilation.errors;
expect(statsErrors?.length).toBe(0);

const result = fs.readFileSync(
path.join(__dirname, 'cases/filename-with-special-characters/dist/index.html'),
'utf8',
);

const expected = fs.readFileSync(
path.join(__dirname, 'cases/filename-with-special-characters/expected/index.html'),
'utf8',
);
expect(result).toBe(expected);

const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/filename-with-special-characters/expected/'));
const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/filename-with-special-characters/dist/'));
expect(expectedFileList.sort()).toEqual(generatedFileList.sort());

resolve(true);
});
});

await webpackPromise;
});

it('should respect plugin options on script matching pattern', async () => {
const webpackPromise = new Promise((resolve) => {
const compiler = webpack(ignoreScriptsConfig);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!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>
</head>
<body>
<p>This is minimal code to demonstrate webpack usage</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line no-console
console.log('Hello world');
21 changes: 21 additions & 0 deletions __tests__/cases/filename-with-special-characters/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path';
import type { Configuration } from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import Self from '../../../dist';

const config: Configuration = {
mode: 'production',
entry: path.join(__dirname, './fixtures/index.js'),
output: {
path: path.join(__dirname, './dist'),
filename: '[name]@production.js'
},
plugins: [
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './fixtures/index.html')
}),
new Self()
]
};

export default config;
3 changes: 2 additions & 1 deletion src/HtmlInlineScriptPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class HtmlInlineScriptPlugin implements WebpackPluginInstance {
return tag;
}

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

if (!this.isFileNeedsToBeInlined(scriptName)) {
return tag;
Expand Down