-
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): add nodes - fixing vite build issues
- Loading branch information
Showing
11 changed files
with
180 additions
and
145 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
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,108 @@ | ||
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; | ||
devDependencies['vitest'] = vitestVersion; | ||
devDependencies['@vitest/ui'] = vitestUiVersion; | ||
|
||
if (process.env['NX_PCV3'] === 'true') { | ||
devDependencies['@nx/vite/plugin'] = nxVersion; | ||
} | ||
|
||
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); | ||
} |
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
Oops, something went wrong.