Skip to content

Commit 87dcc5b

Browse files
fix(qwik-nx): storybook plugin should not add @qwik-city-sw-register to rollupOptions.external (#218)
1 parent b97d16c commit 87dcc5b

File tree

6 files changed

+52
-39
lines changed

6 files changed

+52
-39
lines changed

packages/qwik-nx/src/generators/storybook-configuration/__snapshots__/generator.spec.ts.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`storybook-configuration generator should add required targets 1`] = `
4-
"import { mergeConfig, UserConfig } from 'vite';
4+
"import { UserConfig } from 'vite';
55
import { withNx } from 'qwik-nx/storybook';
6-
import viteConfig from './../vite.config';
76
87
const config = {
98
stories: ['../**/*.stories.mdx', '../**/*.stories.@(js|jsx|ts|tsx)'],
109
addons: ['@storybook/addon-essentials'],
1110
framework: { name: 'storybook-framework-qwik' },
1211
async viteFinal(config: UserConfig) {
13-
const updatedConfig = mergeConfig(config, viteConfig);
14-
return withNx(updatedConfig);
12+
return withNx(config);
1513
},
1614
};
1715

packages/qwik-nx/src/generators/storybook-configuration/files/.storybook/main.__configExtension__.template

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { mergeConfig, UserConfig } from 'vite';
1+
import { UserConfig } from 'vite';
22
import { withNx } from 'qwik-nx/storybook';
3-
import viteConfig from './../vite.config';
43

54
const config = {
65
stories: [
@@ -10,8 +9,7 @@ const config = {
109
addons: ['@storybook/addon-essentials'],
1110
framework: { name: 'storybook-framework-qwik', },
1211
async viteFinal(config: UserConfig) {
13-
const updatedConfig = mergeConfig(config, viteConfig);
14-
return withNx(updatedConfig);
12+
return withNx(config<% if(isLib) { %>, true <% } %>);
1513
},
1614
};
1715

packages/qwik-nx/src/generators/storybook-configuration/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
function addFiles(
3232
tree: Tree,
3333
options: StorybookConfigurationGeneratorSchema,
34-
{ root }: ProjectConfiguration
34+
{ root, projectType }: ProjectConfiguration
3535
) {
3636
tree.delete(path.join(root, '.storybook/main.js'));
3737

@@ -41,6 +41,7 @@ function addFiles(
4141
offsetFromRoot: offsetFromRoot(root),
4242
projectRoot: root,
4343
configExtension: options.tsConfiguration ? 'ts' : 'js',
44+
isLib: projectType === 'library',
4445
};
4546
generateFiles(tree, path.join(__dirname, 'files'), root, templateOptions);
4647

packages/qwik-nx/src/plugins/qwik-nx-vite.plugin.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
QwikVitePluginStub,
55
} from './models/qwik-nx-vite';
66
import { getVendorRoots } from './utils/get-vendor-roots';
7+
import { output } from '@nx/devkit';
78

89
/**
910
* `qwikNxVite` plugin serves as an integration step between Qwik and Nx.
@@ -21,18 +22,28 @@ export function qwikNxVite(options?: QwikNxVitePluginOptions): Plugin {
2122
enforce: 'pre',
2223

2324
async configResolved(config) {
24-
const qwikPlugin = config.plugins.find(
25+
const qwikPlugins = config.plugins.filter(
2526
(p) => p.name === 'vite-plugin-qwik'
26-
) as QwikVitePluginStub;
27-
if (!qwikPlugin) {
27+
) as QwikVitePluginStub[];
28+
if (!qwikPlugins.length) {
2829
throw new Error('Missing vite-plugin-qwik');
2930
}
3031

31-
const pluginOptions = qwikPlugin.api.getOptions();
32+
if (qwikPlugins.length > 1) {
33+
output.warn({
34+
title:
35+
'Multiple instances of "vite-plugin-qwik" found. Check your vite.config!',
36+
});
37+
}
38+
39+
for (const qwikPlugin of qwikPlugins) {
40+
// it's not expected to have the plugin duplicated, but handling it as an edge case
41+
const pluginOptions = qwikPlugin.api.getOptions();
3242

33-
const vendorRoots = await getVendorRoots(options, pluginOptions);
43+
const vendorRoots = await getVendorRoots(options, pluginOptions);
3444

35-
pluginOptions.vendorRoots.push(...vendorRoots);
45+
pluginOptions.vendorRoots.push(...vendorRoots);
46+
}
3647
},
3748
};
3849

packages/qwik-nx/src/plugins/utils/get-vendor-roots.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function getVendorRoots(
7676

7777
if (options?.debug) {
7878
log(
79-
'Projects after excluding those not in tsconfig.base.json:',
79+
'Final projects list after excluding those not in tsconfig.base.json:',
8080
projects.map((p) => p.name)
8181
);
8282
}

packages/qwik-nx/src/utils/storybook.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,36 @@ import {
1111
*/
1212
export function withNx(
1313
config: UserConfig,
14+
excludeQwikCitySwRegister = false,
1415
qwikViteOpts?: QwikVitePluginOptions
1516
): UserConfig {
16-
const updated: UserConfig = mergeConfig(config, {
17-
build: {
18-
rollupOptions: {
19-
external: ['@qwik-city-sw-register'],
20-
},
21-
},
22-
});
23-
updated.plugins = updated.plugins?.flat(10).map((plugin: PluginOption) => {
24-
switch ((plugin as any)?.name) {
25-
case 'vite-plugin-qwik':
26-
// as of now there's no way of extending qwikVite with overridden output paths, thus have to override to completely
27-
return qwikVite(qwikViteOpts);
17+
const updated: UserConfig = excludeQwikCitySwRegister
18+
? mergeConfig(config, {
19+
build: {
20+
rollupOptions: {
21+
external: ['@qwik-city-sw-register'],
22+
},
23+
},
24+
})
25+
: { ...config };
26+
return {
27+
...updated,
28+
plugins: config.plugins?.flat(10).map((plugin: PluginOption) => {
29+
switch ((plugin as any)?.name) {
30+
case 'vite-plugin-qwik':
31+
// as of now there's no way of extending qwikVite with overridden output paths, thus have to override to completely
32+
return qwikVite(qwikViteOpts);
2833

29-
case 'vite-plugin-qwik-city':
30-
// logic below has been copied from "storybook-framework-qwik" plugin
31-
// it doesn't work out of the box for Nx applications because base config is not included by storybook if it's not in the root cwd
32-
// Qwik-city plugin may be used in apps, but it has mdx stuff that conflicts with Storybook mdx
33-
// we'll try to only remove the transform code (where the mdx stuff is), and keep everything else.
34-
return { ...plugin, transform: () => null } as PluginOption;
34+
case 'vite-plugin-qwik-city':
35+
// logic below has been copied from "storybook-framework-qwik" plugin
36+
// it doesn't work out of the box for Nx applications because base config is not included by storybook if it's not in the root cwd
37+
// Qwik-city plugin may be used in apps, but it has mdx stuff that conflicts with Storybook mdx
38+
// we'll try to only remove the transform code (where the mdx stuff is), and keep everything else.
39+
return { ...plugin, transform: () => null } as PluginOption;
3540

36-
default:
37-
return plugin;
38-
}
39-
});
40-
return updated;
41+
default:
42+
return plugin;
43+
}
44+
}),
45+
};
4146
}

0 commit comments

Comments
 (0)