From 940071fbcfa03f2fa9139a6f2aaebefa267922ad Mon Sep 17 00:00:00 2001 From: Ayc0 Date: Wed, 8 Aug 2018 23:14:06 +0200 Subject: [PATCH] Supports --globals config --- README.md | 1 + src/index.js | 11 +++++++++++ src/prog.js | 2 ++ 3 files changed, 14 insertions(+) diff --git a/README.md b/README.md index c93e4e7..cee4a28 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ Just like `microbundle build`, but watches your source files and rebuilds on any -w, --watch Rebuilds on any change (default false) --target Specify your target environment (default node) --external Specify external dependencies, or 'none' + --globals Specify globals dependencies, or 'none' --compress Compress output using UglifyJS (default true) --strict Enforce undefined global context and add "use strict" --name Specify name exposed in UMD builds diff --git a/src/index.js b/src/index.js index 503d036..84e1e8d 100644 --- a/src/index.js +++ b/src/index.js @@ -31,6 +31,14 @@ const safeVariableName = name => .toLowerCase() .replace(/((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g, ''), ); +const parseGlobals = globalStrings => { + const globals = {}; + globalStrings.split(',').forEach(globalString => { + const [localName, globalName] = globalString.split('='); + globals[localName] = globalName; + }); + return globals; +}; const WATCH_OPTS = { exclude: 'node_modules/**', @@ -243,6 +251,9 @@ function createConfig(options, entry, format, writeMeta) { } return globals; }, {}); + if (options.globals && options.globals !== 'none') { + globals = Object.assign(globals, parseGlobals(options.globals)); + } function replaceName(filename, name) { return resolve( diff --git a/src/prog.js b/src/prog.js index ed820ac..a61b12b 100644 --- a/src/prog.js +++ b/src/prog.js @@ -20,6 +20,8 @@ export default handler => { .option('--watch, -w', 'Rebuilds on any change', false) .option('--target', 'Specify your target environment', 'node') .option('--external', `Specify external dependencies, or 'none'`) + .option('--globals', `Specify globals dependencies, or 'none'`) + .example('microbundle --globals react=React,jquery=$') .option('--compress', 'Compress output using UglifyJS', true) .option('--strict', 'Enforce undefined global context and add "use strict"') .option('--name', 'Specify name exposed in UMD builds')