Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ts): auto generate tsconfig.json #4776

Merged
merged 24 commits into from Jan 23, 2019
Merged
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
94f5386
Bundle `@types/node` into `nuxt-ts` distribution
kevinmarrec Jan 16, 2019
ffdb67c
feat(ts): Autogenerated tsconfig.json
kevinmarrec Jan 16, 2019
f2c9cb0
fix(examples): Update typescript examples
kevinmarrec Jan 16, 2019
0591cfa
fix(ts): Add `dist` & `tsconfig.json` to `pakage.json` files
kevinmarrec Jan 16, 2019
a82f4a5
fix: add missing new line at the end of generated `tsconfig.json`
kevinmarrec Jan 17, 2019
5a43f09
add suffix if needed to `tsconfig.json` types during package build
kevinmarrec Jan 17, 2019
ea49d17
use `readJSON` over `readJSONSync`
kevinmarrec Jan 17, 2019
d925c69
fix indentation in `nuxt-ts/tsconfig.json`
kevinmarrec Jan 17, 2019
bcd9a61
use writeJson instead of writeFile
kevinmarrec Jan 17, 2019
0397272
split `nuxt-ts/src` in 2 functions + rootDir parametr + add tests
kevinmarrec Jan 17, 2019
1028711
add `fs-extra` to dependencies to prevent build warning
kevinmarrec Jan 18, 2019
1db91a6
JSON everywhere instead of JSON/Json
kevinmarrec Jan 18, 2019
9358a93
fix: missing call to generateTsConfigIfMissing
kevinmarrec Jan 18, 2019
18ce8ad
Merge branch 'dev' into autogenerate-tsconfig
kevinmarrec Jan 19, 2019
c349626
ask confirmation for tsconfig.json generation
kevinmarrec Jan 19, 2019
909bd8e
improvement: let compilerOptions.module as default
kevinmarrec Jan 20, 2019
fa053bc
improvements
kevinmarrec Jan 20, 2019
1bb8ec2
move from github link to friendly linkable local path
kevinmarrec Jan 20, 2019
b0f475b
add resolveJsonModule in default config
husayt Jan 21, 2019
7f3d6f3
Merge remote-tracking branch 'official/dev' into autogenerate-tsconfig
kevinmarrec Jan 21, 2019
5cb2419
Move `nuxt-ts` logic to `@nuxt/typescript`
kevinmarrec Jan 21, 2019
ce3bf2d
Merge remote-tracking branch 'official/dev' into autogenerate-tsconfig
kevinmarrec Jan 21, 2019
473b464
Merge branch 'dev' into autogenerate-tsconfig
pi0 Jan 22, 2019
0976aa2
Upgrade to `ts-node@8.0.1`
kevinmarrec Jan 22, 2019
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -3,16 +3,17 @@
// Globally indicate we are running in ts mode
process.env.NUXT_TS = 'true'

// https://github.com/TypeStrong/ts-node
require('ts-node').register({
compilerOptions: {
module: 'commonjs'
}
})
// rootDir should be set by a CLI helper to handle cases like `nuxt-ts path/to/project`
const rootDir = process.cwd()

const { generateTsConfigIfMissing, registerTsNode } = require('..')

const suffix = require('../package.json').name.includes('-edge') ? '-edge' : ''
require('@nuxt/cli' + suffix).run()
.catch((error) => {
require('consola').fatal(error)
process.exit(2)
})
generateTsConfigIfMissing(rootDir).then(() => {
registerTsNode()
const suffix = require('../package.json').name.includes('-edge') ? '-edge' : ''
require('@nuxt/cli' + suffix).run()
.catch((error) => {
require('consola').fatal(error)
process.exit(2)
})
})
@@ -1,5 +1,7 @@
import { readJSON, writeJSON } from 'fs-extra'

