Skip to content

Commit

Permalink
fix(qwikVite): multiple outputs logic (#6723)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingSora authored Jul 28, 2024
1 parent aac2f47 commit fb59aa6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 74 deletions.
31 changes: 17 additions & 14 deletions packages/qwik/src/optimizer/src/plugins/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions packages/qwik/src/optimizer/src/plugins/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
80 changes: 25 additions & 55 deletions packages/qwik/src/optimizer/src/plugins/vite.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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, [/./]);
Expand Down Expand Up @@ -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');
Expand All @@ -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, [/./]);
Expand Down Expand Up @@ -396,21 +371,16 @@ 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');
assert.deepEqual(build.minify, false);
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);
Expand Down

0 comments on commit fb59aa6

Please sign in to comment.