From c1641790d9ad44495b65d82a3cf146591877b03a Mon Sep 17 00:00:00 2001 From: Mark Banner Date: Sat, 24 Feb 2024 10:00:59 +0000 Subject: [PATCH] Bug 1881817 - Move all asrouter/content-src/*.js files which are modules to use .mjs extensions. r=aminomancer,omc-reviewers Differential Revision: https://phabricator.services.mozilla.com/D202622 --- browser/components/asrouter/.eslintrc.js | 2 +- .../{asrouter-utils.js => asrouter-utils.mjs} | 18 +++-- .../asrouter/content/asrouter-admin.bundle.js | 73 ++++++++----------- .../components/asrouter/karma.mc.config.js | 4 +- .../asrouter/tests/unit/ASRouterChild.test.js | 11 --- .../tests/unit/asrouter-utils.test.js | 36 ++++----- .../asrouter/webpack.asrouter-admin.config.js | 2 +- 7 files changed, 59 insertions(+), 87 deletions(-) rename browser/components/asrouter/content-src/{asrouter-utils.js => asrouter-utils.mjs} (73%) diff --git a/browser/components/asrouter/.eslintrc.js b/browser/components/asrouter/.eslintrc.js index 7a67e797e6ea..60b020f9a731 100644 --- a/browser/components/asrouter/.eslintrc.js +++ b/browser/components/asrouter/.eslintrc.js @@ -15,7 +15,7 @@ module.exports = { { // TODO: Bug 1773467 - Move these to .mjs or figure out a generic way // to identify these as modules. - files: ["content-src/**/*.js", "tests/unit/**/*.js"], + files: ["tests/unit/**/*.js"], parserOptions: { sourceType: "module", }, diff --git a/browser/components/asrouter/content-src/asrouter-utils.js b/browser/components/asrouter/content-src/asrouter-utils.mjs similarity index 73% rename from browser/components/asrouter/content-src/asrouter-utils.js rename to browser/components/asrouter/content-src/asrouter-utils.mjs index 65d25cb9073e..989d864e71ac 100644 --- a/browser/components/asrouter/content-src/asrouter-utils.js +++ b/browser/components/asrouter/content-src/asrouter-utils.mjs @@ -2,23 +2,25 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { MESSAGE_TYPE_HASH as msg } from "modules/ActorConstants.sys.mjs"; -import { actionCreators as ac } from "common/Actions.sys.mjs"; +// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system +import { MESSAGE_TYPE_HASH as msg } from "../modules/ActorConstants.sys.mjs"; +// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system +import { actionCreators as ac } from "../../newtab/common/Actions.sys.mjs"; export const ASRouterUtils = { addListener(listener) { - if (global.ASRouterAddParentListener) { - global.ASRouterAddParentListener(listener); + if (globalThis.ASRouterAddParentListener) { + globalThis.ASRouterAddParentListener(listener); } }, removeListener(listener) { - if (global.ASRouterRemoveParentListener) { - global.ASRouterRemoveParentListener(listener); + if (globalThis.ASRouterRemoveParentListener) { + globalThis.ASRouterRemoveParentListener(listener); } }, sendMessage(action) { - if (global.ASRouterMessage) { - return global.ASRouterMessage(action); + if (globalThis.ASRouterMessage) { + return globalThis.ASRouterMessage(action); } throw new Error(`Unexpected call:\n${JSON.stringify(action, null, 3)}`); }, diff --git a/browser/components/asrouter/content/asrouter-admin.bundle.js b/browser/components/asrouter/content/asrouter-admin.bundle.js index b92be649c624..170f32a66fc9 100644 --- a/browser/components/asrouter/content/asrouter-admin.bundle.js +++ b/browser/components/asrouter/content/asrouter-admin.bundle.js @@ -10,108 +10,97 @@ var ASRouterAdminRenderUtils; /******/ var __webpack_modules__ = ([ /* 0 */, /* 1 */ -/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { __webpack_require__.r(__webpack_exports__); /* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ "ASRouterUtils": () => (/* binding */ ASRouterUtils) /* harmony export */ }); -/* harmony import */ var modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2); -/* harmony import */ var common_Actions_sys_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3); +/* harmony import */ var _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2); +/* harmony import */ var _newtab_common_Actions_sys_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3); /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ +// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system + +// eslint-disable-next-line mozilla/reject-import-system-module-from-non-system const ASRouterUtils = { addListener(listener) { - if (__webpack_require__.g.ASRouterAddParentListener) { - __webpack_require__.g.ASRouterAddParentListener(listener); + if (globalThis.ASRouterAddParentListener) { + globalThis.ASRouterAddParentListener(listener); } }, removeListener(listener) { - if (__webpack_require__.g.ASRouterRemoveParentListener) { - __webpack_require__.g.ASRouterRemoveParentListener(listener); + if (globalThis.ASRouterRemoveParentListener) { + globalThis.ASRouterRemoveParentListener(listener); } }, sendMessage(action) { - if (__webpack_require__.g.ASRouterMessage) { - return __webpack_require__.g.ASRouterMessage(action); + if (globalThis.ASRouterMessage) { + return globalThis.ASRouterMessage(action); } throw new Error(`Unexpected call:\n${JSON.stringify(action, null, 3)}`); }, blockById(id, options) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.BLOCK_MESSAGE_BY_ID, - data: { - id, - ...options - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.BLOCK_MESSAGE_BY_ID, + data: { id, ...options }, }); }, modifyMessageJson(content) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.MODIFY_MESSAGE_JSON, - data: { - content - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.MODIFY_MESSAGE_JSON, + data: { content }, }); }, executeAction(button_action) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.USER_ACTION, - data: button_action + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.USER_ACTION, + data: button_action, }); }, unblockById(id) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.UNBLOCK_MESSAGE_BY_ID, - data: { - id - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.UNBLOCK_MESSAGE_BY_ID, + data: { id }, }); }, blockBundle(bundle) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.BLOCK_BUNDLE, - data: { - bundle - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.BLOCK_BUNDLE, + data: { bundle }, }); }, unblockBundle(bundle) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.UNBLOCK_BUNDLE, - data: { - bundle - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.UNBLOCK_BUNDLE, + data: { bundle }, }); }, overrideMessage(id) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.OVERRIDE_MESSAGE, - data: { - id - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.OVERRIDE_MESSAGE, + data: { id }, }); }, editState(key, value) { return ASRouterUtils.sendMessage({ - type: modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.EDIT_STATE, - data: { - [key]: value - } + type: _modules_ActorConstants_sys_mjs__WEBPACK_IMPORTED_MODULE_0__.MESSAGE_TYPE_HASH.EDIT_STATE, + data: { [key]: value }, }); }, sendTelemetry(ping) { - return ASRouterUtils.sendMessage(common_Actions_sys_mjs__WEBPACK_IMPORTED_MODULE_1__.actionCreators.ASRouterUserEvent(ping)); + return ASRouterUtils.sendMessage(_newtab_common_Actions_sys_mjs__WEBPACK_IMPORTED_MODULE_1__.actionCreators.ASRouterUserEvent(ping)); }, getPreviewEndpoint() { return null; - } + }, }; + /***/ }), /* 2 */ /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { diff --git a/browser/components/asrouter/karma.mc.config.js b/browser/components/asrouter/karma.mc.config.js index e6efd1ea754c..63b4cf785d3f 100644 --- a/browser/components/asrouter/karma.mc.config.js +++ b/browser/components/asrouter/karma.mc.config.js @@ -124,7 +124,7 @@ module.exports = function (config) { devtool: "inline-source-map", // This resolve config allows us to import with paths relative to the root directory resolve: { - extensions: [".js", ".jsx"], + extensions: [".mjs", ".js", ".jsx"], modules: [ PATHS.moduleResolveDirectory, "node_modules", @@ -181,7 +181,7 @@ module.exports = function (config) { }, { enforce: "post", - test: /\.js[mx]?$/, + test: /\.js[x]?$/, loader: "@jsdevtools/coverage-istanbul-loader", options: { esModules: true }, include: [path.resolve("content-src"), path.resolve("modules")], diff --git a/browser/components/asrouter/tests/unit/ASRouterChild.test.js b/browser/components/asrouter/tests/unit/ASRouterChild.test.js index 41fdd79ea26f..b73e56d5102f 100644 --- a/browser/components/asrouter/tests/unit/ASRouterChild.test.js +++ b/browser/components/asrouter/tests/unit/ASRouterChild.test.js @@ -1,22 +1,12 @@ /*eslint max-nested-callbacks: ["error", 10]*/ import { ASRouterChild } from "actors/ASRouterChild.sys.mjs"; import { MESSAGE_TYPE_HASH as msg } from "modules/ActorConstants.sys.mjs"; -import { GlobalOverrider } from "test/unit/utils"; describe("ASRouterChild", () => { let asRouterChild = null; - let globals = null; - let overrider = null; let sandbox = null; beforeEach(() => { sandbox = sinon.createSandbox(); - globals = { - Cu: { - cloneInto: sandbox.stub().returns(Promise.resolve()), - }, - }; - overrider = new GlobalOverrider(); - overrider.set(globals); asRouterChild = new ASRouterChild(); asRouterChild.telemetry = { sendTelemetry: sandbox.stub(), @@ -26,7 +16,6 @@ describe("ASRouterChild", () => { }); afterEach(() => { sandbox.restore(); - overrider.restore(); asRouterChild = null; }); describe("asRouterMessage", () => { diff --git a/browser/components/asrouter/tests/unit/asrouter-utils.test.js b/browser/components/asrouter/tests/unit/asrouter-utils.test.js index 553c9608ed34..81252b4d5046 100644 --- a/browser/components/asrouter/tests/unit/asrouter-utils.test.js +++ b/browser/components/asrouter/tests/unit/asrouter-utils.test.js @@ -1,30 +1,22 @@ -import { ASRouterUtils } from "content-src/asrouter-utils"; -import { GlobalOverrider } from "test/unit/utils"; +import { ASRouterUtils } from "../../content-src/asrouter-utils.mjs"; describe("ASRouterUtils", () => { - let globals = null; - let overrider = null; let sandbox = null; beforeEach(() => { sandbox = sinon.createSandbox(); - globals = { - ASRouterMessage: sandbox.stub().resolves({}), - }; - overrider = new GlobalOverrider(); - overrider.set(globals); + globalThis.ASRouterMessage = sandbox.stub().resolves({}); }); afterEach(() => { sandbox.restore(); - overrider.restore(); }); describe("sendMessage", () => { it("default", () => { ASRouterUtils.sendMessage({ foo: "bar" }); - assert.calledOnce(globals.ASRouterMessage); - assert.calledWith(globals.ASRouterMessage, { foo: "bar" }); + assert.calledOnce(globalThis.ASRouterMessage); + assert.calledWith(globalThis.ASRouterMessage, { foo: "bar" }); }); it("throws if ASRouterMessage is not defined", () => { - overrider.set("ASRouterMessage", undefined); + globalThis.ASRouterMessage = null; assert.throws(() => ASRouterUtils.sendMessage({ foo: "bar" })); }); it("can accept the legacy NEWTAB_MESSAGE_REQUEST message without throwing", async () => { @@ -41,7 +33,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.blockById(1, { foo: "bar" }); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { foo: "bar", id: 1 } }) ); }); @@ -50,7 +42,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.modifyMessageJson({ foo: "bar" }); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { content: { foo: "bar" } } }) ); }); @@ -59,7 +51,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.executeAction({ foo: "bar" }); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { foo: "bar" } }) ); }); @@ -68,7 +60,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.unblockById(2); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { id: 2 } }) ); }); @@ -77,7 +69,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.blockBundle(2); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { bundle: 2 } }) ); }); @@ -86,7 +78,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.unblockBundle(2); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { bundle: 2 } }) ); }); @@ -95,7 +87,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.overrideMessage(12); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { id: 12 } }) ); }); @@ -104,7 +96,7 @@ describe("ASRouterUtils", () => { it("default", () => { ASRouterUtils.editState("foo", "bar"); assert.calledWith( - globals.ASRouterMessage, + globalThis.ASRouterMessage, sinon.match({ data: { foo: "bar" } }) ); }); @@ -112,7 +104,7 @@ describe("ASRouterUtils", () => { describe("sendTelemetry", () => { it("default", () => { ASRouterUtils.sendTelemetry({ foo: "bar" }); - assert.calledOnce(globals.ASRouterMessage); + assert.calledOnce(globalThis.ASRouterMessage); }); }); }); diff --git a/browser/components/asrouter/webpack.asrouter-admin.config.js b/browser/components/asrouter/webpack.asrouter-admin.config.js index a8eb54d43d59..d051b42a62a7 100644 --- a/browser/components/asrouter/webpack.asrouter-admin.config.js +++ b/browser/components/asrouter/webpack.asrouter-admin.config.js @@ -25,7 +25,7 @@ module.exports = Object.assign({}, config(), { plugins: [new webpack.BannerPlugin(banner)], // This resolve config allows us to import with paths relative to the root directory resolve: { - extensions: [".js", ".jsx"], + extensions: [".js", ".jsx", ".mjs"], alias: { newtab: absolute("../newtab"), common: absolute("../newtab/common"),