mozsrcUriPlugin.js (1797B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // This plugin supports finding files with particular resource:// URIs 6 // and translating the uri into a relative filesystem path where the file may be 7 // found when running within the Karma / Mocha test framework. 8 9 /* eslint-env node */ 10 const path = require("path"); 11 12 module.exports = { 13 MozSrcUriPlugin: class MozSrcUriPlugin { 14 /** 15 * The base directory of the repository. 16 */ 17 #baseDir; 18 19 /** 20 * @param {object} options 21 * Object passed during the instantiation of MozSrcUriPlugin 22 * @param {string} options.baseDir 23 * The base directory of the repository. 24 */ 25 constructor({ baseDir }) { 26 this.#baseDir = baseDir; 27 } 28 29 apply(compiler) { 30 compiler.hooks.compilation.tap( 31 "MozSrcUriPlugin", 32 (compilation, { normalModuleFactory }) => { 33 normalModuleFactory.hooks.resolveForScheme 34 .for("moz-src") 35 .tap("MozSrcUriPlugin", resourceData => { 36 const url = new URL(resourceData.resource); 37 38 // path.join() is necessary to normalize the path on Windows. 39 // Without it, the path may contain backslashes, resulting in 40 // different build output on Windows than on Unix systems. 41 const pathname = path.join(this.#baseDir, url.pathname); 42 resourceData.path = pathname; 43 resourceData.query = url.search; 44 resourceData.fragment = url.hash; 45 resourceData.resource = pathname + url.search + url.hash; 46 return true; 47 }); 48 } 49 ); 50 } 51 }, 52 };