mirror of
https://github.com/developit/microbundle.git
synced 2026-01-18 13:56:35 +00:00
Make --define preserve boolean and integer literals by default instead of making everything a string.
This commit is contained in:
parent
39dad8e724
commit
3d21c043c1
39
src/index.js
39
src/index.js
@ -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;
|
||||
};
|
||||
@ -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) {
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
6
test/fixtures/define/index.js
vendored
Normal file
6
test/fixtures/define/index.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
const DEBUG = true;
|
||||
if (DEBUG) {
|
||||
console.log('debug mode', DEBUG);
|
||||
} else {
|
||||
console.log('production mode', DEBUG);
|
||||
}
|
||||
6
test/fixtures/define/package.json
vendored
Normal file
6
test/fixtures/define/package.json
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "alias",
|
||||
"scripts": {
|
||||
"build": "microbundle --no-sourcemap -f cjs --define DEBUG=false"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user