|
1 | 1 | import fs from 'fs';
|
2 | 2 | import path from 'path';
|
3 | 3 | import webpack from 'webpack';
|
| 4 | + |
| 5 | +import Self from '../dist'; |
| 6 | + |
4 | 7 | import simpleConfig from './cases/simple/webpack.config';
|
5 | 8 | import multipleInstanceConfig from './cases/multiple-instance/webpack.config';
|
6 | 9 | import jsWithImportConfig from './cases/js-with-import/webpack.config';
|
7 | 10 | import webWorkerConfig from './cases/web-worker/webpack.config';
|
8 | 11 | import inlineWebWorkerConfig from './cases/inline-web-worker/webpack.config';
|
| 12 | +import ignoreScriptsConfig from './cases/ignore-scripts/webpack.config'; |
| 13 | +import ignoreHtmlsConfig from './cases/ignore-htmls/webpack.config'; |
| 14 | +import ignoreScriptsAndHtmlsConfig from './cases/ignore-scripts-and-htmls/webpack.config'; |
9 | 15 |
|
10 | 16 | describe('HtmlInlineScriptPlugin', () => {
|
11 | 17 | it('should build simple webpack config without error', async () => {
|
@@ -195,4 +201,135 @@ describe('HtmlInlineScriptPlugin', () => {
|
195 | 201 |
|
196 | 202 | await webpackPromise;
|
197 | 203 | });
|
| 204 | + |
| 205 | + it('should respect plugin options on script matching pattern', async () => { |
| 206 | + const webpackPromise = new Promise((resolve) => { |
| 207 | + const compiler = webpack(ignoreScriptsConfig); |
| 208 | + |
| 209 | + compiler.run((error, stats) => { |
| 210 | + expect(error).toBeNull(); |
| 211 | + |
| 212 | + const statsErrors = stats?.compilation.errors; |
| 213 | + expect(statsErrors?.length).toBe(0); |
| 214 | + |
| 215 | + const result1 = fs.readFileSync( |
| 216 | + path.join(__dirname, 'cases/ignore-scripts/dist/index.html'), |
| 217 | + 'utf8', |
| 218 | + ); |
| 219 | + |
| 220 | + const expected1 = fs.readFileSync( |
| 221 | + path.join(__dirname, 'cases/ignore-scripts/expected/index.html'), |
| 222 | + 'utf8', |
| 223 | + ); |
| 224 | + |
| 225 | + expect(result1).toBe(expected1); |
| 226 | + |
| 227 | + const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts/expected/')); |
| 228 | + const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts/dist/')); |
| 229 | + expect(expectedFileList.sort()).toEqual(generatedFileList.sort()); |
| 230 | + |
| 231 | + resolve(true); |
| 232 | + }); |
| 233 | + }); |
| 234 | + |
| 235 | + await webpackPromise; |
| 236 | + }); |
| 237 | + |
| 238 | + it('should respect plugin options on html template matching pattern', async () => { |
| 239 | + const webpackPromise = new Promise((resolve) => { |
| 240 | + const compiler = webpack(ignoreHtmlsConfig); |
| 241 | + |
| 242 | + compiler.run((error, stats) => { |
| 243 | + expect(error).toBeNull(); |
| 244 | + |
| 245 | + const statsErrors = stats?.compilation.errors; |
| 246 | + expect(statsErrors?.length).toBe(0); |
| 247 | + |
| 248 | + const result1 = fs.readFileSync( |
| 249 | + path.join(__dirname, 'cases/ignore-htmls/dist/index.html'), |
| 250 | + 'utf8', |
| 251 | + ); |
| 252 | + |
| 253 | + const expected1 = fs.readFileSync( |
| 254 | + path.join(__dirname, 'cases/ignore-htmls/expected/index.html'), |
| 255 | + 'utf8', |
| 256 | + ); |
| 257 | + |
| 258 | + expect(result1).toBe(expected1); |
| 259 | + |
| 260 | + const result2 = fs.readFileSync( |
| 261 | + path.join(__dirname, 'cases/ignore-htmls/dist/page2.html'), |
| 262 | + 'utf8', |
| 263 | + ); |
| 264 | + |
| 265 | + const expected2 = fs.readFileSync( |
| 266 | + path.join(__dirname, 'cases/ignore-htmls/expected/page2.html'), |
| 267 | + 'utf8', |
| 268 | + ); |
| 269 | + |
| 270 | + expect(result2).toBe(expected2); |
| 271 | + |
| 272 | + const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-htmls/expected/')); |
| 273 | + const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-htmls/dist/')); |
| 274 | + expect(expectedFileList.sort()).toEqual(generatedFileList.sort()); |
| 275 | + |
| 276 | + resolve(true); |
| 277 | + }); |
| 278 | + }); |
| 279 | + |
| 280 | + await webpackPromise; |
| 281 | + }); |
| 282 | + |
| 283 | + it('should respect plugin options on both script matching pattern and html template matching pattern', async () => { |
| 284 | + const webpackPromise = new Promise((resolve) => { |
| 285 | + const compiler = webpack(ignoreScriptsAndHtmlsConfig); |
| 286 | + |
| 287 | + compiler.run((error, stats) => { |
| 288 | + expect(error).toBeNull(); |
| 289 | + |
| 290 | + const statsErrors = stats?.compilation.errors; |
| 291 | + expect(statsErrors?.length).toBe(0); |
| 292 | + |
| 293 | + const result1 = fs.readFileSync( |
| 294 | + path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/index.html'), |
| 295 | + 'utf8', |
| 296 | + ); |
| 297 | + |
| 298 | + const expected1 = fs.readFileSync( |
| 299 | + path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/index.html'), |
| 300 | + 'utf8', |
| 301 | + ); |
| 302 | + |
| 303 | + expect(result1).toBe(expected1); |
| 304 | + |
| 305 | + const result2 = fs.readFileSync( |
| 306 | + path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/page2.html'), |
| 307 | + 'utf8', |
| 308 | + ); |
| 309 | + |
| 310 | + const expected2 = fs.readFileSync( |
| 311 | + path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/page2.html'), |
| 312 | + 'utf8', |
| 313 | + ); |
| 314 | + |
| 315 | + expect(result2).toBe(expected2); |
| 316 | + |
| 317 | + const expectedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts-and-htmls/expected/')); |
| 318 | + const generatedFileList = fs.readdirSync(path.join(__dirname, 'cases/ignore-scripts-and-htmls/dist/')); |
| 319 | + expect(expectedFileList.sort()).toEqual(generatedFileList.sort()); |
| 320 | + |
| 321 | + resolve(true); |
| 322 | + }); |
| 323 | + }); |
| 324 | + |
| 325 | + await webpackPromise; |
| 326 | + }); |
| 327 | + |
| 328 | + it('should build throw error if options passed to plugin is old options format', async () => { |
| 329 | + const initializedPlugin = () => { |
| 330 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-new |
| 331 | + new Self([/runtime~.+[.]js$/, /app~.+[.]js$/] as any); |
| 332 | + }; |
| 333 | + expect(initializedPlugin).toThrow(); |
| 334 | + }); |
198 | 335 | });
|
0 commit comments