From deb87242beeffde1b151a3319e49f67fc689d78c Mon Sep 17 00:00:00 2001 From: Rene Haas Date: Fri, 26 Jul 2024 10:37:47 +0200 Subject: [PATCH] fix multiple outputs logic --- .../qwik/src/optimizer/src/plugins/rollup.ts | 31 +++---- .../qwik/src/optimizer/src/plugins/vite.ts | 8 +- .../src/optimizer/src/plugins/vite.unit.ts | 80 ++++++------------- 3 files changed, 45 insertions(+), 74 deletions(-) diff --git a/packages/qwik/src/optimizer/src/plugins/rollup.ts b/packages/qwik/src/optimizer/src/plugins/rollup.ts index 8658f08cf1f..8c1c4ba66e2 100644 --- a/packages/qwik/src/optimizer/src/plugins/rollup.ts +++ b/packages/qwik/src/optimizer/src/plugins/rollup.ts @@ -148,22 +148,25 @@ export function qwikRollup(qwikRollupOpts: QwikRollupPluginOptions = {}): any { export function normalizeRollupOutputOptions( opts: NormalizedQwikPluginOptions, rollupOutputOpts: Rollup.OutputOptions | Rollup.OutputOptions[] | undefined, - useAssetsDir: boolean -): Rollup.OutputOptions[] { - const outputOpts: Rollup.OutputOptions[] = Array.isArray(rollupOutputOpts) - ? // fill the `outputOpts` array with all existing option entries - [...rollupOutputOpts] - : // use the existing rollupOutputOpts object or create a new one - [rollupOutputOpts || {}]; - - // make sure at least one output is present in every case - if (!outputOpts.length) { - outputOpts.push({}); + useAssetsDir: boolean, + outDir?: string +): Rollup.OutputOptions | Rollup.OutputOptions[] { + if (Array.isArray(rollupOutputOpts)) { + // make sure at least one output is present in every case + if (!rollupOutputOpts.length) { + rollupOutputOpts.push({}); + } + + return rollupOutputOpts.map((outputOptsObj) => ({ + ...normalizeRollupOutputOptionsObject(opts, outputOptsObj, useAssetsDir), + dir: outDir || outputOptsObj.dir, + })); } - return outputOpts.map((outputOptsObj) => - normalizeRollupOutputOptionsObject(opts, outputOptsObj, useAssetsDir) - ); + return { + ...normalizeRollupOutputOptionsObject(opts, rollupOutputOpts, useAssetsDir), + dir: outDir || rollupOutputOpts?.dir, + }; } export function normalizeRollupOutputOptionsObject( diff --git a/packages/qwik/src/optimizer/src/plugins/vite.ts b/packages/qwik/src/optimizer/src/plugins/vite.ts index 2c145086be2..ba3789bdd28 100644 --- a/packages/qwik/src/optimizer/src/plugins/vite.ts +++ b/packages/qwik/src/optimizer/src/plugins/vite.ts @@ -339,11 +339,9 @@ export function qwikVite(qwikViteOpts: QwikVitePluginOptions = {}): any { output: normalizeRollupOutputOptions( opts, viteConfig.build?.rollupOptions?.output, - useAssetsDir - ).map((outputOptsObj) => { - outputOptsObj.dir = buildOutputDir; - return outputOptsObj; - }), + useAssetsDir, + buildOutputDir + ), preserveEntrySignatures: 'exports-only', onwarn: (warning, warn) => { if (warning.plugin === 'typescript' && warning.message.includes('outputToFilesystem')) { diff --git a/packages/qwik/src/optimizer/src/plugins/vite.unit.ts b/packages/qwik/src/optimizer/src/plugins/vite.unit.ts index 427c9ddc06b..041149e4198 100644 --- a/packages/qwik/src/optimizer/src/plugins/vite.unit.ts +++ b/packages/qwik/src/optimizer/src/plugins/vite.unit.ts @@ -52,7 +52,7 @@ test('command: serve, mode: development', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'client'); assert.deepEqual(opts.buildMode, 'development'); @@ -62,15 +62,10 @@ test('command: serve, mode: development', async () => { assert.deepEqual(build.outDir, normalizePath(resolve(cwd, 'dist'))); assert.deepEqual(rollupOptions.input, normalizePath(resolve(cwd, 'src', 'entry.dev'))); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, 'build/[name].js'); - assert.deepEqual(outputOptionsObj.entryFileNames, 'build/[name].js'); - assert.deepEqual(outputOptionsObj.format, 'es'); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, 'build/[name].js'); + assert.deepEqual(outputOptions.entryFileNames, 'build/[name].js'); + assert.deepEqual(outputOptions.format, 'es'); assert.deepEqual(build.dynamicImportVarsOptions?.exclude, [/./]); assert.deepEqual(build.ssr, undefined); @@ -92,7 +87,7 @@ test('command: serve, mode: production', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'client'); assert.deepEqual(opts.buildMode, 'production'); @@ -104,15 +99,10 @@ test('command: serve, mode: production', async () => { assert.deepEqual(build.emptyOutDir, undefined); assert.deepEqual(rollupOptions.input, normalizePath(resolve(cwd, 'src', 'entry.dev'))); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, 'build/q-[hash].js'); - assert.deepEqual(outputOptionsObj.entryFileNames, 'build/q-[hash].js'); - assert.deepEqual(outputOptionsObj.format, 'es'); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, 'build/q-[hash].js'); + assert.deepEqual(outputOptions.entryFileNames, 'build/q-[hash].js'); + assert.deepEqual(outputOptions.format, 'es'); assert.deepEqual(build.dynamicImportVarsOptions?.exclude, [/./]); assert.deepEqual(build.ssr, undefined); @@ -133,7 +123,7 @@ test('command: build, mode: development', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'client'); assert.deepEqual(opts.buildMode, 'development'); @@ -146,14 +136,9 @@ test('command: build, mode: development', async () => { assert.deepEqual(build.emptyOutDir, undefined); assert.deepEqual(rollupOptions.input, [normalizePath(resolve(cwd, 'src', 'root'))]); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, 'build/[name].js'); - assert.deepEqual(outputOptionsObj.entryFileNames, 'build/[name].js'); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, 'build/[name].js'); + assert.deepEqual(outputOptions.entryFileNames, 'build/[name].js'); assert.deepEqual(build.dynamicImportVarsOptions?.exclude, [/./]); assert.deepEqual(build.ssr, undefined); @@ -177,7 +162,7 @@ test('command: build, mode: production', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'client'); assert.deepEqual(opts.buildMode, 'production'); @@ -190,14 +175,9 @@ test('command: build, mode: production', async () => { assert.deepEqual(build.emptyOutDir, undefined); assert.deepEqual(rollupOptions.input, [normalizePath(resolve(cwd, 'src', 'root'))]); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, 'build/q-[hash].js'); - assert.deepEqual(outputOptionsObj.entryFileNames, 'build/q-[hash].js'); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, 'build/q-[hash].js'); + assert.deepEqual(outputOptions.entryFileNames, 'build/q-[hash].js'); assert.deepEqual(build.outDir, normalizePath(resolve(cwd, 'dist'))); assert.deepEqual(build.dynamicImportVarsOptions?.exclude, [/./]); @@ -248,7 +228,7 @@ test('command: build, --ssr entry.server.tsx', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'ssr'); assert.deepEqual(opts.buildMode, 'development'); @@ -261,14 +241,9 @@ test('command: build, --ssr entry.server.tsx', async () => { assert.deepEqual(build.emptyOutDir, undefined); assert.deepEqual(rollupOptions.input, [normalizePath(resolve(cwd, 'src', 'entry.server.tsx'))]); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, undefined); - assert.deepEqual(outputOptionsObj.entryFileNames, undefined); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, undefined); + assert.deepEqual(outputOptions.entryFileNames, undefined); assert.deepEqual(build.outDir, normalizePath(resolve(cwd, 'server'))); assert.deepEqual(build.dynamicImportVarsOptions?.exclude, [/./]); @@ -396,7 +371,7 @@ test('command: build, --mode lib', async () => { const opts = await plugin.api?.getOptions(); const build = c.build!; const rollupOptions = build!.rollupOptions!; - const outputOptions = rollupOptions.output as Rollup.OutputOptions[]; + const outputOptions = rollupOptions.output as Rollup.OutputOptions; assert.deepEqual(opts.target, 'lib'); assert.deepEqual(opts.buildMode, 'development'); @@ -404,13 +379,8 @@ test('command: build, --mode lib', async () => { assert.deepEqual(build.ssr, undefined); assert.deepEqual(rollupOptions.input, [normalizePath(resolve(cwd, 'src', 'index.ts'))]); - assert.ok(Array.isArray(outputOptions)); - assert.lengthOf(outputOptions, 1); - - outputOptions.forEach((outputOptionsObj) => { - assert.deepEqual(outputOptionsObj.assetFileNames, 'build/q-[hash].[ext]'); - assert.deepEqual(outputOptionsObj.chunkFileNames, undefined); - }); + assert.deepEqual(outputOptions.assetFileNames, 'build/q-[hash].[ext]'); + assert.deepEqual(outputOptions.chunkFileNames, undefined); assert.deepEqual(c.build.outDir, normalizePath(resolve(cwd, 'lib'))); assert.deepEqual(build.emptyOutDir, undefined);