From 2820c325e0fbcdaaf4eb436bea6afdfb8ce24291 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Mon, 9 Nov 2026 03:27:21 +0800 Subject: [PATCH 1/2] Preserve equals signs in define mapping values --- src/lib/option-normalization.js | 6 ++- test/__snapshots__/index.test.js.snap | 24 ++++++++++ test/fixtures/define-equals/index.js | 6 +++ test/fixtures/define-equals/package.json | 6 +++ test/option-normalization.test.js | 60 ++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/define-equals/index.js create mode 100644 test/fixtures/define-equals/package.json create mode 100644 test/option-normalization.test.js diff --git a/src/lib/option-normalization.js b/src/lib/option-normalization.js index 22811039..9881f3d0 100644 --- a/src/lib/option-normalization.js +++ b/src/lib/option-normalization.js @@ -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) { diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index 27dedc37..2664cdcd 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -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 diff --git a/test/fixtures/define-equals/index.js b/test/fixtures/define-equals/index.js new file mode 100644 index 00000000..e82af6c0 --- /dev/null +++ b/test/fixtures/define-equals/index.js @@ -0,0 +1,6 @@ +export default { + token: TOKEN, + url: URL, + equal: IS_EQUAL, + incremented: INCREMENT(2), +}; diff --git a/test/fixtures/define-equals/package.json b/test/fixtures/define-equals/package.json new file mode 100644 index 00000000..1682c905 --- /dev/null +++ b/test/fixtures/define-equals/package.json @@ -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)\"" + } +} diff --git a/test/option-normalization.test.js b/test/option-normalization.test.js new file mode 100644 index 00000000..4e53e4a6 --- /dev/null +++ b/test/option-normalization.test.js @@ -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'); + }); +}); From 4f21a2cd3b6e12987f982edada7ae0a84304cfd7 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Mon, 9 Nov 2026 08:56:26 +0800 Subject: [PATCH 2/2] Add patch changeset for complete define values --- .changeset/tame-tokens-keep.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tame-tokens-keep.md diff --git a/.changeset/tame-tokens-keep.md b/.changeset/tame-tokens-keep.md new file mode 100644 index 00000000..f24b62af --- /dev/null +++ b/.changeset/tame-tokens-keep.md @@ -0,0 +1,5 @@ +--- +'microbundle': patch +--- + +Preserve equals signs in `--define` values, including padded tokens and equality or arrow expressions.