Skip to content

Commit

Permalink
Merge pull request #312 from developit/define-option
Browse files Browse the repository at this point in the history
Add define option
  • Loading branch information
developit committed Feb 20, 2019
2 parents 3021595 + 47a2e86 commit f58e677
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/index.js
Expand Up @@ -25,7 +25,8 @@ import camelCase from 'camelcase';

const removeScope = name => name.replace(/^@.*\//, '');

const parseGlobals = globalStrings => {
// Parses values of the form "$=jQuery,React=react" into key-value object pairs.
const parseMappingArgument = globalStrings => {
const globals = {};
globalStrings.split(',').forEach(globalString => {
const [localName, globalName] = globalString.split('=');
Expand Down Expand Up @@ -292,7 +293,7 @@ function createConfig(options, entry, format, writeMeta) {
aliases['.'] = './' + basename(options.output);
}
if (options.alias) {
aliases = Object.assign(aliases, parseGlobals(options.alias));
aliases = Object.assign(aliases, parseMappingArgument(options.alias));
}

const peerDeps = Object.keys(pkg.peerDependencies || {});
Expand All @@ -314,7 +315,12 @@ function createConfig(options, entry, format, writeMeta) {
return globals;
}, {});
if (options.globals && options.globals !== 'none') {
globals = Object.assign(globals, parseGlobals(options.globals));
globals = Object.assign(globals, parseMappingArgument(options.globals));
}

let defines = {};
if (options.define) {
defines = Object.assign(defines, parseMappingArgument(options.define));
}

function replaceName(filename, name) {
Expand Down Expand Up @@ -491,6 +497,7 @@ function createConfig(options, entry, format, writeMeta) {
compress: {
keep_infinity: true,
pure_getters: true,
global_defs: defines,
},
warnings: true,
ecma: 5,
Expand Down
2 changes: 2 additions & 0 deletions src/prog.js
Expand Up @@ -24,6 +24,8 @@ export default handler => {
.option('--external', `Specify external dependencies, or 'none'`)
.option('--globals', `Specify globals dependencies, or 'none'`)
.example('microbundle --globals react=React,jquery=$')
.option('--define', 'Replace constants with hard-coded values')
.example('microbundle --define API_KEY=1234')
.option('--alias', `Map imports to different modules`)
.example('microbundle --alias react=preact')
.option('--compress', 'Compress output using Terser', null)
Expand Down

0 comments on commit f58e677

Please sign in to comment.