From 832bd1209103f424ddfd67faa9871ab302fdafb8 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 30 Mar 2019 00:39:20 +0430 Subject: [PATCH] refactor(typescript): use an object for _typescript (#5418) --- packages/cli/src/utils/index.js | 2 +- packages/cli/src/utils/typescript.js | 26 ++++++++++++++++++-------- packages/webpack/src/config/client.js | 5 ++--- test/unit/typescript.modern.test.js | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/cli/src/utils/index.js b/packages/cli/src/utils/index.js index 7b4a7ad930..afad65561d 100644 --- a/packages/cli/src/utils/index.js +++ b/packages/cli/src/utils/index.js @@ -101,7 +101,7 @@ export function showBanner(nuxt) { // Running mode titleLines.push(`Running in ${nuxt.options.dev ? chalk.bold.blue('development') : chalk.bold.green('production')} mode (${chalk.bold(nuxt.options.mode)})`) - if (nuxt.options._typescript) { + if (nuxt.options._typescript && nuxt.options._typescript.runtime) { titleLines.push(`TypeScript support is ${chalk.green.bold('enabled')}`) } diff --git a/packages/cli/src/utils/typescript.js b/packages/cli/src/utils/typescript.js index da91bacd15..3e4429179b 100644 --- a/packages/cli/src/utils/typescript.js +++ b/packages/cli/src/utils/typescript.js @@ -2,7 +2,7 @@ import path from 'path' import fs from 'fs-extra' import * as imports from '../imports' -async function registerTSNode(tsConfigPath, options) { +async function registerTSNode({ tsConfigPath, options }) { const { register } = await imports.tsNode() // https://github.com/TypeStrong/ts-node @@ -26,24 +26,34 @@ async function getNuxtTypeScript() { } export async function detectTypeScript(rootDir, options = {}) { - // Check if tsconfig.json exists in project rootDir - const tsConfigPath = path.resolve(rootDir, 'tsconfig.json') + const typescript = { + tsConfigPath: path.resolve(rootDir, 'tsconfig.json'), + tsConfigExists: false, + runtime: false, + build: false, + options + } + + // Check if tsconfig.json exists + typescript.tsConfigExists = await fs.exists(typescript.tsConfigPath) // Skip if tsconfig.json not exists - if (!await fs.exists(tsConfigPath)) { - return false + if (!typescript.tsConfigExists) { + return typescript } // Register runtime support - await registerTSNode(tsConfigPath, options) + typescript.runtime = true + await registerTSNode(typescript) // Try to load @nuxt/typescript const nuxtTypeScript = await getNuxtTypeScript() // If exists do additional setup if (nuxtTypeScript) { - await nuxtTypeScript.setupDefaults(tsConfigPath) + typescript.build = true + await nuxtTypeScript.setupDefaults(typescript.tsConfigPath) } - return true + return typescript } diff --git a/packages/webpack/src/config/client.js b/packages/webpack/src/config/client.js index 6642105240..77383b2cbb 100644 --- a/packages/webpack/src/config/client.js +++ b/packages/webpack/src/config/client.js @@ -76,7 +76,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig { plugins() { const plugins = super.plugins() - const { buildOptions, options: { appTemplatePath, buildDir, modern, rootDir, _typescript } } = this.buildContext + const { buildOptions, options: { appTemplatePath, buildDir, modern, rootDir, _typescript = {} } } = this.buildContext // Generate output HTML for SSR if (buildOptions.ssr) { @@ -139,8 +139,7 @@ export default class WebpackClientConfig extends WebpackBaseConfig { // TypeScript type checker // Only performs once per client compilation and only if `ts-loader` checker is not used (transpileOnly: true) - if (_typescript && buildOptions.typescript && buildOptions.typescript.typeCheck && !this.isModern && this.loaders.ts.transpileOnly) { - // We assume that "_typescript" being truthy means @nuxt/typescript is installed <=> fork-ts-checker-webpack-plugin is installed + if (_typescript.build && buildOptions.typescript && buildOptions.typescript.typeCheck && !this.isModern && this.loaders.ts.transpileOnly) { const ForkTsCheckerWebpackPlugin = require(this.buildContext.nuxt.resolver.resolveModule('fork-ts-checker-webpack-plugin')) plugins.push(new ForkTsCheckerWebpackPlugin(Object.assign({ vue: true, diff --git a/test/unit/typescript.modern.test.js b/test/unit/typescript.modern.test.js index 4f934f653e..a14bafe199 100644 --- a/test/unit/typescript.modern.test.js +++ b/test/unit/typescript.modern.test.js @@ -8,7 +8,7 @@ describe('typescript modern', () => { beforeAll(async () => { const options = await loadFixture('typescript') - nuxt = new Nuxt(Object.assign(options, { modern: true, _typescript: true })) + nuxt = new Nuxt(Object.assign(options, { modern: true, _typescript: { build: true } })) await new Builder(nuxt, BundleBuilder).build() })