reject-import-system-module-from-non-system.rst (1428B)
1 reject-import-system-module-from-non-system 2 =========================================== 3 4 Rejects static import declaration for system modules (``.sys.mjs``) from non-system 5 modules. 6 7 Using static import for a system module into a non-system module would create a separate instance of the imported object(s) that is not shared with the other system modules and would break the per-process singleton expectation. 8 9 The reason for this is that inside system modules, a static import will load the module into the shared global. Inside non-system modules, the static import will load into a different global (e.g. window). This will cause the module to be loaded into different scopes, and hence create separate instances. The fix is to use ``ChromeUtils.importESModule`` which will import the object via the system module shared global scope. 10 11 12 Examples of incorrect code for this rule: 13 ----------------------------------------- 14 15 Inside a non-system module: 16 17 .. code-block:: js 18 19 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs"; 20 21 Examples of correct code for this rule: 22 --------------------------------------- 23 24 Inside a non-system module: 25 26 .. code-block:: js 27 28 const { AppConstants } = ChromeUtils.importESModule( 29 "resource://gre/modules/AppConstants.sys.mjs" 30 ); 31 32 Inside a system module: 33 34 .. code-block:: js 35 36 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";