diff --git a/__tests__/HtmlInlineScriptPlugin.test.ts b/__tests__/HtmlInlineScriptPlugin.test.ts index 68dda11..a518687 100644 --- a/__tests__/HtmlInlineScriptPlugin.test.ts +++ b/__tests__/HtmlInlineScriptPlugin.test.ts @@ -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 () => { @@ -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); diff --git a/__tests__/cases/filename-with-special-characters/expected/index.html b/__tests__/cases/filename-with-special-characters/expected/index.html new file mode 100644 index 0000000..bbe46c3 --- /dev/null +++ b/__tests__/cases/filename-with-special-characters/expected/index.html @@ -0,0 +1 @@ +webpack test

This is minimal code to demonstrate webpack usage

\ No newline at end of file diff --git a/__tests__/cases/filename-with-special-characters/fixtures/index.html b/__tests__/cases/filename-with-special-characters/fixtures/index.html new file mode 100644 index 0000000..c061c8e --- /dev/null +++ b/__tests__/cases/filename-with-special-characters/fixtures/index.html @@ -0,0 +1,14 @@ + + + + + + + + + webpack test + + +

This is minimal code to demonstrate webpack usage

+ + diff --git a/__tests__/cases/filename-with-special-characters/fixtures/index.js b/__tests__/cases/filename-with-special-characters/fixtures/index.js new file mode 100644 index 0000000..2ce7e4a --- /dev/null +++ b/__tests__/cases/filename-with-special-characters/fixtures/index.js @@ -0,0 +1,2 @@ +// eslint-disable-next-line no-console +console.log('Hello world'); diff --git a/__tests__/cases/filename-with-special-characters/webpack.config.ts b/__tests__/cases/filename-with-special-characters/webpack.config.ts new file mode 100644 index 0000000..fce6552 --- /dev/null +++ b/__tests__/cases/filename-with-special-characters/webpack.config.ts @@ -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; diff --git a/src/HtmlInlineScriptPlugin.ts b/src/HtmlInlineScriptPlugin.ts index 1d5392a..36346a2 100644 --- a/src/HtmlInlineScriptPlugin.ts +++ b/src/HtmlInlineScriptPlugin.ts @@ -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;