From 6c601d3922f761d73c503833d16312e7ab00355e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 9 Jul 2023 11:37:00 +0200 Subject: [PATCH] [ESM] Convert the "default preferences"-handling to use `import()` syntax Unfortunately I wasn't able to come up with a *simple* way to just replace the synchronous `require`-call, since we need to ensure that the default preferences are available when bundling starts. Hence this patch adds a new intermediate parsing-step in all the relevant gulp-tasks, but this shouldn't affect build-times noticeable since the amount of extra parsing is very small. *Please note:* It's very possible that there's a better way to handle this, however I figured that unblocking further ESM-work is more important than a "perfect" solution. --- gulpfile.js | 83 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index e4436dba684ae..a7266b1ee59ea 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -770,32 +770,52 @@ function buildDefaultPreferences(defines, dir) { const bundleDefines = builder.merge(defines, { LIB: true, - SKIP_BABEL: false, - BUNDLE_VERSION: 0, // Dummy version - BUNDLE_BUILD: 0, // Dummy build TESTING: defines.TESTING ?? process.env.TESTING === "true", }); - const inputStream = merge([ - gulp.src(["web/app_options.js"], { - base: ".", - }), - ]); - - return buildLibHelper( + const defaultPreferencesConfig = createWebpackConfig( bundleDefines, - inputStream, - DEFAULT_PREFERENCES_DIR + dir + { + filename: "app_options.mjs", + library: { + type: "module", + }, + }, + { + disableVersionInfo: true, + } ); + return gulp + .src("web/app_options.js") + .pipe(webpack2Stream(defaultPreferencesConfig)) + .pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + dir)); } -function getDefaultPreferences(dir) { - const { AppOptions, OptionKind } = require("./" + - DEFAULT_PREFERENCES_DIR + - dir + - "web/app_options.js"); +async function parseDefaultPreferences(dir) { + console.log(); + console.log("### Parsing default preferences"); + + // eslint-disable-next-line no-unsanitized/method + const { AppOptions, OptionKind } = await import( + "./" + DEFAULT_PREFERENCES_DIR + dir + "app_options.mjs" + ); + + const prefs = AppOptions.getAll(OptionKind.PREFERENCE); + if (Object.keys(prefs).length === 0) { + throw new Error("No default preferences found."); + } - return AppOptions.getAll(OptionKind.PREFERENCE); + fs.writeFileSync( + DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json", + JSON.stringify(prefs) + ); +} + +function getDefaultPreferences(dir) { + const str = fs + .readFileSync(DEFAULT_PREFERENCES_DIR + dir + "default_preferences.json") + .toString(); + return JSON.parse(str); } gulp.task("locale", function () { @@ -948,6 +968,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsGeneric() { + await parseDefaultPreferences("generic/"); + }, function createGeneric() { console.log(); console.log("### Creating generic viewer"); @@ -975,6 +998,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsGenericLegacy() { + await parseDefaultPreferences("generic-legacy/"); + }, function createGenericLegacy() { console.log(); console.log("### Creating generic (legacy) viewer"); @@ -1186,6 +1212,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsMinified() { + await parseDefaultPreferences("minified/"); + }, function createMinified() { console.log(); console.log("### Creating minified viewer"); @@ -1216,6 +1245,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsMinifiedLegacy() { + await parseDefaultPreferences("minified-legacy/"); + }, function createMinifiedLegacy() { console.log(); console.log("### Creating minified (legacy) viewer"); @@ -1268,6 +1300,9 @@ gulp.task( const defines = builder.merge(DEFINES, { MOZCENTRAL: true }); return buildDefaultPreferences(defines, "mozcentral/"); }, + async function prefsMozcentral() { + await parseDefaultPreferences("mozcentral/"); + }, function createMozcentral() { console.log(); console.log("### Building mozilla-central extension"); @@ -1365,6 +1400,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsChromium() { + await parseDefaultPreferences("chromium/"); + }, function createChromium() { console.log(); console.log("### Building Chromium extension"); @@ -1589,6 +1627,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsLib() { + await parseDefaultPreferences("lib/"); + }, function createLib() { const defines = builder.merge(DEFINES, { GENERIC: true, LIB: true }); @@ -1615,6 +1656,9 @@ gulp.task( createTemporaryScriptingBundle(defines), ]); }, + async function prefsLibLegacy() { + await parseDefaultPreferences("lib-legacy/"); + }, function createLibLegacy() { const defines = builder.merge(DEFINES, { GENERIC: true, @@ -1927,6 +1971,9 @@ gulp.task( }); return buildDefaultPreferences(defines, "lint-chromium/"); }, + async function prefsLintChromium() { + await parseDefaultPreferences("lint-chromium/"); + }, function runLintChromium(done) { console.log(); console.log("### Checking supplemental Chromium files");