Source code

Revision control

Other Tools

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