Bug 1966764 - Add a moz-src scheme handler for webpack to allow newtab/asrouter/aboutwelcome tests to work with the new protocol. r=home-newtab-reviewers,omc-reviewers,mconley,pdahiya.

Differential Revision: https://phabricator.services.mozilla.com/D249737
This commit is contained in:
Mark Banner
2025-05-17 17:36:14 +00:00
committed by mbanner@mozilla.com
parent 2a18da0848
commit b71032b413
5 changed files with 67 additions and 0 deletions

View File

@@ -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"),
}),

View File

@@ -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"),
}),

View File

@@ -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"),
}),

View File

@@ -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)}`
),

View File

@@ -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;
});
}
);
}
},
};