From 5c7af3bca3dc5a8cf8c37034df817c330f63c17c Mon Sep 17 00:00:00 2001 From: Ruben Barilani Date: Sun, 25 Oct 2020 02:07:06 +0200 Subject: [PATCH 1/4] test: RemoveSourceMapURLWebpackPlugin#testFile --- test/plugin.test.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/plugin.test.js diff --git a/test/plugin.test.js b/test/plugin.test.js new file mode 100644 index 0000000..171b2c3 --- /dev/null +++ b/test/plugin.test.js @@ -0,0 +1,33 @@ +const RemoveSourceMapURLWebpackPlugin = require("../index"); + +describe("RemoveSourceMapURLWebpackPlugin#testFile", () => { + const files = ["main.js", "foo.js", "bar.js", "john.svg"]; + const cases = [ + [{ test: /\.svg($|\?)/i }, ["john.svg"]], + [{ test: "main.js" }, ["main.js"]], + [ + { + test: (file) => { + return file === "main.js" || file === "bar.js"; + }, + }, + ["main.js", "bar.js"], + ], + ]; + + test.each(cases)( + "should return true for file that must be processed, user options: %o", + (options, expectedToMatch) => { + const plugin = new RemoveSourceMapURLWebpackPlugin(options); + const matched = files.filter((file) => plugin.testFile(file)); + + expect(matched.length).toEqual(expectedToMatch.length); + expect(matched).toEqual(expectedToMatch); + } + ); + + it("should throw if user's test option is invalid", () => { + const plugin = new RemoveSourceMapURLWebpackPlugin({test: 333}); + expect(() => plugin.testFile("do-not-matter")).toThrow(); + }); +}); From 2b69fe71ff32987c483ed1a912cff48ffac7d676 Mon Sep 17 00:00:00 2001 From: Ruben Barilani Date: Sun, 25 Oct 2020 02:48:52 +0200 Subject: [PATCH 2/4] refactor: add src folder --- .travis.yml | 2 +- index.js | 71 +--------------------------------- src/plugin.js | 74 ++++++++++++++++++++++++++++++++++++ {test => src}/plugin.test.js | 0 4 files changed, 76 insertions(+), 71 deletions(-) create mode 100644 src/plugin.js rename {test => src}/plugin.test.js (100%) diff --git a/.travis.yml b/.travis.yml index 0e25d29..595ef76 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,6 @@ language: node_js node_js: - 12 script: - - npm run test:coverage + - npm run test:coverage -- --verbose after_script: - npm run coveralls diff --git a/index.js b/index.js index 16aea54..0ae1e5f 100644 --- a/index.js +++ b/index.js @@ -1,74 +1,5 @@ "use strict"; -const { sources, Compilation } = require("webpack"); -const colors = require("colors/safe"); - -const RemoveSourceMapURLWebpackPlugin = function (opts) { - this.options = opts || {}; - this.options.test = this.options.test || /\.js($|\?)/i; -}; - -RemoveSourceMapURLWebpackPlugin.prototype.testFile = function (file) { - if (this.options.test instanceof RegExp) { - return this.options.test.test(file); - } - - if (typeof this.options.test === "string") { - return this.options.test === file; - } - - if (typeof this.options.test === "function") { - return this.options.test(file); - } - - throw new Error( - `remove-source-map-url: Invalid "test" option. May be a RegExp (tested against asset key), a string containing the key, a function(key): bool` - ); -}; - -RemoveSourceMapURLWebpackPlugin.prototype.processAssets = function (assets) { - return Object.keys(assets) - .filter((file) => this.testFile(file)) - .map((file) => { - const asset = assets[file]; - const source = asset - .source() - .replace(/# sourceMappingURL=(.+?\.map)/g, "# $1"); - - return { - file, - source: new sources.RawSource(source), - }; - }); -}; - -RemoveSourceMapURLWebpackPlugin.prototype.apply = function (compiler) { - compiler.hooks.compilation.tap("after-compile", (compilation) => { - compilation.hooks.processAssets.tap( - { - name: "RemoveSourceMapURLWebpackPlugin", - stage: Compilation.PROCESS_ASSETS_STAGE_DERIVED, - }, - (assets) => { - // process assets - const count = this.processAssets(assets).reduce( - (count, { file, source }) => { - // update asset for the current compilation - compilation.updateAsset(file, source); - return count + 1; - }, - 0 - ); - console.log( - colors.green( - `remove-source-map-url: ${count}/${ - Object.keys(assets).length - } asset(s) processed and updated` - ) - ); - } - ); - }); -}; +const RemoveSourceMapURLWebpackPlugin = require("./src/plugin"); module.exports = RemoveSourceMapURLWebpackPlugin; diff --git a/src/plugin.js b/src/plugin.js new file mode 100644 index 0000000..16aea54 --- /dev/null +++ b/src/plugin.js @@ -0,0 +1,74 @@ +"use strict"; + +const { sources, Compilation } = require("webpack"); +const colors = require("colors/safe"); + +const RemoveSourceMapURLWebpackPlugin = function (opts) { + this.options = opts || {}; + this.options.test = this.options.test || /\.js($|\?)/i; +}; + +RemoveSourceMapURLWebpackPlugin.prototype.testFile = function (file) { + if (this.options.test instanceof RegExp) { + return this.options.test.test(file); + } + + if (typeof this.options.test === "string") { + return this.options.test === file; + } + + if (typeof this.options.test === "function") { + return this.options.test(file); + } + + throw new Error( + `remove-source-map-url: Invalid "test" option. May be a RegExp (tested against asset key), a string containing the key, a function(key): bool` + ); +}; + +RemoveSourceMapURLWebpackPlugin.prototype.processAssets = function (assets) { + return Object.keys(assets) + .filter((file) => this.testFile(file)) + .map((file) => { + const asset = assets[file]; + const source = asset + .source() + .replace(/# sourceMappingURL=(.+?\.map)/g, "# $1"); + + return { + file, + source: new sources.RawSource(source), + }; + }); +}; + +RemoveSourceMapURLWebpackPlugin.prototype.apply = function (compiler) { + compiler.hooks.compilation.tap("after-compile", (compilation) => { + compilation.hooks.processAssets.tap( + { + name: "RemoveSourceMapURLWebpackPlugin", + stage: Compilation.PROCESS_ASSETS_STAGE_DERIVED, + }, + (assets) => { + // process assets + const count = this.processAssets(assets).reduce( + (count, { file, source }) => { + // update asset for the current compilation + compilation.updateAsset(file, source); + return count + 1; + }, + 0 + ); + console.log( + colors.green( + `remove-source-map-url: ${count}/${ + Object.keys(assets).length + } asset(s) processed and updated` + ) + ); + } + ); + }); +}; + +module.exports = RemoveSourceMapURLWebpackPlugin; diff --git a/test/plugin.test.js b/src/plugin.test.js similarity index 100% rename from test/plugin.test.js rename to src/plugin.test.js From 8bd77531a7f382227f23c883b6de535146e4e1e5 Mon Sep 17 00:00:00 2001 From: Ruben Barilani Date: Sun, 25 Oct 2020 02:03:39 +0100 Subject: [PATCH 3/4] test --- src/plugin.js | 1 + src/plugin.test.js | 43 +++++++++++++++++++++---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/plugin.js b/src/plugin.js index 16aea54..42ffde6 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -18,6 +18,7 @@ RemoveSourceMapURLWebpackPlugin.prototype.testFile = function (file) { } if (typeof this.options.test === "function") { + console.log("khkhhs"); return this.options.test(file); } diff --git a/src/plugin.test.js b/src/plugin.test.js index 171b2c3..eb728fa 100644 --- a/src/plugin.test.js +++ b/src/plugin.test.js @@ -1,33 +1,32 @@ const RemoveSourceMapURLWebpackPlugin = require("../index"); describe("RemoveSourceMapURLWebpackPlugin#testFile", () => { - const files = ["main.js", "foo.js", "bar.js", "john.svg"]; - const cases = [ - [{ test: /\.svg($|\?)/i }, ["john.svg"]], - [{ test: "main.js" }, ["main.js"]], - [ - { - test: (file) => { - return file === "main.js" || file === "bar.js"; - }, - }, - ["main.js", "bar.js"], - ], - ]; + it("should throw if user's test option is invalid", () => { + const plugin = new RemoveSourceMapURLWebpackPlugin({ test: 333 }); + expect(() => plugin.testFile("do-not-matter")).toThrow(); + }); - test.each(cases)( - "should return true for file that must be processed, user options: %o", - (options, expectedToMatch) => { + it(`should return true for file that must be processed`, () => { + const files = ["main.js", "foo.js", "bar.js", "john.svg"]; + const cases = [ + [undefined, ["main.js", "foo.js", "bar.js"]], + [{ test: /\.svg($|\?)/i }, ["john.svg"]], + [{ test: "main.js" }, ["main.js"]], + [ + { + test: (file) => { + return file === "main.js" || file === "bar.js"; + }, + }, + ["main.js", "bar.js"], + ], + ]; + cases.forEach(([options, expectedToMatch]) => { const plugin = new RemoveSourceMapURLWebpackPlugin(options); const matched = files.filter((file) => plugin.testFile(file)); expect(matched.length).toEqual(expectedToMatch.length); expect(matched).toEqual(expectedToMatch); - } - ); - - it("should throw if user's test option is invalid", () => { - const plugin = new RemoveSourceMapURLWebpackPlugin({test: 333}); - expect(() => plugin.testFile("do-not-matter")).toThrow(); + }); }); }); From 68374314823b5af06c99511e011d6b6a9394e627 Mon Sep 17 00:00:00 2001 From: Ruben Barilani Date: Sun, 25 Oct 2020 10:54:57 +0100 Subject: [PATCH 4/4] test output --- .travis.yml | 3 +++ src/plugin.js | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 595ef76..adfde3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,7 @@ node_js: script: - npm run test:coverage -- --verbose after_script: + - echo "------------------------------" + - cat coverage/coverage-final.json + - echo "------------------------------" - npm run coveralls diff --git a/src/plugin.js b/src/plugin.js index 42ffde6..16aea54 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -18,7 +18,6 @@ RemoveSourceMapURLWebpackPlugin.prototype.testFile = function (file) { } if (typeof this.options.test === "function") { - console.log("khkhhs"); return this.options.test(file); }