export default {
build: false,
build: true,
hooks: {
async 'build:done'(pkg) {
const mono = pkg.load('../..')
@@ -18,6 +20,17 @@ export default {
])

await pkg.writePackage()

if (pkg.options.suffix && pkg.options.linkedDependencies) {
const tsconfig = await readJSON(pkg.resolvePath('tsconfig.json'))

tsconfig.compilerOptions.types = tsconfig.compilerOptions.types.map((type) => {
const suffix = pkg.options.linkedDependencies.includes(type) ? pkg.options.suffix : ''
return type + suffix
})

await writeJSON(pkg.resolvePath('tsconfig.json'), tsconfig, { spaces: 2 })
}
}
}
}
@@ -40,8 +40,11 @@
}
],
"files": [
"bin"
"bin",
"dist",
"tsconfig.json"
],
"main": "dist/nuxt-ts.js",
"bin": {
"nuxt-ts": "bin/nuxt-ts.js",
"nuxts": "bin/nuxt-ts.js"
@@ -53,7 +56,13 @@
"@nuxt/generator": "2.3.4",
"@nuxt/opencollective": "^0.2.1",
"@nuxt/webpack": "2.3.4",
"@types/node": "^10.12.18",
"chalk": "^2.4.2",
"consola": "^2.3.2",
"enquirer": "^2.3.0",
"fork-ts-checker-webpack-plugin": "^0.5.2",
"fs-extra": "^7.0.1",
"std-env": "^2.2.1",
"ts-loader": "^5.3.3",
"ts-node": "^7.0.1",
"tslint": "^5.12.1",
@@ -0,0 +1,38 @@
import { resolve } from 'path'
import chalk from 'chalk'
import consola from 'consola'
import env from 'std-env'
import { existsSync, writeJSON } from 'fs-extra'
import { register } from 'ts-node'
import { prompt } from 'enquirer'

export async function generateTsConfigIfMissing(rootDir) {
const tsConfigPath = resolve(rootDir, 'tsconfig.json')

if (!existsSync(tsConfigPath)) {
const { confirmGeneration } = await prompt({
type: 'confirm',
name: 'confirmGeneration',
message: `Missing ${chalk.bold.blue('tsconfig.json')} in ${rootDir === process.cwd() ? 'current directory' : chalk.bold.green(resolve(rootDir))}, generate it ?`,
initial: true,
skip: env.minimal
})

if (confirmGeneration) {
const configToExtend = 'nuxt-ts'
await writeJSON(tsConfigPath, {
This conversation was marked as resolved by kevinmarrec

This comment has been minimized.

Copy link
@pi0

pi0 Jan 20, 2019

Member

Idea: We can use .json5 for tsconfig and annotate it with some comments. Like links to docs or indicating it is an autogenerated file.

This comment has been minimized.

Copy link
@pi0

pi0 Jan 20, 2019

Member

(Not sure. But probably typescript supports json5 by default for tsconfig.json files too)

This comment has been minimized.

Copy link
@kevinmarrec

kevinmarrec Jan 20, 2019

Author Member

Gonna take a look, thanks

This comment has been minimized.

Copy link
@kevinmarrec

kevinmarrec Jan 20, 2019

Author Member

@pi0 I think we can't do it, I tried things but got issues.
Gonna add a consola.info with a link to the extended tsconfig.json

This comment has been minimized.

Copy link
@pi0

pi0 Jan 20, 2019

Member

Ok. Thanks for your time on it ☺️

extends: configToExtend,
compilerOptions: {
baseUrl: '.'
}
}, { spaces: 2 })
consola.info(`Extending ${chalk.bold.blue(`node_modules/${configToExtend}/tsconfig.json`)}`)
consola.success(`Generated successfully at ${chalk.bold.green(resolve(rootDir, 'tsconfig.json'))}`)
}
}
}

export function registerTsNode() {
// https://github.com/TypeStrong/ts-node
register()
}
@@ -0,0 +1,33 @@
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"lib": [
"esnext",
"esnext.asynciterable",
"dom"
],
"esModuleInterop": true,
This conversation was marked as resolved by kevinmarrec

This comment has been minimized.

Copy link
@husayt

husayt Jan 21, 2019

Collaborator

@kevinmarrec might be worth to add resolveJsonModule": true, to allow frictionless import of json files

Suggested change
"esModuleInterop": true,
"esModuleInterop": true,
"resolveJsonModule": true,

This comment has been minimized.

Copy link
@kevinmarrec

kevinmarrec Jan 21, 2019

Author Member

Yeah thanks !

"experimentalDecorators": true,
"jsx": "preserve",
"sourceMap": true,
"strict": true,
"noImplicitAny": true,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/vue-app"
]
}
}
@@ -16,7 +16,6 @@
"lint": "tslint --project ."
},
"devDependencies": {
"@types/node": "^10.12.18",
"tslint-config-standard": "^8.0.1"
}
}
@@ -1,27 +1,7 @@
{
"extends": "nuxt-ts-edge",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["esnext", "esnext.asynciterable", "dom"],
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"baseUrl": ".",
"paths": {
"~/*": ["./*"]
},
"types": [
"@types/node",
"@nuxt/vue-app-edge"
]
"noImplicitAny": false
}
}
@@ -13,7 +13,6 @@
"lint": "tslint --project ."
},
"devDependencies": {
"@types/node": "^10.12.18",
"tslint-config-standard": "^8.0.1"
}
}
@@ -1,27 +1,6 @@
{
"extends": "nuxt-ts-edge",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["esnext", "esnext.asynciterable", "dom"],
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": true,
"baseUrl": ".",
"paths": {
"~/*": ["./*"]
},
"types": [
"@types/node",
"@nuxt/vue-app-edge"
]
"baseUrl": "."
}
}
@@ -1,25 +1,6 @@
{
"extends": "nuxt-ts",
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["esnext", "esnext.asynciterable", "dom"],
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"jsx": "preserve",
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": ".",
"paths": {
"~/*": ["./*"]
},
"types": [
"@nuxt/vue-app"
]
"noImplicitAny": false
}
}
@@ -0,0 +1,35 @@
import { resolve } from 'path'
import { mkdirp, exists, readJSON, remove } from 'fs-extra'
import { register } from 'ts-node'
import { generateTsConfigIfMissing, registerTsNode } from '../../distributions/nuxt-ts/src'

