-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite): nodes for build, serve, test, preview targets
- Loading branch information
Showing
19 changed files
with
633 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { createNodes, VitePluginOptions } from './src/plugins/plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
logger, | ||
readJson, | ||
readNxJson, | ||
Tree, | ||
updateJson, | ||
updateNxJson, | ||
} from '@nx/devkit'; | ||
|
||
import { | ||
edgeRuntimeVmVersion, | ||
happyDomVersion, | ||
jsdomVersion, | ||
nxVersion, | ||
vitePluginDtsVersion, | ||
vitePluginReactSwcVersion, | ||
vitePluginReactVersion, | ||
vitestUiVersion, | ||
vitestVersion, | ||
viteVersion, | ||
} from '../../../utils/versions'; | ||
import { InitGeneratorSchema } from '../schema'; | ||
|
||
export function checkDependenciesInstalled( | ||
host: Tree, | ||
schema: InitGeneratorSchema | ||
) { | ||
const packageJson = readJson(host, 'package.json'); | ||
const devDependencies = {}; | ||
const dependencies = {}; | ||
packageJson.dependencies = packageJson.dependencies || {}; | ||
packageJson.devDependencies = packageJson.devDependencies || {}; | ||
|
||
// base deps | ||
devDependencies['@nx/vite'] = nxVersion; | ||
devDependencies['vite'] = viteVersion; | ||
|
||
// Do not install latest version if vitest already exists | ||
// because version 0.32 and newer versions break nuxt-vitest | ||
// https://github.com/vitest-dev/vitest/issues/3540 | ||
// https://github.com/danielroe/nuxt-vitest/issues/213#issuecomment-1588728111 | ||
if ( | ||
!packageJson.dependencies['vitest'] && | ||
!packageJson.devDependencies['vitest'] | ||
) { | ||
devDependencies['vitest'] = vitestVersion; | ||
} | ||
if ( | ||
!packageJson.dependencies['@vitest/ui'] && | ||
!packageJson.devDependencies['@vitest/ui'] | ||
) { | ||
devDependencies['@vitest/ui'] = vitestVersion; | ||
} | ||
|
||
if (schema.testEnvironment === 'jsdom') { | ||
devDependencies['jsdom'] = jsdomVersion; | ||
} else if (schema.testEnvironment === 'happy-dom') { | ||
devDependencies['happy-dom'] = happyDomVersion; | ||
} else if (schema.testEnvironment === 'edge-runtime') { | ||
devDependencies['@edge-runtime/vm'] = edgeRuntimeVmVersion; | ||
} else if (schema.testEnvironment !== 'node' && schema.testEnvironment) { | ||
logger.info( | ||
`A custom environment was provided: ${schema.testEnvironment}. You need to install it manually.` | ||
); | ||
} | ||
|
||
if (schema.uiFramework === 'react') { | ||
if (schema.compiler === 'swc') { | ||
devDependencies['@vitejs/plugin-react-swc'] = vitePluginReactSwcVersion; | ||
} else { | ||
devDependencies['@vitejs/plugin-react'] = vitePluginReactVersion; | ||
} | ||
} | ||
|
||
if (schema.includeLib) { | ||
devDependencies['vite-plugin-dts'] = vitePluginDtsVersion; | ||
} | ||
|
||
return addDependenciesToPackageJson(host, dependencies, devDependencies); | ||
} | ||
|
||
export function moveToDevDependencies(tree: Tree) { | ||
updateJson(tree, 'package.json', (packageJson) => { | ||
packageJson.dependencies = packageJson.dependencies || {}; | ||
packageJson.devDependencies = packageJson.devDependencies || {}; | ||
|
||
if (packageJson.dependencies['@nx/vite']) { | ||
packageJson.devDependencies['@nx/vite'] = | ||
packageJson.dependencies['@nx/vite']; | ||
delete packageJson.dependencies['@nx/vite']; | ||
} | ||
return packageJson; | ||
}); | ||
} | ||
|
||
export function createVitestConfig(tree: Tree) { | ||
const nxJson = readNxJson(tree); | ||
|
||
const productionFileSet = nxJson.namedInputs?.production; | ||
if (productionFileSet) { | ||
productionFileSet.push( | ||
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)', | ||
'!{projectRoot}/tsconfig.spec.json' | ||
); | ||
|
||
nxJson.namedInputs.production = Array.from(new Set(productionFileSet)); | ||
} | ||
|
||
nxJson.targetDefaults ??= {}; | ||
nxJson.targetDefaults['@nx/vite:test'] ??= {}; | ||
nxJson.targetDefaults['@nx/vite:test'].cache ??= true; | ||
nxJson.targetDefaults['@nx/vite:test'].inputs ??= [ | ||
'default', | ||
productionFileSet ? '^production' : '^default', | ||
]; | ||
|
||
updateNxJson(tree, nxJson); | ||
} | ||
|
||
export function addPlugin(tree: Tree) { | ||
const nxJson = readNxJson(tree); | ||
nxJson.plugins ??= []; | ||
|
||
for (const plugin of nxJson.plugins) { | ||
if ( | ||
typeof plugin === 'string' | ||
? plugin === '@nx/vite/plugin' | ||
: plugin.plugin === '@nx/vite/plugin' | ||
) { | ||
return; | ||
} | ||
} | ||
|
||
nxJson.plugins.push({ | ||
plugin: '@nx/vite/plugin', | ||
options: { | ||
buildTargetName: 'build', | ||
previewTargetName: 'preview', | ||
testTargetName: 'test', | ||
serveTargetName: 'serve', | ||
}, | ||
}); | ||
updateNxJson(tree, nxJson); | ||
} |
Oops, something went wrong.