loader-plugin-raw.sys.mjs (1327B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { NetUtil } from "resource://gre/modules/NetUtil.sys.mjs"; 6 7 /** 8 * A function that can be used as part of a require hook for a 9 * loader.js Loader. 10 * This function handles "raw!" and "theme-loader!" requires. 11 * See also: https://github.com/webpack/raw-loader. 12 */ 13 export const requireRawId = function (id, require) { 14 const index = id.indexOf("!"); 15 const rawId = id.slice(index + 1); 16 let uri = require.resolve(rawId); 17 // If the original string did not end with ".js", then 18 // require.resolve might have added the suffix. We don't want to 19 // add a suffix for a raw load (if needed the caller can specify it 20 // manually), so remove it here. 21 if (!id.endsWith(".js") && uri.endsWith(".js")) { 22 uri = uri.slice(0, -3); 23 } 24 25 const stream = NetUtil.newChannel({ 26 uri: NetUtil.newURI(uri, "UTF-8"), 27 loadUsingSystemPrincipal: true, 28 }).open(); 29 30 const count = stream.available(); 31 const data = NetUtil.readInputStreamToString(stream, count, { 32 charset: "UTF-8", 33 }); 34 stream.close(); 35 36 // For the time being it doesn't seem worthwhile to cache the 37 // result here. 38 return data; 39 };