diff --git a/.eslintrc.js b/.eslintrc.js index 095ce2a..29e7717 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,4 +1,4 @@ module.exports = { root: true, - extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'], + extends: ["@webpack-contrib/eslint-config-webpack", "prettier"], }; diff --git a/.prettierrc.js b/.prettierrc.js deleted file mode 100644 index 4f14003..0000000 --- a/.prettierrc.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = { singleQuote: true }; diff --git a/README.md b/README.md index ddb67e6..71ad6d7 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ $ npm install worker-loader --save-dev **App.js** ```js -import Worker from 'worker-loader!./Worker.js'; +import Worker from "worker-loader!./Worker.js"; ``` ### Config @@ -42,7 +42,7 @@ module.exports = { rules: [ { test: /\.worker\.js$/, - use: { loader: 'worker-loader' }, + use: { loader: "worker-loader" }, }, ], }, @@ -52,14 +52,14 @@ module.exports = { **App.js** ```js -import Worker from './file.worker.js'; +import Worker from "./file.worker.js"; const worker = new Worker(); worker.postMessage({ a: 1 }); worker.onmessage = function (event) {}; -worker.addEventListener('message', function (event) {}); +worker.addEventListener("message", function (event) {}); ``` And run `webpack` via your preferred method. @@ -94,9 +94,9 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { - worker: 'SharedWorker', + worker: "SharedWorker", }, }, ], @@ -116,14 +116,14 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { worker: { - type: 'SharedWorker', + type: "SharedWorker", options: { - type: 'classic', - credentials: 'omit', - name: 'my-custom-worker-name', + type: "classic", + credentials: "omit", + name: "my-custom-worker-name", }, }, }, @@ -151,9 +151,9 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { - publicPath: '/scripts/workers/', + publicPath: "/scripts/workers/", }, }, ], @@ -171,7 +171,7 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { publicPath: (pathData, assetInfo) => { return `/scripts/${pathData.hash}/workers/`; @@ -200,9 +200,9 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { - filename: '[name].[contenthash].worker.js', + filename: "[name].[contenthash].worker.js", }, }, ], @@ -220,16 +220,16 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { filename: (pathData) => { if ( /\.worker\.(c|m)?js$/i.test(pathData.chunk.entryModule.resource) ) { - return '[name].custom.worker.js'; + return "[name].custom.worker.js"; } - return '[name].js'; + return "[name].js"; }, }, }, @@ -253,9 +253,9 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { - chunkFilename: '[id].[contenthash].worker.js', + chunkFilename: "[id].[contenthash].worker.js", }, }, ], @@ -280,9 +280,9 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { - inline: 'fallback', + inline: "fallback", }, }, ], @@ -307,7 +307,7 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { esModule: false, }, @@ -326,7 +326,7 @@ The worker file can import dependencies just like any other file: **index.js** ```js -import Worker from './my.worker.js'; +import Worker from "./my.worker.js"; var worker = new Worker(); @@ -334,8 +334,8 @@ var result; worker.onmessage = function (event) { if (!result) { - result = document.createElement('div'); - result.setAttribute('id', 'result'); + result = document.createElement("div"); + result.setAttribute("id", "result"); document.body.append(result); } @@ -343,9 +343,9 @@ worker.onmessage = function (event) { result.innerText = JSON.stringify(event.data); }; -const button = document.getElementById('button'); +const button = document.getElementById("button"); -button.addEventListener('click', function () { +button.addEventListener("click", function () { worker.postMessage({ postMessage: true }); }); ``` @@ -370,7 +370,7 @@ module.exports = { rules: [ { test: /\.worker\.(c|m)?js$/i, - loader: 'worker-loader', + loader: "worker-loader", options: { esModule: false, }, @@ -387,7 +387,7 @@ You can even use ES6+ features if you have the [`babel-loader`](https://github.c **index.js** ```js -import Worker from './my.worker.js'; +import Worker from "./my.worker.js"; const worker = new Worker(); @@ -395,8 +395,8 @@ let result; worker.onmessage = (event) => { if (!result) { - result = document.createElement('div'); - result.setAttribute('id', 'result'); + result = document.createElement("div"); + result.setAttribute("id", "result"); document.body.append(result); } @@ -404,9 +404,9 @@ worker.onmessage = (event) => { result.innerText = JSON.stringify(event.data); }; -const button = document.getElementById('button'); +const button = document.getElementById("button"); -button.addEventListener('click', () => { +button.addEventListener("click", () => { worker.postMessage({ postMessage: true }); }); ``` @@ -433,12 +433,12 @@ module.exports = { test: /\.worker\.(c|m)?js$/i, use: [ { - loader: 'worker-loader', + loader: "worker-loader", }, { - loader: 'babel-loader', + loader: "babel-loader", options: { - presets: ['@babel/preset-env'], + presets: ["@babel/preset-env"], }, }, ], @@ -455,7 +455,7 @@ To integrate with TypeScript, you will need to define a custom module for the ex **typings/worker-loader.d.ts** ```typescript -declare module 'worker-loader!*' { +declare module "worker-loader!*" { // You need to change `Worker`, if you specified a different value for the `workerType` option class WebpackWorker extends Worker { constructor(); @@ -473,23 +473,23 @@ declare module 'worker-loader!*' { const ctx: Worker = self as any; // Post data to parent thread -ctx.postMessage({ foo: 'foo' }); +ctx.postMessage({ foo: "foo" }); // Respond to message from parent thread -ctx.addEventListener('message', (event) => console.log(event)); +ctx.addEventListener("message", (event) => console.log(event)); ``` **index.ts** ```typescript -import Worker from 'worker-loader!./Worker'; +import Worker from "worker-loader!./Worker"; const worker = new Worker(); worker.postMessage({ a: 1 }); worker.onmessage = (event) => {}; -worker.addEventListener('message', (event) => {}); +worker.addEventListener("message", (event) => {}); ``` ### Cross-Origin Policy @@ -505,7 +505,7 @@ Firstly, you can inline the worker as a blob instead of downloading it as an ext **App.js** ```js -import Worker from './file.worker.js'; +import Worker from "./file.worker.js"; ``` **webpack.config.js** @@ -515,8 +515,8 @@ module.exports = { module: { rules: [ { - loader: 'worker-loader', - options: { inline: 'fallback' }, + loader: "worker-loader", + options: { inline: "fallback" }, }, ], }, @@ -529,7 +529,7 @@ Secondly, you may override the base download URL for your worker script via the ```js // This will cause the worker to be downloaded from `/workers/file.worker.js` -import Worker from './file.worker.js'; +import Worker from "./file.worker.js"; ``` **webpack.config.js** @@ -539,8 +539,8 @@ module.exports = { module: { rules: [ { - loader: 'worker-loader', - options: { publicPath: '/workers/' }, + loader: "worker-loader", + options: { publicPath: "/workers/" }, }, ], }, diff --git a/babel.config.js b/babel.config.js index 2cfa79c..baa26c5 100644 --- a/babel.config.js +++ b/babel.config.js @@ -7,23 +7,23 @@ module.exports = (api) => { return { presets: [ [ - '@babel/preset-env', + "@babel/preset-env", { targets: { - node: '10.13.0', + node: "10.13.0", }, }, ], ], overrides: [ { - test: './src/runtime', + test: "./src/runtime", presets: [ [ - '@babel/preset-env', + "@babel/preset-env", { targets: { - node: '0.12', + node: "0.12", }, }, ], diff --git a/commitlint.config.js b/commitlint.config.js index 84dcb12..69b4242 100644 --- a/commitlint.config.js +++ b/commitlint.config.js @@ -1,3 +1,3 @@ module.exports = { - extends: ['@commitlint/config-conventional'], + extends: ["@commitlint/config-conventional"], }; diff --git a/globalSetupTest.js b/globalSetupTest.js index 1fd91d4..6796091 100644 --- a/globalSetupTest.js +++ b/globalSetupTest.js @@ -1,7 +1,7 @@ -import path from 'path'; +import path from "path"; // eslint-disable-next-line import/no-extraneous-dependencies -import del from 'del'; +import del from "del"; async function setup() { await del(path.resolve(__dirname, `./test/outputs`)); diff --git a/husky.config.js b/husky.config.js index 4c2ec3a..6cf9b3f 100644 --- a/husky.config.js +++ b/husky.config.js @@ -1,6 +1,6 @@ module.exports = { hooks: { - 'pre-commit': 'lint-staged', - 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS', + "pre-commit": "lint-staged", + "commit-msg": "commitlint -E HUSKY_GIT_PARAMS", }, }; diff --git a/jest.config.js b/jest.config.js index 3ca5bb0..3cf688a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - testEnvironment: 'node', - globalSetup: '/globalSetupTest.js', - setupFilesAfterEnv: ['/setupTest.js'], + testEnvironment: "node", + globalSetup: "/globalSetupTest.js", + setupFilesAfterEnv: ["/setupTest.js"], }; diff --git a/lint-staged.config.js b/lint-staged.config.js index c417cb1..dc1bf51 100644 --- a/lint-staged.config.js +++ b/lint-staged.config.js @@ -1,4 +1,4 @@ module.exports = { - '*.js': ['prettier --write', 'eslint --fix'], - '*.{json,md,yml,css,ts}': ['prettier --write'], + "*.js": ["eslint --fix", "prettier --write"], + "*.{json,md,yml,css,ts}": ["prettier --write"], }; diff --git a/src/cjs.js b/src/cjs.js index 8b8e60f..95d63be 100644 --- a/src/cjs.js +++ b/src/cjs.js @@ -1,4 +1,4 @@ -const loader = require('./index'); +const loader = require("./index"); module.exports = loader.default; module.exports.pitch = loader.pitch; diff --git a/src/index.js b/src/index.js index 7bec930..ce92fb1 100644 --- a/src/index.js +++ b/src/index.js @@ -1,37 +1,37 @@ -import path from 'path'; +import path from "path"; -import { getOptions } from 'loader-utils'; -import { validate } from 'schema-utils'; +import { getOptions } from "loader-utils"; +import { validate } from "schema-utils"; -import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin'; -import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin'; -import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin'; -import ExternalsPlugin from 'webpack/lib/ExternalsPlugin'; +import NodeTargetPlugin from "webpack/lib/node/NodeTargetPlugin"; +import SingleEntryPlugin from "webpack/lib/SingleEntryPlugin"; +import WebWorkerTemplatePlugin from "webpack/lib/webworker/WebWorkerTemplatePlugin"; +import ExternalsPlugin from "webpack/lib/ExternalsPlugin"; -import schema from './options.json'; -import supportWebpack5 from './supportWebpack5'; -import supportWebpack4 from './supportWebpack4'; +import schema from "./options.json"; +import supportWebpack5 from "./supportWebpack5"; +import supportWebpack4 from "./supportWebpack4"; import { getDefaultFilename, getDefaultChunkFilename, getExternalsType, -} from './utils'; +} from "./utils"; let FetchCompileWasmPlugin; let FetchCompileAsyncWasmPlugin; // determine the version of webpack peer dependency // eslint-disable-next-line global-require, import/no-unresolved -const useWebpack5 = require('webpack/package.json').version.startsWith('5.'); +const useWebpack5 = require("webpack/package.json").version.startsWith("5."); if (useWebpack5) { // eslint-disable-next-line global-require, import/no-unresolved - FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmPlugin'); + FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmPlugin"); // eslint-disable-next-line global-require, import/no-unresolved - FetchCompileAsyncWasmPlugin = require('webpack/lib/web/FetchCompileAsyncWasmPlugin'); + FetchCompileAsyncWasmPlugin = require("webpack/lib/web/FetchCompileAsyncWasmPlugin"); } else { // eslint-disable-next-line global-require, import/no-unresolved - FetchCompileWasmPlugin = require('webpack/lib/web/FetchCompileWasmTemplatePlugin'); + FetchCompileWasmPlugin = require("webpack/lib/web/FetchCompileWasmTemplatePlugin"); } export default function loader() {} @@ -42,8 +42,8 @@ export function pitch(request) { const options = getOptions(this); validate(schema, options, { - name: 'Worker Loader', - baseDataPath: 'options', + name: "Worker Loader", + baseDataPath: "options", }); const workerContext = {}; @@ -62,7 +62,7 @@ export function pitch(request) { filename, chunkFilename, publicPath, - globalObject: 'self', + globalObject: "self", }; workerContext.compiler = this._compilation.createChildCompiler( @@ -72,7 +72,7 @@ export function pitch(request) { new WebWorkerTemplatePlugin().apply(workerContext.compiler); - if (this.target !== 'webworker' && this.target !== 'web') { + if (this.target !== "webworker" && this.target !== "web") { new NodeTargetPlugin().apply(workerContext.compiler); } @@ -105,7 +105,7 @@ export function pitch(request) { if ( workerContext.compiler.cache && - typeof workerContext.compiler.cache.get === 'function' + typeof workerContext.compiler.cache.get === "function" ) { supportWebpack5(this, workerContext, options, cb); } else { diff --git a/src/runtime/inline.js b/src/runtime/inline.js index d1259c1..f114edd 100644 --- a/src/runtime/inline.js +++ b/src/runtime/inline.js @@ -39,7 +39,7 @@ module.exports = (content, workerConstructor, workerOptions, url) => { } } catch (e) { if (!url) { - throw Error('Inline worker is not supported'); + throw Error("Inline worker is not supported"); } return new window[workerConstructor](url, workerOptions); diff --git a/src/supportWebpack4.js b/src/supportWebpack4.js index 508beeb..3ad792c 100644 --- a/src/supportWebpack4.js +++ b/src/supportWebpack4.js @@ -2,7 +2,7 @@ import { workerGenerator, sourceMappingURLRegex, sourceURLWebpackRegex, -} from './utils'; +} from "./utils"; export default function runAsChild( loaderContext, @@ -21,7 +21,7 @@ export default function runAsChild( let workerSource = compilation.assets[workerFilename].source(); - if (options.inline === 'no-fallback') { + if (options.inline === "no-fallback") { // eslint-disable-next-line no-underscore-dangle, no-param-reassign delete loaderContext._compilation.assets[workerFilename]; @@ -33,10 +33,10 @@ export default function runAsChild( } // Remove `/* sourceMappingURL=url */` comment - workerSource = workerSource.replace(sourceMappingURLRegex, ''); + workerSource = workerSource.replace(sourceMappingURLRegex, ""); // Remove `//# sourceURL=webpack-internal` comment - workerSource = workerSource.replace(sourceURLWebpackRegex, ''); + workerSource = workerSource.replace(sourceURLWebpackRegex, ""); } const workerCode = workerGenerator( diff --git a/src/supportWebpack5.js b/src/supportWebpack5.js index c2a6725..a2a9f61 100644 --- a/src/supportWebpack5.js +++ b/src/supportWebpack5.js @@ -2,7 +2,7 @@ import { workerGenerator, sourceMappingURLRegex, sourceURLWebpackRegex, -} from './utils'; +} from "./utils"; export default function runAsChild( loaderContext, @@ -17,7 +17,7 @@ export default function runAsChild( if (entries[0]) { const [workerFilename] = [...entries[0].files]; - const cache = workerContext.compiler.getCache('worker-loader'); + const cache = workerContext.compiler.getCache("worker-loader"); const cacheIdent = workerFilename; const cacheETag = cache.getLazyHashedEtag( compilation.assets[workerFilename] @@ -28,7 +28,7 @@ export default function runAsChild( return callback(getCacheError); } - if (options.inline === 'no-fallback') { + if (options.inline === "no-fallback") { // eslint-disable-next-line no-underscore-dangle, no-param-reassign delete loaderContext._compilation.assets[workerFilename]; @@ -46,12 +46,12 @@ export default function runAsChild( let workerSource = compilation.assets[workerFilename].source(); - if (options.inline === 'no-fallback') { + if (options.inline === "no-fallback") { // Remove `/* sourceMappingURL=url */` comment - workerSource = workerSource.replace(sourceMappingURLRegex, ''); + workerSource = workerSource.replace(sourceMappingURLRegex, ""); // Remove `//# sourceURL=webpack-internal` comment - workerSource = workerSource.replace(sourceURLWebpackRegex, ''); + workerSource = workerSource.replace(sourceURLWebpackRegex, ""); } const workerCode = workerGenerator( diff --git a/src/utils.js b/src/utils.js index 9c7d2a5..bcd6cbf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,15 +1,15 @@ -import { stringifyRequest } from 'loader-utils'; +import { stringifyRequest } from "loader-utils"; function getDefaultFilename(filename) { - if (typeof filename === 'function') { + if (typeof filename === "function") { return filename; } - return filename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2'); + return filename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2"); } function getDefaultChunkFilename(chunkFilename) { - return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2'); + return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, ".worker.$1$2"); } function getExternalsType(compilerOptions) { @@ -28,37 +28,37 @@ function getExternalsType(compilerOptions) { } if (compilerOptions.output.module) { - return 'module'; + return "module"; } - return 'var'; + return "var"; } function workerGenerator(loaderContext, workerFilename, workerSource, options) { let workerConstructor; let workerOptions; - if (typeof options.worker === 'undefined') { - workerConstructor = 'Worker'; - } else if (typeof options.worker === 'string') { + if (typeof options.worker === "undefined") { + workerConstructor = "Worker"; + } else if (typeof options.worker === "string") { workerConstructor = options.worker; } else { ({ type: workerConstructor, options: workerOptions } = options.worker); } const esModule = - typeof options.esModule !== 'undefined' ? options.esModule : true; + typeof options.esModule !== "undefined" ? options.esModule : true; const fnName = `${workerConstructor}_fn`; if (options.inline) { const InlineWorkerPath = stringifyRequest( loaderContext, - `!!${require.resolve('./runtime/inline.js')}` + `!!${require.resolve("./runtime/inline.js")}` ); let fallbackWorkerPath; - if (options.inline === 'fallback') { + if (options.inline === "fallback") { fallbackWorkerPath = `__webpack_public_path__ + ${JSON.stringify( workerFilename )}`; @@ -72,7 +72,7 @@ ${ } ${ - esModule ? 'export default' : 'module.exports =' + esModule ? "export default" : "module.exports =" } function ${fnName}() {\n return worker(${JSON.stringify( workerSource )}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify( @@ -81,10 +81,10 @@ ${ } return `${ - esModule ? 'export default' : 'module.exports =' + esModule ? "export default" : "module.exports =" } function ${fnName}() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify( workerFilename - )}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''});\n}\n`; + )}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ""});\n}\n`; } // Matches only the last occurrence of sourceMappingURL @@ -92,24 +92,24 @@ const innerRegex = /\s*[#@]\s*sourceMappingURL\s*=\s*(.*?(?=[\s'"]|\\n|\*\/|$)(? /* eslint-disable prefer-template */ const sourceMappingURLRegex = RegExp( - '(?:' + - '/\\*' + - '(?:\\s*\r?\n(?://)?)?' + - '(?:' + + "(?:" + + "/\\*" + + "(?:\\s*\r?\n(?://)?)?" + + "(?:" + innerRegex.source + - ')' + - '\\s*' + - '\\*/' + - '|' + - '//(?:' + + ")" + + "\\s*" + + "\\*/" + + "|" + + "//(?:" + innerRegex.source + - ')' + - ')' + - '\\s*' + ")" + + ")" + + "\\s*" ); const sourceURLWebpackRegex = RegExp( - '\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n' + "\\/\\/#\\ssourceURL=webpack-internal:\\/\\/\\/(.*?)\\\\n" ); /* eslint-enable prefer-template */ diff --git a/test/chunkFilename-option.test.js b/test/chunkFilename-option.test.js index 6dcd708..54f800f 100644 --- a/test/chunkFilename-option.test.js +++ b/test/chunkFilename-option.test.js @@ -1,6 +1,6 @@ -import path from 'path'; +import path from "path"; -import { customAlphabet } from 'nanoid'; +import { customAlphabet } from "nanoid"; import { compile, @@ -9,34 +9,34 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"name" option', () => { it('should work ("string")', async () => { - const compiler = getCompiler('./chunks/entry.js', { - chunkFilename: 'test.worker.chunk.js', + const compiler = getCompiler("./chunks/entry.js", { + chunkFilename: "test.worker.chunk.js", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.chunkFilename" default value option', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].js', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].js", + publicPath: "", }, module: { rules: [ @@ -44,7 +44,7 @@ describe('"name" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -55,25 +55,25 @@ describe('"name" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.chunkFilename" option ("string")', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].js', - chunkFilename: '[name].chunk.js', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].js", + chunkFilename: "[name].chunk.js", + publicPath: "", }, module: { rules: [ @@ -81,7 +81,7 @@ describe('"name" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -92,25 +92,25 @@ describe('"name" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - it('should chunkFilename suffix be inserted before query parameters', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + it("should chunkFilename suffix be inserted before query parameters", async () => { + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].js', - chunkFilename: '[name].chunk.js?foo=bar&baz=bar', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].js", + chunkFilename: "[name].chunk.js?foo=bar&baz=bar", + publicPath: "", }, module: { rules: [ @@ -118,7 +118,7 @@ describe('"name" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -132,18 +132,18 @@ describe('"name" option', () => { let hasChankName = false; Object.keys(stats.compilation.assets).forEach((asset) => { - if (asset.endsWith('chunk.worker.js?foo=bar&baz=bar')) { + if (asset.endsWith("chunk.worker.js?foo=bar&baz=bar")) { hasChankName = true; } }); expect(hasChankName).toBe(true); - expect(result).toMatchSnapshot('result'); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(result).toMatchSnapshot("result"); + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/cjs.test.js b/test/cjs.test.js index 8aba6ba..dc8321a 100644 --- a/test/cjs.test.js +++ b/test/cjs.test.js @@ -1,8 +1,8 @@ -import src from '../src'; -import cjs from '../src/cjs'; +import src from "../src"; +import cjs from "../src/cjs"; -describe('cjs', () => { - it('should exported', () => { +describe("cjs", () => { + it("should exported", () => { expect(cjs).toEqual(src); }); }); diff --git a/test/esModule-option.test.js b/test/esModule-option.test.js index 84b2746..243c748 100644 --- a/test/esModule-option.test.js +++ b/test/esModule-option.test.js @@ -5,49 +5,49 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"esModule" option', () => { - it('should work and generate ES module syntax by default', async () => { - const compiler = getCompiler('./basic/entry.js'); + it("should work and generate ES module syntax by default", async () => { + const compiler = getCompiler("./basic/entry.js"); const stats = await compile(compiler); // const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); // expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "true" value', async () => { - const compiler = getCompiler('./basic/entry.js', { + const compiler = getCompiler("./basic/entry.js", { esModule: true, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "false" value', async () => { - const compiler = getCompiler('./basic/entry.js', { + const compiler = getCompiler("./basic/entry.js", { esModule: false, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/filename-options.test.js b/test/filename-options.test.js index a44cf0c..0d3205c 100644 --- a/test/filename-options.test.js +++ b/test/filename-options.test.js @@ -1,6 +1,6 @@ -import path from 'path'; +import path from "path"; -import { customAlphabet } from 'nanoid'; +import { customAlphabet } from "nanoid"; import { compile, @@ -9,49 +9,49 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"filename" option', () => { it('should work ("string")', async () => { - const compiler = getCompiler('./chunks/entry.js', { - filename: '[name].custom.worker.js', + const compiler = getCompiler("./chunks/entry.js", { + filename: "[name].custom.worker.js", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work ("function")', async () => { - const compiler = getCompiler('./chunks/entry.js', { - filename: () => '[name].custom.worker.js', + const compiler = getCompiler("./chunks/entry.js", { + filename: () => "[name].custom.worker.js", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.filename" default value option', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - chunkFilename: '[name].chunk.js', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + chunkFilename: "[name].chunk.js", + publicPath: "", }, module: { rules: [ @@ -59,7 +59,7 @@ describe('"filename" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -70,25 +70,25 @@ describe('"filename" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.filename" option ("string")', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].custom.js', - chunkFilename: '[name].chunk.js', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].custom.js", + chunkFilename: "[name].chunk.js", + publicPath: "", }, module: { rules: [ @@ -96,7 +96,7 @@ describe('"filename" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -107,30 +107,30 @@ describe('"filename" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.filename" option ("function")', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), filename: (pathData) => { if (/worker\.js$/.test(pathData.chunk.entryModule.resource)) { - return '[name].custom.worker.js'; + return "[name].custom.worker.js"; } - return '[name].js'; + return "[name].js"; }, - publicPath: '', + publicPath: "", }, module: { rules: [ @@ -138,7 +138,7 @@ describe('"filename" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../src'), + loader: path.resolve(__dirname, "../src"), }, ], }, @@ -149,11 +149,11 @@ describe('"filename" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/helpers/execute.js b/test/helpers/execute.js index 866001a..7e1a94e 100644 --- a/test/helpers/execute.js +++ b/test/helpers/execute.js @@ -1,14 +1,14 @@ -import Module from 'module'; -import path from 'path'; +import Module from "module"; +import path from "path"; const parentModule = module; export default (code) => { - const resource = 'test.js'; + const resource = "test.js"; const module = new Module(resource, parentModule); // eslint-disable-next-line no-underscore-dangle module.paths = Module._nodeModulePaths( - path.resolve(__dirname, '../fixtures') + path.resolve(__dirname, "../fixtures") ); module.filename = resource; diff --git a/test/helpers/getCompiler.js b/test/helpers/getCompiler.js index 894748a..51dafe7 100644 --- a/test/helpers/getCompiler.js +++ b/test/helpers/getCompiler.js @@ -1,22 +1,22 @@ -import path from 'path'; +import path from "path"; -import webpack from 'webpack'; -import { customAlphabet } from 'nanoid'; -import HtmlWebpackPlugin from 'html-webpack-plugin'; +import webpack from "webpack"; +import { customAlphabet } from "nanoid"; +import HtmlWebpackPlugin from "html-webpack-plugin"; export default (fixture, loaderOptions = {}, config = {}) => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const fullConfig = { - mode: 'development', + mode: "development", devtool: config.devtool || false, - context: path.resolve(__dirname, '../fixtures'), - entry: path.resolve(__dirname, '../fixtures', fixture), + context: path.resolve(__dirname, "../fixtures"), + entry: path.resolve(__dirname, "../fixtures", fixture), output: { - path: path.resolve(__dirname, '../outputs', `test_${nanoid()}`), - filename: '[name].bundle.js', - chunkFilename: '[name].chunk.js', - publicPath: '', + path: path.resolve(__dirname, "../outputs", `test_${nanoid()}`), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "", }, module: { rules: [ @@ -24,8 +24,8 @@ export default (fixture, loaderOptions = {}, config = {}) => { test: /(worker|TypeDetection)\.js$/i, rules: [ { - loader: path.resolve(__dirname, '../../src'), - options: { filename: 'test.worker.js', ...loaderOptions }, + loader: path.resolve(__dirname, "../../src"), + options: { filename: "test.worker.js", ...loaderOptions }, }, ], }, @@ -35,16 +35,16 @@ export default (fixture, loaderOptions = {}, config = {}) => { new HtmlWebpackPlugin({ template: path.resolve( __dirname, - '../fixtures', + "../fixtures", path.dirname(fixture), - 'index.html' + "index.html" ), }), ], ...config, }; - if (webpack.version[0] === '5') { + if (webpack.version[0] === "5") { if (!fullConfig.experiments) { fullConfig.experiments = {}; } diff --git a/test/helpers/getErrors.js b/test/helpers/getErrors.js index 716fbbb..552233e 100644 --- a/test/helpers/getErrors.js +++ b/test/helpers/getErrors.js @@ -1,4 +1,4 @@ -import normalizeErrors from './normalizeErrors'; +import normalizeErrors from "./normalizeErrors"; export default (stats) => { return normalizeErrors(stats.compilation.errors); diff --git a/test/helpers/getResultFromBrowser.js b/test/helpers/getResultFromBrowser.js index 64771b1..2208c14 100644 --- a/test/helpers/getResultFromBrowser.js +++ b/test/helpers/getResultFromBrowser.js @@ -1,8 +1,8 @@ -import path from 'path'; +import path from "path"; -import getPort from 'get-port'; -import express from 'express'; -import puppeteer from 'puppeteer'; +import getPort from "get-port"; +import express from "express"; +import puppeteer from "puppeteer"; export default async function getResultFromBrowser(stats) { const assets = Object.entries(stats.compilation.assets); @@ -11,22 +11,22 @@ export default async function getResultFromBrowser(stats) { const server = app.listen(port); app.use( - '/public-path-static', + "/public-path-static", express.static(stats.compilation.outputOptions.path) ); app.use( - '/public-path-static-other', + "/public-path-static-other", express.static(stats.compilation.outputOptions.path) ); for (const asset of assets) { let [route] = asset; - [route] = route.split('?'); + [route] = route.split("?"); const existsAt = path.resolve(stats.compilation.outputOptions.path, route); - if (route === 'index.html') { - app.get('/', (req, res) => { + if (route === "index.html") { + app.get("/", (req, res) => { res.sendFile(existsAt); }); } else { @@ -40,11 +40,11 @@ export default async function getResultFromBrowser(stats) { const page = await browser.newPage(); page - .on('console', (message) => + .on("console", (message) => // eslint-disable-next-line no-console console.log(message) ) - .on('pageerror', ({ message }) => + .on("pageerror", ({ message }) => // eslint-disable-next-line no-console console.log(message) ) @@ -52,18 +52,18 @@ export default async function getResultFromBrowser(stats) { // // eslint-disable-next-line no-console // console.log(`${response.status()} ${response.url()}`) // ) - .on('requestfailed', (request) => + .on("requestfailed", (request) => // eslint-disable-next-line no-console console.log(`${request.failure().errorText} ${request.url()}`) ); await page.goto(`http://127.0.0.1:${port}/`); - await page.waitForSelector('button', { timeout: 90000 }); - await page.click('button'); - await page.waitForSelector('#result', { timeout: 90000 }); + await page.waitForSelector("button", { timeout: 90000 }); + await page.click("button"); + await page.waitForSelector("#result", { timeout: 90000 }); const addedFromWorkerText = await page.$eval( - '#result', + "#result", (el) => el.textContent ); diff --git a/test/helpers/getWarnings.js b/test/helpers/getWarnings.js index c8a09d6..458ca5a 100644 --- a/test/helpers/getWarnings.js +++ b/test/helpers/getWarnings.js @@ -1,4 +1,4 @@ -import normalizeErrors from './normalizeErrors'; +import normalizeErrors from "./normalizeErrors"; export default (stats) => { return normalizeErrors(stats.compilation.warnings); diff --git a/test/helpers/index.js b/test/helpers/index.js index 2a7837e..915ba43 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,13 +1,13 @@ -import compile from './compile'; -import execute from './execute'; -import getCompiler from './getCompiler'; -import getErrors from './getErrors'; -import getModuleSource from './getModuleSource'; -import getResultFromBrowser from './getResultFromBrowser'; -import getWarnings from './getWarnings'; -import normalizeErrors from './normalizeErrors'; -import readAsset from './readAsset'; -import readsAssets from './readAssets'; +import compile from "./compile"; +import execute from "./execute"; +import getCompiler from "./getCompiler"; +import getErrors from "./getErrors"; +import getModuleSource from "./getModuleSource"; +import getResultFromBrowser from "./getResultFromBrowser"; +import getWarnings from "./getWarnings"; +import normalizeErrors from "./normalizeErrors"; +import readAsset from "./readAsset"; +import readsAssets from "./readAssets"; export { compile, diff --git a/test/helpers/normalizeErrors.js b/test/helpers/normalizeErrors.js index c6d0f16..f1c380e 100644 --- a/test/helpers/normalizeErrors.js +++ b/test/helpers/normalizeErrors.js @@ -1,19 +1,19 @@ function removeCWD(str) { - const isWin = process.platform === 'win32'; + const isWin = process.platform === "win32"; let cwd = process.cwd(); if (isWin) { // eslint-disable-next-line no-param-reassign - str = str.replace(/\\/g, '/'); + str = str.replace(/\\/g, "/"); // eslint-disable-next-line no-param-reassign - cwd = cwd.replace(/\\/g, '/'); + cwd = cwd.replace(/\\/g, "/"); } - return str.replace(new RegExp(cwd, 'g'), ''); + return str.replace(new RegExp(cwd, "g"), ""); } export default (errors) => { return errors.map((error) => - removeCWD(error.toString().split('\n').slice(0, 2).join('\n')) + removeCWD(error.toString().split("\n").slice(0, 2).join("\n")) ); }; diff --git a/test/helpers/readAsset.js b/test/helpers/readAsset.js index 8f4699f..fde3ff3 100644 --- a/test/helpers/readAsset.js +++ b/test/helpers/readAsset.js @@ -1,13 +1,13 @@ -import path from 'path'; +import path from "path"; export default (asset, compiler, stats) => { const usedFs = compiler.outputFileSystem; const outputPath = stats.compilation.outputOptions.path; - let data = ''; + let data = ""; let targetFile = asset; - const queryStringIdx = targetFile.indexOf('?'); + const queryStringIdx = targetFile.indexOf("?"); if (queryStringIdx >= 0) { targetFile = targetFile.substr(0, queryStringIdx); diff --git a/test/helpers/readAssets.js b/test/helpers/readAssets.js index a2fb783..15b9dca 100644 --- a/test/helpers/readAssets.js +++ b/test/helpers/readAssets.js @@ -1,4 +1,4 @@ -import readAsset from './readAsset'; +import readAsset from "./readAsset"; export default function readAssets(compiler, stats) { const assets = {}; diff --git a/test/inline-option.test.js b/test/inline-option.test.js index 1c5a0f0..c67f568 100644 --- a/test/inline-option.test.js +++ b/test/inline-option.test.js @@ -5,245 +5,245 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"inline" option', () => { - it('should not work by default', async () => { - const compiler = getCompiler('./basic/entry.js'); + it("should not work by default", async () => { + const compiler = getCompiler("./basic/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value', async () => { - const compiler = getCompiler('./basic/entry.js', { inline: 'no-fallback' }); + const compiler = getCompiler("./basic/entry.js", { inline: "no-fallback" }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value and the "devtool" option ("source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', - { inline: 'no-fallback' }, - { devtool: 'source-map' } + "./basic/entry.js", + { inline: "no-fallback" }, + { devtool: "source-map" } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); expect( moduleSource.indexOf('sourceMappingURL=test.worker.js.map"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(stats.compilation.assets["test.worker.js.map"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value and the "devtool" option ("eval-source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', - { inline: 'no-fallback' }, - { devtool: 'eval-source-map' } + "./basic/entry.js", + { inline: "no-fallback" }, + { devtool: "eval-source-map" } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); const sourceUrlInternalIndex = moduleSource.indexOf( - 'sourceURL=webpack-internal:///./basic/worker.js' + "sourceURL=webpack-internal:///./basic/worker.js" ); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); expect( moduleSource.indexOf( - 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + "sourceMappingURL=data:application/json;charset=utf-8;base64," ) === -1 ).toBe(true); expect(sourceUrlInternalIndex === -1).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(stats.compilation.assets["test.worker.js.map"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value and the "devtool" option ("source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', - { inline: 'no-fallback' }, + "./basic/entry.js", + { inline: "no-fallback" }, { devtool: false } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(stats.compilation.assets["test.worker.js.map"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "fallback" value', async () => { - const compiler = getCompiler('./basic/entry.js', { inline: 'fallback' }); + const compiler = getCompiler("./basic/entry.js", { inline: "fallback" }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "fallback" value and the "devtool" option ("source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', - { inline: 'fallback' }, - { devtool: 'source-map' } + "./basic/entry.js", + { inline: "fallback" }, + { devtool: "source-map" } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(stats.compilation.assets['test.worker.js.map']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(stats.compilation.assets["test.worker.js.map"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "fallback" value and the "devtool" option ("source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', - { inline: 'fallback' }, + "./basic/entry.js", + { inline: "fallback" }, { devtool: false } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(stats.compilation.assets["test.worker.js.map"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value and "esModule" with "false" value', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'no-fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "no-fallback", esModule: false, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "no-fallback" value and "esModule" with "true" value', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'no-fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "no-fallback", esModule: true, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "fallback" value and "esModule" with "false" value', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "fallback", esModule: false, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "fallback" value and "esModule" with "true" value', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "fallback", esModule: true, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/loader.test.js b/test/loader.test.js index b9efb8b..3bc972a 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -5,126 +5,126 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; -describe('worker-loader', () => { - it('should work', async () => { - const compiler = getCompiler('./basic/entry.js'); +describe("worker-loader", () => { + it("should work", async () => { + const compiler = getCompiler("./basic/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - it('should work with inline syntax', async () => { - const compiler = getCompiler('./query/entry.js'); + it("should work with inline syntax", async () => { + const compiler = getCompiler("./query/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./query/my-worker-name.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./query/my-worker-name.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - it('should work with WASM', async () => { - const compiler = getCompiler('./wasm/entry.js'); + it("should work with WASM", async () => { + const compiler = getCompiler("./wasm/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./wasm/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./wasm/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - it('should work with async chunks', async () => { - const compiler = getCompiler('./chunks/entry.js'); + it("should work with async chunks", async () => { + const compiler = getCompiler("./chunks/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work with "externals"', async () => { const compiler = getCompiler( - './external/entry.js', + "./external/entry.js", {}, { externals: { - 'my-custom-module': 'navigator', + "my-custom-module": "navigator", }, } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./external/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./external/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "devtool" option ("source-map" value)', async () => { const compiler = getCompiler( - './basic/entry.js', + "./basic/entry.js", {}, - { devtool: 'source-map' } + { devtool: "source-map" } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(stats.compilation.assets['test.worker.js.map']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js.map"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "devtool" option ("false" value)', async () => { - const compiler = getCompiler('./basic/entry.js', {}, { devtool: false }); + const compiler = getCompiler("./basic/entry.js", {}, { devtool: false }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(stats.compilation.assets['test.worker.js.map']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js.map"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); - it('should work and have the same base file name as the source files', async () => { - const compiler = getCompiler('./name/entry.js', { - filename: '[name].worker.js', + it("should work and have the same base file name as the source files", async () => { + const compiler = getCompiler("./name/entry.js", { + filename: "[name].worker.js", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./name/TypeDetection.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./name/TypeDetection.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/publicPath.test.js b/test/publicPath.test.js index a487957..e00cd0d 100644 --- a/test/publicPath.test.js +++ b/test/publicPath.test.js @@ -1,6 +1,6 @@ -import path from 'path'; +import path from "path"; -import { customAlphabet } from 'nanoid'; +import { customAlphabet } from "nanoid"; import { compile, @@ -9,39 +9,39 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"publicPath" option', () => { it('should work and use "__webpack_public_path__" by default', async () => { - const compiler = getCompiler('./chunks/entry.js'); + const compiler = getCompiler("./chunks/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "publicPath" option ("string")', async () => { - const compiler = getCompiler('./chunks/entry.js', { - publicPath: '/public-path-static/', + const compiler = getCompiler("./chunks/entry.js", { + publicPath: "/public-path-static/", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "publicPath" option ("function")', async () => { - const compiler = getCompiler('./chunks/entry.js', { + const compiler = getCompiler("./chunks/entry.js", { publicPath: () => { return `/public-path-static/`; }, @@ -49,93 +49,93 @@ describe('"publicPath" option', () => { const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.publicPath" option default value', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].bundle.js', - chunkFilename: '[name].chunk.js', - publicPath: '', + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", + publicPath: "", }, } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.publicPath" option value ("string")', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - publicPath: '/public-path-static/', - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].bundle.js', - chunkFilename: '[name].chunk.js', + publicPath: "/public-path-static/", + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", }, } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect the "output.publicPath" option value ("function")', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { output: { - publicPath: () => '/public-path-static/', - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: '[name].bundle.js', - chunkFilename: '[name].chunk.js', + publicPath: () => "/public-path-static/", + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "[name].bundle.js", + chunkFilename: "[name].chunk.js", }, } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect "filename" and "chunkFilename" option values', async () => { - const nanoid = customAlphabet('1234567890abcdef', 10); + const nanoid = customAlphabet("1234567890abcdef", 10); const compiler = getCompiler( - './chunks/entry.js', + "./chunks/entry.js", {}, { module: { @@ -144,45 +144,45 @@ describe('"publicPath" option', () => { test: /worker\.js$/i, rules: [ { - loader: path.resolve(__dirname, './../src'), + loader: path.resolve(__dirname, "./../src"), }, ], }, ], }, output: { - publicPath: '/public-path-static-other/', - path: path.resolve(__dirname, './outputs', `test_${nanoid()}`), - filename: 'other-static/js/[name].bundle.js', - chunkFilename: 'other-static/js/[name].chunk.js', + publicPath: "/public-path-static-other/", + path: path.resolve(__dirname, "./outputs", `test_${nanoid()}`), + filename: "other-static/js/[name].bundle.js", + chunkFilename: "other-static/js/[name].chunk.js", }, } ); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should work and respect "filename" and "chunkFilename" option values', async () => { - const compiler = getCompiler('./chunks/entry.js', { - publicPath: '/public-path-static-other/', - filename: 'other-static/js/[name].worker.js', - chunkFilename: 'other-static/js/[name].chunk.worker.js', + const compiler = getCompiler("./chunks/entry.js", { + publicPath: "/public-path-static-other/", + filename: "other-static/js/[name].worker.js", + chunkFilename: "other-static/js/[name].chunk.worker.js", }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./chunks/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./chunks/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); }); diff --git a/test/sourceMapperRegexp.test.js b/test/sourceMapperRegexp.test.js index ed3188a..548e26a 100644 --- a/test/sourceMapperRegexp.test.js +++ b/test/sourceMapperRegexp.test.js @@ -1,31 +1,31 @@ -import { sourceMappingURLRegex } from '../src/utils'; +import { sourceMappingURLRegex } from "../src/utils"; -describe('source-map-loader', () => { +describe("source-map-loader", () => { const cases = [ - '/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/', - '/* #sourceMappingURL=absolute-sourceRoot-source-map.map */', - '//#sourceMappingURL=absolute-sourceRoot-source-map.map', - '//@sourceMappingURL=absolute-sourceRoot-source-map.map', - ' // #sourceMappingURL=absolute-sourceRoot-source-map.map', - ' // # sourceMappingURL = absolute-sourceRoot-source-map.map ', - '// #sourceMappingURL = http://sampledomain.com/external-source-map2.map', - '// #sourceMappingURL = //sampledomain.com/external-source-map2.map', - '// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==', + "/*#sourceMappingURL=absolute-sourceRoot-source-map.map*/", + "/* #sourceMappingURL=absolute-sourceRoot-source-map.map */", + "//#sourceMappingURL=absolute-sourceRoot-source-map.map", + "//@sourceMappingURL=absolute-sourceRoot-source-map.map", + " // #sourceMappingURL=absolute-sourceRoot-source-map.map", + " // # sourceMappingURL = absolute-sourceRoot-source-map.map ", + "// #sourceMappingURL = http://sampledomain.com/external-source-map2.map", + "// #sourceMappingURL = //sampledomain.com/external-source-map2.map", + "// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==", ` with SourceMap // #sourceMappingURL = /sample-source-map.map // comment `, - 'onmessage = function(event) {\n const workerResult = event.data;\n\n workerResult.onmessage = true;\n\n postMessage(workerResult);\n};\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9iYXNpYy93b3JrZXIuanM/OGFiZiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBOztBQUVBOztBQUVBO0FBQ0EiLCJmaWxlIjoiLi9iYXNpYy93b3JrZXIuanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJvbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkge1xuICBjb25zdCB3b3JrZXJSZXN1bHQgPSBldmVudC5kYXRhO1xuXG4gIHdvcmtlclJlc3VsdC5vbm1lc3NhZ2UgPSB0cnVlO1xuXG4gIHBvc3RNZXNzYWdlKHdvcmtlclJlc3VsdCk7XG59O1xuIl0sInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///./basic/worker.js\n', + "onmessage = function(event) {\n const workerResult = event.data;\n\n workerResult.onmessage = true;\n\n postMessage(workerResult);\n};\n//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9iYXNpYy93b3JrZXIuanM/OGFiZiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBOztBQUVBOztBQUVBO0FBQ0EiLCJmaWxlIjoiLi9iYXNpYy93b3JrZXIuanMuanMiLCJzb3VyY2VzQ29udGVudCI6WyJvbm1lc3NhZ2UgPSBmdW5jdGlvbihldmVudCkge1xuICBjb25zdCB3b3JrZXJSZXN1bHQgPSBldmVudC5kYXRhO1xuXG4gIHdvcmtlclJlc3VsdC5vbm1lc3NhZ2UgPSB0cnVlO1xuXG4gIHBvc3RNZXNzYWdlKHdvcmtlclJlc3VsdCk7XG59O1xuIl0sInNvdXJjZVJvb3QiOiIifQ==\n//# sourceURL=webpack-internal:///./basic/worker.js\n", ]; cases.forEach((item) => { it(`should work with "${item}" url`, async () => { - const result = item.replace(sourceMappingURLRegex, 'REPLASED'); + const result = item.replace(sourceMappingURLRegex, "REPLASED"); - expect(result.indexOf('REPLASED') !== -1).toBe(true); - expect(result).toMatchSnapshot('result'); + expect(result.indexOf("REPLASED") !== -1).toBe(true); + expect(result).toMatchSnapshot("result"); }); }); }); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 5b7e6fe..29802c7 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -1,45 +1,45 @@ -import { getCompiler, compile } from './helpers/index'; +import { getCompiler, compile } from "./helpers/index"; -describe('validate options', () => { +describe("validate options", () => { const tests = { worker: { success: [ - 'Worker', - { type: 'Worker' }, - { type: 'Worker', options: { type: 'classic', name: 'worker-name' } }, + "Worker", + { type: "Worker" }, + { type: "Worker", options: { type: "classic", name: "worker-name" } }, ], failure: [true, []], }, publicPath: { - success: ['/assets/', () => '/assets/'], + success: ["/assets/", () => "/assets/"], failure: [true, []], }, filename: { - success: ['[name].worker.js', () => '[name].worker.js'], + success: ["[name].worker.js", () => "[name].worker.js"], failure: [true, []], }, chunkFilename: { - success: ['[name].chunk.worker.js'], + success: ["[name].chunk.worker.js"], failure: [true, []], }, inline: { - success: ['fallback', 'no-fallback'], - failure: [true, 'true'], + success: ["fallback", "no-fallback"], + failure: [true, "true"], }, esModule: { success: [true, false], - failure: ['true'], + failure: ["true"], }, unknown: { success: [], - failure: [1, true, false, 'test', /test/, [], {}, { foo: 'bar' }], + failure: [1, true, false, "test", /test/, [], {}, { foo: "bar" }], }, }; function stringifyValue(value) { if ( Array.isArray(value) || - (value && typeof value === 'object' && value.constructor === Object) + (value && typeof value === "object" && value.constructor === Object) ) { return JSON.stringify(value); } @@ -49,18 +49,18 @@ describe('validate options', () => { async function createTestCase(key, value, type) { it(`should ${ - type === 'success' ? 'successfully validate' : 'throw an error on' + type === "success" ? "successfully validate" : "throw an error on" } the "${key}" option with "${stringifyValue(value)}" value`, async () => { - const compiler = getCompiler('./basic/entry.js', { [key]: value }); + const compiler = getCompiler("./basic/entry.js", { [key]: value }); let stats; try { stats = await compile(compiler); } finally { - if (type === 'success') { + if (type === "success") { expect(stats.hasErrors()).toBe(false); - } else if (type === 'failure') { + } else if (type === "failure") { const { compilation: { errors }, } = stats; diff --git a/test/worker-option.test.js b/test/worker-option.test.js index e7599e2..ad4e099 100644 --- a/test/worker-option.test.js +++ b/test/worker-option.test.js @@ -5,105 +5,105 @@ import { getModuleSource, getResultFromBrowser, getWarnings, -} from './helpers'; +} from "./helpers"; describe('"workerType" option', () => { it('should use "Worker" by default', async () => { - const compiler = getCompiler('./basic/entry.js'); + const compiler = getCompiler("./basic/entry.js"); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should support the "Worker" string value', async () => { - const compiler = getCompiler('./basic/entry.js', { worker: 'Worker' }); + const compiler = getCompiler("./basic/entry.js", { worker: "Worker" }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should support the "Worker" object value', async () => { - const compiler = getCompiler('./basic/entry.js', { + const compiler = getCompiler("./basic/entry.js", { worker: { - type: 'Worker', + type: "Worker", options: { - type: 'classic', - name: 'worker-name', + type: "classic", + name: "worker-name", }, }, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - expect(getModuleSource('./basic/worker.js', stats)).toMatchSnapshot( - 'module' + expect(getModuleSource("./basic/worker.js", stats)).toMatchSnapshot( + "module" ); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should support the "Worker" object value for inline workers with fallback', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "fallback", worker: { - type: 'Worker', + type: "Worker", options: { - type: 'classic', - name: 'worker-name', + type: "classic", + name: "worker-name", }, }, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeDefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeDefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); it('should support the "Worker" object value for inline workers without fallback', async () => { - const compiler = getCompiler('./basic/entry.js', { - inline: 'no-fallback', + const compiler = getCompiler("./basic/entry.js", { + inline: "no-fallback", worker: { - type: 'Worker', + type: "Worker", options: { - type: 'classic', - name: 'worker-name', + type: "classic", + name: "worker-name", }, }, }); const stats = await compile(compiler); const result = await getResultFromBrowser(stats); - const moduleSource = getModuleSource('./basic/worker.js', stats); + const moduleSource = getModuleSource("./basic/worker.js", stats); - expect(moduleSource.indexOf('inline.js') > 0).toBe(true); + expect(moduleSource.indexOf("inline.js") > 0).toBe(true); expect( moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1 ).toBe(true); - expect(stats.compilation.assets['test.worker.js']).toBeUndefined(); - expect(result).toMatchSnapshot('result'); - expect(getWarnings(stats)).toMatchSnapshot('warnings'); - expect(getErrors(stats)).toMatchSnapshot('errors'); + expect(stats.compilation.assets["test.worker.js"]).toBeUndefined(); + expect(result).toMatchSnapshot("result"); + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); }); });