@nx/nuxt
Version:
52 lines (51 loc) • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
const tsquery_1 = require("@phenomnomnominal/tsquery");
async function default_1(tree) {
(0, devkit_1.visitNotIgnoredFiles)(tree, '', (path) => {
if (!path.endsWith('.storybook/main.ts') &&
!path.endsWith('.storybook/main.js')) {
return;
}
const projectRoot = (0, path_1.dirname)((0, path_1.dirname)(path));
const possibleNuxtConfigPaths = [
(0, devkit_1.joinPathFragments)(projectRoot, 'nuxt.config.ts'),
(0, devkit_1.joinPathFragments)(projectRoot, 'nuxt.config.js'),
];
if (!possibleNuxtConfigPaths.some((p) => tree.exists(p))) {
return;
}
const pathToStorybookConfig = path;
const storybookConfigContents = tree.read(pathToStorybookConfig, 'utf-8');
if (!storybookConfigContents.includes('viteFinal') &&
!storybookConfigContents.includes('@vitejs/plugin-vue')) {
return;
}
const VITE_FINAL_PLUGINS_SELECTOR = 'PropertyAssignment:has(Identifier[name=viteFinal]) PropertyAssignment:has(Identifier[name=plugins]) > ArrayLiteralExpression';
const ast = tsquery_1.tsquery.ast(storybookConfigContents);
const nodes = (0, tsquery_1.tsquery)(ast, VITE_FINAL_PLUGINS_SELECTOR, {
visitAllChildren: true,
});
if (!nodes.length) {
// This would be an invalid config modified by the user already if it does work
// Therefore, do not touch their config file
return;
}
const pluginsValueNode = nodes[0];
if (pluginsValueNode.getText().includes('vue()')) {
// The plugin has already been registered, do nothing
return;
}
const updatedPluginsValue = `[vue(), ${pluginsValueNode
.getText()
.slice(1)}`;
let newStorybookConfigContents = `${storybookConfigContents.slice(0, pluginsValueNode.getStart())}${updatedPluginsValue}${storybookConfigContents.slice(pluginsValueNode.getEnd())}`;
newStorybookConfigContents = `import vue from '@vitejs/plugin-vue';
${newStorybookConfigContents}`;
tree.write(pathToStorybookConfig, newStorybookConfigContents);
});
await (0, devkit_1.formatFiles)(tree);
}