Exports global variables using ES6 module syntax.
This is useful for working in a code base that is stuck with code that doesn't use modules, such as legacy or vendor code that we do not own, so I wrote this plugin to shim them and import them using modules. This doesn't provide any mechanism for specifying imports or requires, though I welcome additions.
Shimming dependency management is, in my experience, a very error prone approach, with a near guarantee of breaking. The goal of this module isn't to make non-modules walk like modules, but to allow them to be included via module loaders to free client code written as modules from the burden of having to import something from the global namespace, and enable you to inject a better dependency later on without changing the consumers.
var exportGlobals = ;moduleexports =;
this will augment the input file (
foo.js) with the following export statements:
You can also modify more than one file in the same tree by passing an object mapping the file name relative to the root
of the tree to an object containing either the
exports properties (or both):
The default format is ES2015 modules, but CommonJS is also supported. This can be handy if you're going to compile your ES6 modules down to CJS anyway, you can avoid the overhead of invoking your compiler for something we can do up front anyway.
- ES6 modules -
- CommonJS -
This will yield the following output:
exportsbar = bar;exportsbaz = baz;Object;exports'default' = Foo;
This CJS output is compatible with the way code compiled with babel looks for ES6 default exports by checking the
__esModule property on the object returned from
require. If you have no default export, this code will not be emitted.
Issues and PRs are very welcome. PRs are likely to be merged if you don't change the existing tests too much, code coverage doesn't drop, and you respect the existing code style, which is pretty heavily enforced by the linter (so don't suppress it).