Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-tokens-keep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'microbundle': patch
---

Preserve equals signs in `--define` values, including padded tokens and equality or arrow expressions.
6 changes: 5 additions & 1 deletion src/lib/option-normalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ export function toReplacementExpression(value, name) {
export function parseMappingArgument(globalStrings, processValue) {
const globals = {};
globalStrings.split(',').forEach(globalString => {
let [key, value] = globalString.split('=');
const separator = globalString.indexOf('=');
let key =
separator === -1 ? globalString : globalString.slice(0, separator);
let value =
separator === -1 ? undefined : globalString.slice(separator + 1);
if (processValue) {
const r = processValue(value, key);
if (r !== undefined) {
Expand Down
24 changes: 24 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,30 @@ exports[`fixtures build define with microbundle 5`] = `
"
`;

exports[`fixtures build define-equals with microbundle 1`] = `
"Used script: microbundle --no-sourcemap -f cjs --define \\"TOKEN='abc==',URL='https://example.test/?key=value=tail',@IS_EQUAL=(1===1),@INCREMENT=(value=>value+1)\\"

Directory tree:

define-equals
dist
define-equals.js
index.js
package.json


Build \\"define-equals\\" to dist:
112 B: define-equals.js.gz
91 B: define-equals.js.br"
`;

exports[`fixtures build define-equals with microbundle 2`] = `1`;

exports[`fixtures build define-equals with microbundle 3`] = `
"module.exports={token:\\"abc==\\",url:\\"https://example.test/?key=value=tail\\",equal:!0,incremented:3};
"
`;

exports[`fixtures build define-expression with microbundle 1`] = `
"Used script: microbundle -f esm --no-sourcemap --define @Object.freeze=Object

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/define-equals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
token: TOKEN,
url: URL,
equal: IS_EQUAL,
incremented: INCREMENT(2),
};
6 changes: 6 additions & 0 deletions test/fixtures/define-equals/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "define-equals",
"scripts": {
"build": "microbundle --no-sourcemap -f cjs --define \"TOKEN='abc==',URL='https://example.test/?key=value=tail',@IS_EQUAL=(1===1),@INCREMENT=(value=>value+1)\""
}
}
60 changes: 60 additions & 0 deletions test/option-normalization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
parseMappingArgument,
toReplacementExpression,
} from '../src/lib/option-normalization';

describe('mapping arguments', () => {
it.each([
['TOKEN=abc==', { TOKEN: 'abc==' }],
[
'URL=https://example.test/?key=value=tail',
{ URL: 'https://example.test/?key=value=tail' },
],
['VALUE==', { VALUE: '=' }],
[
'VALUE=first=second\nthird=fourth',
{ VALUE: 'first=second\nthird=fourth' },
],
])('preserves equals signs in %s', (input, expected) => {
expect(parseMappingArgument(input)).toEqual(expected);
});

it.each([
['TOKEN=abc==', { TOKEN: '"abc=="' }],
["TOKEN='abc=='", { TOKEN: '"abc=="' }],
['TOKEN="abc=="', { TOKEN: '"abc=="' }],
['@IS_EQUAL=1===1', { IS_EQUAL: '1===1' }],
['@INCREMENT=value=>value+1', { INCREMENT: 'value=>value+1' }],
])('preserves define values in %s', (input, expected) => {
expect(parseMappingArgument(input, toReplacementExpression)).toEqual(
expected,
);
});

it('keeps neighboring definitions and literal conversion', () => {
expect(
parseMappingArgument(
'A=true,TOKEN=abc==,B=42,C=false',
toReplacementExpression,
),
).toEqual({ A: 'true', TOKEN: '"abc=="', B: '42', C: 'false' });
});

it('keeps empty and missing values distinct', () => {
expect(parseMappingArgument('EMPTY=,MISSING')).toEqual({
EMPTY: '',
MISSING: undefined,
});
});

it('passes the complete value and key to the processor', () => {
const process = jest.fn((value, key) => [
value.toUpperCase(),
key.toLowerCase(),
]);
expect(parseMappingArgument('TOKEN=abc==', process)).toEqual({
token: 'ABC==',
});
expect(process).toHaveBeenCalledWith('abc==', 'TOKEN');
});
});