0.0.8 • Published 5 years ago
rollup-plugin-import-from-scope v0.0.8
rollup-plugin-import-from-scope
import {String, Number, Error, ...} from 'global'
allows you to manage usage of global variables.- JavaScript minifier works well because global variables are bind to local variables that can be renamed by minifier.
Installation
npm install --save-dev rollup-plugin-import-from-scope
Usage
// rollup.config.js
const importFromScope = require('rollup-plugin-import-from-scope');
{
...
plugins: [
importFromScope(),
],
...
}
How it works
This plugin detects imports from global
and generates an intermediate file that exports required global objects.
Suppose that you have following code:
// your-script.js
import {console, document} from 'global';
console.log(document.title);
This plugin generates the following intermediate files:
// __intermediate__/your-script.js
import _0 from '__intermediate__/console';
export {_0 as console};
import _1 from '__intermediate__/document';
export {_1 as document};
// __intermediate__/console
export default self['console'];
// __intermediate__/document
export default self['document'];
Options
globalVariable
(default:self
): The name of the global variable.moduleNames
: An object which has following properties.moduleNames.static
(global
)moduleNames.getter
(getglobal
)moduleNames.setter
(setglobal
)acorn
: An object which passed to acorn.
Assume you use the following options:
importFromScope({
globalVariable: 'GLOBAL',
moduleNames: {
static: 'G',
getter: 'getG',
setter: 'setG',
},
})
import {x} from 'G'
→const x = GLOBAL.x
import {x} from 'getG'
→const x = () => GLOBAL.x
import {x} from 'setG'
→const x = (value) => GLOBAL.x = value
LICENSE
MIT