From 70a531e5aa88a68afb67b0ff7902fcfda2d6c298 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 2 Jul 2023 03:59:42 -0400 Subject: [PATCH] fix: inject browser options into node-resolve plugin if config.browser is true --- source/index.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/source/index.ts b/source/index.ts index a53f316..1953ed0 100644 --- a/source/index.ts +++ b/source/index.ts @@ -75,7 +75,31 @@ export default function (): PresetAsset { noSymlinks: ['rollup.config.ts'], supplementaryConfig: { 'gitignore': ['/rollup.config.ts'], - 'rollup': resolve(CONFIGS, 'rollup.yaml'), + 'rollup': (context) => { + const content = loadFile(resolve(CONFIGS, 'rollup.yaml')) as any; + if (context.custom.config.browser) { + let plugins = content.plugins.map((item) => + Array.isArray(item) ? item[0] : item, + ); + + let resolvePluginName = plugins.filter((item) => + item.includes('node-resolve'), + ); + + if (resolvePluginName.length) { + const index = plugins.indexOf(resolvePluginName[0]); + if (!Array.isArray(content.plugins[index])) { + content.plugins[index] = [content.plugins[index], {}]; + } + + content.plugins[index][1] = { + browser: true, + preferBuiltins: false, + }; + } + } + return content; + }, 'tsconfig': { compilerOptions: { moduleResolution: 'nodenext',