jest.mock('ts-node')

describe('nuxt-ts', () => {
test('generate tsconfig.json if missing', async () => {
const rootDir = 'tmp'

// We're assuming that rootDir provided to generateTsConfigIfMissing is existing so we create the tested one
await mkdirp(rootDir)

await generateTsConfigIfMissing(rootDir)

const tsConfigPath = resolve(rootDir, 'tsconfig.json')

expect(await exists(tsConfigPath)).toBe(true)
expect(await readJSON(tsConfigPath)).toEqual({
extends: 'nuxt-ts',
compilerOptions: {
baseUrl: '.'
}
})

// Clean workspace by removing the temporary folder (and the generated tsconfig.json at the same time)
await remove(rootDir)
})

test('register ts-node', () => {
registerTsNode()
expect(register).toHaveBeenCalled()
})
})
@@ -1337,7 +1337,7 @@
resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/node@*", "@types/node@^10.11.7":
"@types/node@*", "@types/node@^10.11.7", "@types/node@^10.12.18":
version "10.12.18"
resolved "https://registry.npmjs.org/@types/node/-/node-10.12.18.tgz#1d3ca764718915584fcd9f6344621b7672665c67"
integrity sha512-fh+pAqt4xRzPfqA6eh3Z2y6fyZavRIumvjhaCL753+TVkGKGhpPeyrJG2JftD0T9q4GF00KjefsQ+PQNDdWQaQ==
@@ -1727,7 +1727,7 @@ ansi-align@^3.0.0:
dependencies:
string-width "^3.0.0"

ansi-colors@^3.0.0:
ansi-colors@^3.0.0, ansi-colors@^3.2.1:
version "3.2.3"
resolved "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.3.tgz#57d35b8686e851e2cc04c403f1c00203976a1813"
integrity sha512-LEHHyuhlPY3TmuUYMh2oz89lTShfvgbmzaBcxve9t/9Wuy7Dwf4yoAKcND7KFT1HAQfqZ12qtc+DUrBMeKF9nw==
@@ -4010,6 +4010,13 @@ enhanced-resolve@^4.0.0, enhanced-resolve@^4.1.0:
memory-fs "^0.4.0"
tapable "^1.0.0"

enquirer@^2.3.0:
version "2.3.0"
resolved "https://registry.npmjs.org/enquirer/-/enquirer-2.3.0.tgz#c362c9d84984ebe854def63caaf12983a16af552"
integrity sha512-RNGUbRVlfnjmpxV+Ed+7CGu0rg3MK7MmlW+DW0v7V2zdAUBC1s4BxCRiIAozbYB2UJ+q4D+8tW9UFb11kF72/g==
dependencies:
ansi-colors "^3.2.1"

entities@^1.1.1, entities@~1.1.1:
version "1.1.2"
resolved "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.