Skip to content

Commit

Permalink
Make --define preserve boolean and integer literals by default instea…
Browse files Browse the repository at this point in the history
…d of making everything a string.
  • Loading branch information
developit committed Feb 22, 2019
1 parent 39dad8e commit 3d21c04
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/index.js
Expand Up @@ -26,12 +26,40 @@ import camelCase from 'camelcase';

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

// Convert booleans and int define= values to literals.
// This is more intuitive than `microbundle --define A=1` producing A="1".
// See: https://github.com/terser-js/terser#conditional-compilation-api
const toTerserLiteral = (value, name) => {
// --define A="1",B='true' produces string:
const matches = value.match(/^(['"])(.+)\1$/);
if (matches) {
return [matches[2], name];
}

// --define A=1,B=true produces int/boolean literal:
if (/^(true|false|\d+)$/i.test(value)) {
return [value, '@' + name];
}

// default: behaviour from Terser (@prefix=1 produces expression/literal, unprefixed=1 produces string literal):
};

// Parses values of the form "$=jQuery,React=react" into key-value object pairs.
const parseMappingArgument = globalStrings => {
const parseMappingArgument = (globalStrings, processValue) => {
const globals = {};
globalStrings.split(',').forEach(globalString => {
const [localName, globalName] = globalString.split('=');
globals[localName] = globalName;
let [key, value] = globalString.split('=');
if (processValue) {
const r = processValue(value, key);
if (r !== undefined) {
if (Array.isArray(r)) {
[value, key] = r;
} else {
value = r;
}
}
}
globals[key] = value;
});
return globals;
};
Expand Down Expand Up @@ -325,7 +353,10 @@ function createConfig(options, entry, format, writeMeta) {

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

function replaceName(filename, name) {
Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/index.test.js.snap
Expand Up @@ -753,6 +753,28 @@ exports[`fixtures default-named 6`] = `
exports[`fixtures default-named 7`] = `"{\\"version\\":3,\\"file\\":\\"default-named.umd.js\\",\\"sources\\":[\\"../src/index.js\\"],\\"sourcesContent\\":[\\"export const foo = 42;\\\\nexport default function bar() {}\\\\n\\"],\\"names\\":[],\\"mappings\\":\\"wLAAmB,aACJ\\"}"`;
exports[`fixtures define 1`] = `
"Used script: microbundle --no-sourcemap -f cjs --define DEBUG=false
Directory tree:
define
dist
alias.js
index.js
package.json
Build \\"alias\\" to dist:
50 B: alias.js.gz
34 B: alias.js.br"
`;
exports[`fixtures define 2`] = `
"console.log(\\"debug mode\\",!1);
"
`;
exports[`fixtures esnext-ts 1`] = `
"Used script: microbundle --raw
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/define/index.js
@@ -0,0 +1,6 @@
const DEBUG = true;
if (DEBUG) {
console.log('debug mode', DEBUG);
} else {
console.log('production mode', DEBUG);
}
6 changes: 6 additions & 0 deletions test/fixtures/define/package.json
@@ -0,0 +1,6 @@
{
"name": "alias",
"scripts": {
"build": "microbundle --no-sourcemap -f cjs --define DEBUG=false"
}
}

0 comments on commit 3d21c04

Please sign in to comment.