diff --git a/browser/components/aboutwelcome/karma.mc.config.js b/browser/components/aboutwelcome/karma.mc.config.js index daafcb76977a..e7f8bbb83b16 100644 --- a/browser/components/aboutwelcome/karma.mc.config.js +++ b/browser/components/aboutwelcome/karma.mc.config.js @@ -5,6 +5,7 @@ const path = require("path"); const webpack = require("webpack"); const { ResourceUriPlugin } = require("../../tools/resourceUriPlugin"); +const { MozSrcUriPlugin } = require("../../tools/mozsrcUriPlugin"); const PATHS = { // Where is the entry point for the unit tests? @@ -205,6 +206,9 @@ module.exports = function (config) { ], ], }), + new MozSrcUriPlugin({ + baseDir: path.join(__dirname, "..", "..", ".."), + }), new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development"), }), diff --git a/browser/components/asrouter/karma.mc.config.js b/browser/components/asrouter/karma.mc.config.js index 72fae93a948b..d1a82c80db07 100644 --- a/browser/components/asrouter/karma.mc.config.js +++ b/browser/components/asrouter/karma.mc.config.js @@ -5,6 +5,7 @@ const path = require("path"); const webpack = require("webpack"); const { ResourceUriPlugin } = require("../../tools/resourceUriPlugin"); +const { MozSrcUriPlugin } = require("../../tools/mozsrcUriPlugin"); const PATHS = { // Where is the entry point for the unit tests? @@ -124,6 +125,9 @@ module.exports = function (config) { ], ], }), + new MozSrcUriPlugin({ + baseDir: path.join(__dirname, "..", "..", ".."), + }), new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development"), }), diff --git a/browser/extensions/newtab/karma.mc.config.js b/browser/extensions/newtab/karma.mc.config.js index ea09c74d7d32..6e41f04c9b11 100644 --- a/browser/extensions/newtab/karma.mc.config.js +++ b/browser/extensions/newtab/karma.mc.config.js @@ -5,6 +5,7 @@ const path = require("path"); const webpack = require("webpack"); const { ResourceUriPlugin } = require("../../tools/resourceUriPlugin"); +const { MozSrcUriPlugin } = require("../../tools/mozsrcUriPlugin"); const PATHS = { // Where is the entry point for the unit tests? @@ -365,6 +366,10 @@ module.exports = function (config) { ], ], }), + new MozSrcUriPlugin({ + baseDir: path.join(__dirname, "..", "..", ".."), + }), + new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("development"), }), diff --git a/browser/extensions/newtab/webpack.system-addon.config.js b/browser/extensions/newtab/webpack.system-addon.config.js index 3e5cb56cb881..2aebd8cad278 100644 --- a/browser/extensions/newtab/webpack.system-addon.config.js +++ b/browser/extensions/newtab/webpack.system-addon.config.js @@ -5,6 +5,7 @@ const path = require("path"); const webpack = require("webpack"); const { ResourceUriPlugin } = require("../../tools/resourceUriPlugin"); +const { MozSrcUriPlugin } = require("../../tools/mozsrcUriPlugin"); const absolute = relPath => path.join(__dirname, relPath); @@ -33,6 +34,7 @@ module.exports = (env = {}) => ({ ], ], }), + new MozSrcUriPlugin({ baseDir: path.join(__dirname, "..", "..", "..") }), new webpack.BannerPlugin( `THIS FILE IS AUTO-GENERATED: ${path.basename(__filename)}` ), diff --git a/browser/tools/mozsrcUriPlugin.js b/browser/tools/mozsrcUriPlugin.js new file mode 100644 index 000000000000..6ffc94587c0e --- /dev/null +++ b/browser/tools/mozsrcUriPlugin.js @@ -0,0 +1,52 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// This plugin supports finding files with particular resource:// URIs +// and translating the uri into a relative filesytem path where the file may be +// found when running within the Karma / Mocha test framework. + +/* eslint-env node */ +const path = require("path"); + +module.exports = { + MozSrcUriPlugin: class MozSrcUriPlugin { + /** + * The base directory of the repository. + */ + #baseDir; + + /** + * @param {object} options + * Object passed during the instantiation of MozSrcUriPlugin + * @param {string} options.baseDir + * The base directory of the repository. + */ + constructor({ baseDir }) { + this.#baseDir = baseDir; + } + + apply(compiler) { + compiler.hooks.compilation.tap( + "MozSrcUriPlugin", + (compilation, { normalModuleFactory }) => { + normalModuleFactory.hooks.resolveForScheme + .for("moz-src") + .tap("MozSrcUriPlugin", resourceData => { + const url = new URL(resourceData.resource); + + // path.join() is necessary to normalize the path on Windows. + // Without it, the path may contain backslashes, resulting in + // different build output on Windows than on Unix systems. + const pathname = path.join(this.#baseDir, url.pathname); + resourceData.path = pathname; + resourceData.query = url.search; + resourceData.fragment = url.hash; + resourceData.resource = pathname + url.search + url.hash; + return true; + }); + } + ); + } + }, +};