diff --git a/examples/typescript-vuex/README.md b/examples/typescript-vuex/README.md new file mode 100644 index 0000000000..6a041d9201 --- /dev/null +++ b/examples/typescript-vuex/README.md @@ -0,0 +1,3 @@ +# TypeScript with Vuex example + +https://nuxtjs.org/examples/typescript-vuex diff --git a/examples/typescript/assets/css/main.css b/examples/typescript-vuex/assets/css/main.css similarity index 100% rename from examples/typescript/assets/css/main.css rename to examples/typescript-vuex/assets/css/main.css diff --git a/examples/typescript/components/Card.vue b/examples/typescript-vuex/components/Card.vue similarity index 74% rename from examples/typescript/components/Card.vue rename to examples/typescript-vuex/components/Card.vue index a897fd5292..eab608c226 100644 --- a/examples/typescript/components/Card.vue +++ b/examples/typescript-vuex/components/Card.vue @@ -13,18 +13,14 @@ diff --git a/examples/typescript/nuxt.config.js b/examples/typescript-vuex/nuxt.config.ts similarity index 53% rename from examples/typescript/nuxt.config.js rename to examples/typescript-vuex/nuxt.config.ts index 682c251744..985fb1eece 100644 --- a/examples/typescript/nuxt.config.js +++ b/examples/typescript-vuex/nuxt.config.ts @@ -1,23 +1,15 @@ -export default { - env: { - baseUrl: process.env.BASE_URL || 'http://localhost:3000' - }, +const config = { head: { title: 'starter', meta: [ { charset: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }, - { hid: 'description', name: 'description', content: 'Nuxt.js project' } + { hid: 'description', name: 'description', content: 'Nuxt TS project' } ], link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }] }, - /* - ** Customize the progress-bar color - */ loading: { color: '#3B8070' }, - /* - ** Build configuration - */ - css: ['tachyons/css/tachyons.min.css', '~/assets/css/main.css'], - modules: ['~/modules/typescript'] + css: ['tachyons/css/tachyons.min.css', '~/assets/css/main.css'] } + +export default config diff --git a/examples/typescript-vuex/package.json b/examples/typescript-vuex/package.json new file mode 100644 index 0000000000..3f69b008d8 --- /dev/null +++ b/examples/typescript-vuex/package.json @@ -0,0 +1,22 @@ +{ + "version": "1.0.0", + "private": true, + "dependencies": { + "axios": "^0.18.0", + "nuxt-ts-edge": "latest", + "tachyons": "^4.11.1", + "vue-property-decorator": "^7.2.0", + "vuex-class": "^0.3.1" + }, + "scripts": { + "dev": "nuxt-ts", + "build": "nuxt-ts build", + "start": "nuxt-ts start", + "generate": "nuxt-ts generate", + "lint": "tslint --project ." + }, + "devDependencies": { + "@types/node": "^10.12.18", + "tslint-config-standard": "^8.0.1" + } +} diff --git a/examples/typescript-vuex/pages/index.vue b/examples/typescript-vuex/pages/index.vue new file mode 100644 index 0000000000..7b2869716c --- /dev/null +++ b/examples/typescript-vuex/pages/index.vue @@ -0,0 +1,39 @@ + + + + + Nuxt TypeScript Starter + + + Selected Person: {{ selectedPerson.first_name }} {{ selectedPerson.last_name }} + + {{ selected }} + + + + + + + + + + diff --git a/examples/typescript/static/favicon.ico b/examples/typescript-vuex/static/favicon.ico similarity index 100% rename from examples/typescript/static/favicon.ico rename to examples/typescript-vuex/static/favicon.ico diff --git a/examples/typescript/static/random-data.json b/examples/typescript-vuex/static/random-data.json similarity index 100% rename from examples/typescript/static/random-data.json rename to examples/typescript-vuex/static/random-data.json diff --git a/examples/typescript/store/index.ts b/examples/typescript-vuex/store/index.ts similarity index 88% rename from examples/typescript/store/index.ts rename to examples/typescript-vuex/store/index.ts index 5bb3f11913..56e90e9be6 100644 --- a/examples/typescript/store/index.ts +++ b/examples/typescript-vuex/store/index.ts @@ -11,11 +11,7 @@ import * as people from './modules/people' // action: Sync or async operations that commit mutations // mutations: Modify the state -interface ModulesStates { - people: people.State -} - -export type RootState = root.State & ModulesStates +export type RootState = root.State const createStore = () => { return new Vuex.Store({ diff --git a/examples/typescript/store/modules/people.ts b/examples/typescript-vuex/store/modules/people.ts similarity index 87% rename from examples/typescript/store/modules/people.ts rename to examples/typescript-vuex/store/modules/people.ts index 25f3fb6f4a..f6deceb8a0 100644 --- a/examples/typescript/store/modules/people.ts +++ b/examples/typescript-vuex/store/modules/people.ts @@ -52,20 +52,20 @@ export const getters: GetterTree = { } export interface Actions extends ActionTree { - select(context: ActionContext, id: number): void + select (context: ActionContext, id: number): void } export const actions: Actions = { - select({ commit }, id: number) { + select ({ commit }, id: number) { commit(types.SELECT, id) } } export const mutations: MutationTree = { - [types.SELECT](state, id: number) { + [types.SELECT] (state, id: number) { state.selected = id }, - [types.SET](state, people: Person[]) { + [types.SET] (state, people: Person[]) { state.people = people } } diff --git a/examples/typescript/store/root.ts b/examples/typescript-vuex/store/root.ts similarity index 73% rename from examples/typescript/store/root.ts rename to examples/typescript-vuex/store/root.ts index 41f5373e8c..145020363a 100644 --- a/examples/typescript/store/root.ts +++ b/examples/typescript-vuex/store/root.ts @@ -1,5 +1,5 @@ import { GetterTree, ActionContext, ActionTree, MutationTree } from 'vuex' -import axios from '~/plugins/axios' +import axios from 'axios' import { RootState } from 'store' import * as people from './modules/people' @@ -12,12 +12,12 @@ export const state = (): State => ({}) export const getters: GetterTree = {} export interface Actions extends ActionTree { - nuxtServerInit(context: ActionContext): void + nuxtServerInit (context: ActionContext): void } export const actions: Actions = { - async nuxtServerInit({ commit }) { - const response = await axios.get('/random-data.json') + async nuxtServerInit ({ commit }) { + const response = await axios.get('/random-data.json', { proxy: { host: '127.0.0.1', port: 3000 } }) const staticPeople = response.data.slice(0, 10) commit(`${people.name}/${people.types.SET}`, staticPeople, { root: true }) } diff --git a/examples/typescript-vuex/tsconfig.json b/examples/typescript-vuex/tsconfig.json new file mode 100644 index 0000000000..6ec8fa2719 --- /dev/null +++ b/examples/typescript-vuex/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "esnext", + "moduleResolution": "node", + "lib": ["es2015", "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" + ] + } +} diff --git a/examples/typescript-vuex/tslint.json b/examples/typescript-vuex/tslint.json new file mode 100644 index 0000000000..085d45bd4b --- /dev/null +++ b/examples/typescript-vuex/tslint.json @@ -0,0 +1,9 @@ +{ + "defaultSeverity": "error", + "extends": [ + "tslint-config-standard" + ], + "rules": { + "prefer-const": true + } +} diff --git a/examples/typescript/.eslintrc.js b/examples/typescript/.eslintrc.js deleted file mode 100644 index 0e406c870b..0000000000 --- a/examples/typescript/.eslintrc.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - parserOptions: { - parser: 'babel-eslint', - sourceType: 'module', - ecmaFeatures: { - legacyDecorators: true - } - } -} diff --git a/examples/typescript/README.md b/examples/typescript/README.md index a72ae8fe82..dd064e4dfd 100644 --- a/examples/typescript/README.md +++ b/examples/typescript/README.md @@ -1,3 +1,3 @@ -# Using typescript within nuxt.js +# TypeScript example -https://github.com/johnlindquist/nuxt-typescript-starter/ +https://nuxtjs.org/examples/typescript diff --git a/examples/typescript/components/HelloWorld.vue b/examples/typescript/components/HelloWorld.vue new file mode 100644 index 0000000000..92c06e3df1 --- /dev/null +++ b/examples/typescript/components/HelloWorld.vue @@ -0,0 +1,14 @@ + + + {{ message }} + + + + diff --git a/examples/typescript/layouts/default.vue b/examples/typescript/layouts/default.vue deleted file mode 100644 index e2f7d77975..0000000000 --- a/examples/typescript/layouts/default.vue +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/examples/typescript/modules/typescript.js b/examples/typescript/modules/typescript.js deleted file mode 100644 index 0c7f88df45..0000000000 --- a/examples/typescript/modules/typescript.js +++ /dev/null @@ -1,30 +0,0 @@ -export default function () { - // Add .ts extension for store, middleware and more - this.nuxt.options.extensions.push('ts') - // Extend build - this.extendBuild((config) => { - const tsLoader = { - loader: 'ts-loader', - options: { - appendTsSuffixTo: [/\.vue$/] - }, - exclude: [ - /dist/, - /\.temp/ - ] - } - // Add TypeScript loader - config.module.rules.push( - Object.assign( - { - test: /((client|server)\.js)|(\.tsx?)$/ - }, - tsLoader - ) - ) - // Add .ts extension in webpack resolve - if (config.resolve.extensions.indexOf('.ts') === -1) { - config.resolve.extensions.push('.ts') - } - }) -} diff --git a/examples/typescript/nuxt.config.ts b/examples/typescript/nuxt.config.ts new file mode 100644 index 0000000000..f33d7c4e6e --- /dev/null +++ b/examples/typescript/nuxt.config.ts @@ -0,0 +1,3 @@ +export default { + plugins: ['~/plugins/hello'] +} diff --git a/examples/typescript/package.json b/examples/typescript/package.json index b0f797df2d..d1b9460392 100644 --- a/examples/typescript/package.json +++ b/examples/typescript/package.json @@ -1,22 +1,19 @@ { - "version": "1.0.1", + "version": "1.0.0", "private": true, "dependencies": { - "axios": "^0.18.0", - "nuxt-class-component": "^1.1.3", - "nuxt-edge": "latest", - "tachyons": "^4.9.1", - "vue-property-decorator": "^7.2.0", - "vuex-class": "^0.3.1" + "nuxt-ts-edge": "latest", + "vue-property-decorator": "^7.2.0" }, "scripts": { - "dev": "nuxt", - "build": "nuxt build", - "start": "nuxt start", - "generate": "nuxt generate" + "dev": "nuxt-ts", + "build": "nuxt-ts build", + "start": "nuxt-ts start", + "generate": "nuxt-ts generate", + "lint": "tslint --project ." }, "devDependencies": { - "ts-loader": "^5.3.1", - "typescript": "^3.1.6" + "@types/node": "^10.12.18", + "tslint-config-standard": "^8.0.1" } } diff --git a/examples/typescript/pages/index.vue b/examples/typescript/pages/index.vue index 5915e56994..1bfe2bdf0b 100644 --- a/examples/typescript/pages/index.vue +++ b/examples/typescript/pages/index.vue @@ -1,41 +1,15 @@ - - - - Nuxt TypeScript Starter - - - Selected Person: {{ selectedPerson.first_name }} {{ selectedPerson.last_name }} - - {{ selected }} - - - - - - - + diff --git a/examples/typescript/plugins/axios.js b/examples/typescript/plugins/axios.js deleted file mode 100644 index 444dd65f50..0000000000 --- a/examples/typescript/plugins/axios.js +++ /dev/null @@ -1,5 +0,0 @@ -import axios from 'axios' - -export default axios.create({ - baseURL: process.env.baseUrl -}) diff --git a/examples/typescript/plugins/hello.ts b/examples/typescript/plugins/hello.ts new file mode 100644 index 0000000000..e37dd192fe --- /dev/null +++ b/examples/typescript/plugins/hello.ts @@ -0,0 +1,3 @@ +export default () => { + console.log(`Hello from ${process.server ? 'Server' : 'Client'} !`) +} diff --git a/examples/typescript/tsconfig.json b/examples/typescript/tsconfig.json index 3bdc658988..6ec8fa2719 100644 --- a/examples/typescript/tsconfig.json +++ b/examples/typescript/tsconfig.json @@ -1,23 +1,27 @@ { "compilerOptions": { "target": "es5", - "lib": [ - "dom", - "es2015" - ], - "module": "es2015", + "module": "esnext", "moduleResolution": "node", + "lib": ["es2015", "dom"], + "esModuleInterop": true, "experimentalDecorators": true, - "noImplicitAny": false, - "noImplicitThis": false, - "strictNullChecks": true, - "removeComments": true, - "suppressImplicitAnyIndexErrors": true, - "allowSyntheticDefaultImports": true, - "baseUrl": ".", "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" + ] } } diff --git a/examples/typescript/tslint.json b/examples/typescript/tslint.json new file mode 100644 index 0000000000..085d45bd4b --- /dev/null +++ b/examples/typescript/tslint.json @@ -0,0 +1,9 @@ +{ + "defaultSeverity": "error", + "extends": [ + "tslint-config-standard" + ], + "rules": { + "prefer-const": true + } +} diff --git a/packages/vue-app/types/index.d.ts b/packages/vue-app/types/index.d.ts index c9582f3a82..da40415848 100644 --- a/packages/vue-app/types/index.d.ts +++ b/packages/vue-app/types/index.d.ts @@ -2,6 +2,9 @@ import Vue from "vue"; import VueRouter, { Route } from "vue-router"; import { Store } from "vuex"; +// augment typings of NodeJS.Process +import "./process"; + // augment typings of Vue.js import "./vue"; diff --git a/packages/vue-app/types/process.d.ts b/packages/vue-app/types/process.d.ts new file mode 100644 index 0000000000..8acfcef602 --- /dev/null +++ b/packages/vue-app/types/process.d.ts @@ -0,0 +1,12 @@ +/** + * Extends NodeJS.Process interface + */ + +declare namespace NodeJS { + interface Process { + browser: boolean; + client: boolean; + server: boolean; + static: boolean; + } +}