Bug 1310702 - use webpack-like require.context in devtools l10n; r=jryans

MozReview-Commit-ID: 5noH0y0MgaK

--HG--
extra : rebase_source : e7a4507a89f467fa8143be1899cd5e6ba80aa6fe
This commit is contained in:
Tom Tromey 2016-10-20 11:00:28 -06:00
parent 1a9b2949bf
commit 92a5c63599
4 changed files with 54 additions and 3 deletions

View File

@ -905,6 +905,14 @@ const Require = iced(function Require(loader, requirer) {
return uri;
}
// This is like webpack's require.context. It returns a new require
// function that prepends the prefix to any requests.
require.context = prefix => {
return id => {
return require(prefix + id);
};
};
// Make `require.main === module` evaluate to true in main module scope.
require.main = loader.main === requirer ? requirer : undefined;
return iced(require);

View File

@ -20,7 +20,8 @@ var {getInplaceEditorForSpan: inplaceEditor} =
const ROOT_TEST_DIR = getRootDirectory(gTestPath);
const FRAME_SCRIPT_URL = ROOT_TEST_DIR + "doc_frame_script.js";
const STYLE_INSPECTOR_L10N = new LocalizationHelper("chrome://devtools-shared/locale/styleinspector.properties");
const STYLE_INSPECTOR_L10N
= new LocalizationHelper("devtools-shared/locale/styleinspector.properties");
registerCleanupFunction(() => {
Services.prefs.clearUserPref("devtools.defaultColorUnit");

View File

@ -21,7 +21,8 @@ const TEST_URL_ROOT_SSL =
"https://example.com/browser/devtools/client/inspector/shared/test/";
const ROOT_TEST_DIR = getRootDirectory(gTestPath);
const FRAME_SCRIPT_URL = ROOT_TEST_DIR + "doc_frame_script.js";
const STYLE_INSPECTOR_L10N = new LocalizationHelper("chrome://devtools-shared/locale/styleinspector.properties");
const STYLE_INSPECTOR_L10N =
new LocalizationHelper("devtools-shared/locale/styleinspector.properties");
// Clean-up all prefs that might have been changed during a test run
// (safer here because if the test fails, then the pref is never reverted)

View File

@ -8,6 +8,31 @@ const { sprintf } = require("devtools/shared/sprintfjs/sprintf");
const propertiesMap = {};
// We need some special treatment here for webpack.
//
// Webpack doesn't always handle dynamic requires in the best way. In
// particular if it sees an unrestricted dynamic require, it will try
// to put all the files it can find into the generated pack. (It can
// also try a bit to parse the expression passed to require, but in
// our case this doesn't work, because our call below doesn't provide
// enough information.)
//
// Webpack also provides a way around this: require.context. The idea
// here is to tell webpack some constraints so that it can include
// fewer files in the pack.
//
// Here we introduce new require contexts for each possible locale
// directory. Then we use the correct context to load the property
// file. In the webpack case this results in just the locale property
// files being included in the pack; and in the devtools case this is
// a wordy no-op.
const reqShared = require.context("raw!devtools-shared/locale/",
true, /^.*\.properties$/);
const reqClient = require.context("raw!devtools/locale/",
true, /^.*\.properties$/);
const reqGlobal = require.context("raw!global/locale/",
true, /^.*\.properties$/);
/**
* Memoized getter for properties files that ensures a given url is only required and
* parsed once.
@ -18,7 +43,23 @@ const propertiesMap = {};
*/
function getProperties(url) {
if (!propertiesMap[url]) {
propertiesMap[url] = parsePropertiesFile(require(`raw!${url}`));
// See the comment above about webpack and require contexts. Here
// we take an input like "devtools-shared/locale/debugger.properties"
// and decide which context require function to use. Despite the
// string processing here, in the end a string identical to |url|
// ends up being passed to "require".
let index = url.lastIndexOf("/");
// Turn "mumble/locale/resource.properties" => "./resource.properties".
let baseName = "." + url.substr(index);
let reqFn;
if (/^global/.test(url)) {
reqFn = reqGlobal;
} else if (/^devtools-shared/.test(url)) {
reqFn = reqShared;
} else {
reqFn = reqClient;
}
propertiesMap[url] = parsePropertiesFile(reqFn(baseName));
}
return propertiesMap[url];