Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,34 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
}
}

// `typescript` is pinned here rather than left to `@nx/js:init` so it lands in
// package.json before the first install. Otherwise npm resolves tsquery's
// `typescript: >3.0.0` peer to 7.x, whose entry point dropped the compiler API.
function getPresetDependencies({
preset,
presetVersion,
bundler,
e2eTestRunner,
js,
}: NormalizedSchema) {
switch (preset) {
// Empty workspaces — no preset generator runs, so `@nx/js:init` never adds
// typescript and pinning it here would be net-new.
case Preset.Apps:
case Preset.NPM:
return { dependencies: {}, dev: { '@nx/js': nxVersion } };

case Preset.TS:
case Preset.TsStandalone:
return { dependencies: {}, dev: { '@nx/js': nxVersion } };
return {
dependencies: {},
dev: {
'@nx/js': nxVersion,
// ts-standalone prompts for JS vs TS; mirror `@nx/js:init`, which
// skips typescript when `js` is set.
typescript: js ? undefined : typescriptVersion,
},
};

case Preset.AngularMonorepo:
case Preset.AngularStandalone:
Expand All @@ -140,7 +156,10 @@ function getPresetDependencies({
};

case Preset.Express:
return { dependencies: {}, dev: { '@nx/express': nxVersion } };
return {
dependencies: {},
dev: { '@nx/express': nxVersion, typescript: typescriptVersion },
};

case Preset.Nest:
return {
Expand All @@ -150,7 +169,10 @@ function getPresetDependencies({

case Preset.NextJs:
case Preset.NextJsStandalone:
return { dependencies: { '@nx/next': nxVersion }, dev: {} };
return {
dependencies: { '@nx/next': nxVersion },
dev: { typescript: typescriptVersion },
};

case Preset.VueMonorepo:
case Preset.VueStandalone:
Expand All @@ -162,6 +184,7 @@ function getPresetDependencies({
'@nx/playwright':
e2eTestRunner === 'playwright' ? nxVersion : undefined,
'@nx/vite': nxVersion,
typescript: typescriptVersion,
},
};

Expand All @@ -174,6 +197,7 @@ function getPresetDependencies({
'@nx/cypress': e2eTestRunner === 'cypress' ? nxVersion : undefined,
'@nx/playwright':
e2eTestRunner === 'playwright' ? nxVersion : undefined,
typescript: typescriptVersion,
},
};

Expand All @@ -189,14 +213,21 @@ function getPresetDependencies({
'@nx/jest': bundler !== 'vite' ? nxVersion : undefined,
'@nx/vite': bundler === 'vite' ? nxVersion : undefined,
'@nx/webpack': bundler === 'webpack' ? nxVersion : undefined,
typescript: typescriptVersion,
},
};

case Preset.ReactNative:
return { dependencies: {}, dev: { '@nx/react-native': nxVersion } };
return {
dependencies: {},
dev: { '@nx/react-native': nxVersion, typescript: typescriptVersion },
};

case Preset.Expo:
return { dependencies: {}, dev: { '@nx/expo': nxVersion } };
return {
dependencies: {},
dev: { '@nx/expo': nxVersion, typescript: typescriptVersion },
};

case Preset.WebComponents:
return {
Expand All @@ -211,6 +242,7 @@ function getPresetDependencies({
dev: {
'@nx/node': nxVersion,
'@nx/webpack': bundler === 'webpack' ? nxVersion : undefined,
typescript: typescriptVersion,
},
};

Expand Down
53 changes: 53 additions & 0 deletions packages/workspace/src/generators/new/new.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,56 @@ describe('new', () => {
expect(readJson(tree, 'my-workspace/package.json')).toMatchSnapshot();
});

it('should not add typescript for presets that scaffold nothing', async () => {
for (const preset of [Preset.Apps, Preset.NPM]) {
tree = createTree();
tree.root = process.cwd();

await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
preset,
});

const { devDependencies } = readJson(tree, 'my-workspace/package.json');
expect(devDependencies).not.toHaveProperty('typescript');
}
});

it('should generate necessary npm dependencies for ts preset', async () => {
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
preset: Preset.TS,
});

const { devDependencies } = readJson(tree, 'my-workspace/package.json');
expect(devDependencies).toStrictEqual({
'@nx/js': nxVersion,
'@nx/workspace': nxVersion,
nx: nxVersion,
typescript: typescriptVersion,
});
});

it('should not add typescript for ts-standalone preset when using js', async () => {
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
appName: 'app',
preset: Preset.TsStandalone,
js: true,
});

const { devDependencies } = readJson(tree, 'my-workspace/package.json');
expect(devDependencies).not.toHaveProperty('typescript');
});

it('should generate necessary npm dependencies for react preset', async () => {
await newGenerator(tree, {
...defaultOptions,
Expand All @@ -111,6 +161,7 @@ describe('new', () => {
'@nx/vite': nxVersion,
'@nx/workspace': nxVersion,
nx: nxVersion,
typescript: typescriptVersion,
});
});

Expand All @@ -131,6 +182,7 @@ describe('new', () => {
'@nx/vite': nxVersion,
'@nx/workspace': nxVersion,
nx: nxVersion,
typescript: typescriptVersion,
});
});

Expand All @@ -150,6 +202,7 @@ describe('new', () => {
'@nx/cypress': nxVersion,
'@nx/workspace': nxVersion,
nx: nxVersion,
typescript: typescriptVersion,
});
});

Expand Down
Loading