JSM to ES Module Babel Plugin
This module converts import and export statements in .jsm
modules to ES modules. For example:
Source:
const utils: Cu = Components;const Bar Baz = Cu; { return 123;} thisWhatever = {};thisStuff = Stuff;var EXPORTED_SYMBOLS = "Stuff" "Whatever";
Compiles to:
; { return 123;} var Whatever = {};
Caveats / Limitations
Use top-level imports and exports only
Because import
and export
statements in ES modules must be statically analyzable, this plugin will only transform
top level Cu.import
/ EXPORTED_SYMBOLS
. Example:
Good:
const utils: Cu = Components;const Bar = Cu; thisBaz = 123;thisEXPORTED_SYMBOLS = "Baz";
Bad:
const utils: Cu = Components;const root = this; { // This won't get converted because it's inside innerFunction const Bar = Cu; // Don't do this either rootBaz = 123;} var EXPORTED_SYMBOLS = "Baz";
this
or dynamically add items to var EXPORTED_SYMBOLS
when exporting
Don't alias You should only declare this.EXPORTED_SYMBOLS
once in the top-level scope, and it should not be modified.
Good:
{...}thisFoo = Foo;thisBar = 123;thisEXPORTED_SYMBOLS = "Foo" "Bar";
Bad:
const root = this;const Foo = 123;rootEXPORTED_SYMBOLS = "Foo"; // Don't alias this when exportingrootBar = 456;rootEXPORTED_SYMBOLS; // Don't do this
Don't reassign Components.utils.import
Assigning variables to Components
or Components.utils
is OK, but don't assign the .import
;
const C = Components; // OKconst utils: Cu = Components; // OKconst u = Componentsutils // OK const import = Cu; // Don't do thisconst i = Componentsutilsimport // Don't do this
Options
basePath
Defaults to /^resource:\/\/
. A RegEx or String that tests for which import paths to rewrite.
replace
Defaults to false
. Remove the basePath
component of the import string?
e.g. If the basePath is /^resource:\/\/
, resource://activity-stream/foo.js
will be rewritten to activity-stream/foo.js
.
"plugins": ["transform-react-jsx", {basePath: "resource://activity-stream/"}],
removeOtherImports
Defaults to false
. Should we remove non-matching imports?