refactor(typescript): use an object for _typescript (#5418)

This commit is contained in:
Pooya Parsa 2019-03-30 00:39:20 +04:30 committed by GitHub
parent 6ffc5c5792
commit 832bd12091
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 13 deletions

View File

@ -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')}`)
}

View File

@ -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
}

View File

@ -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,

View File

@ -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()
})