- Use WebStorm 2020.2.1+.
- Install dependencies:
npm i. - Start developing:
npm run dev.
- Turn on "Run on save for files:" under Settings > Languages & Frameworks > JavaScript > Prettier. Make sure that
.tsand.vueare included. - Turn on "Automatic ESLint configuration" under Settings > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint.
- Turn on "Enable" and "Process JS files" under Settings > Languages & Frameworks > Style Sheets > Stylelint.
- EditorConfig is enabled by default.
-
SFC should be annotated with
<script lang="ts">, default export should be wrapped indefineComponent.<script lang="ts"> import { defineComponent } from '@nuxtjs/composition-api'; export default defineComponent({ // ... }); </script> -
Non-primitive props should be annotated with
PropType<T>.props: { // Primitive count: { type: Number }, value: { type: [Number, String] }, wtf: { type: null }, // Will become "any" type // Non-primitive item: { type: Object as PropType<Item> }, ids: { type: Array as PropType<number[]> }, content: { type: [Array, Object] as PropType<Item | Item[]> }, formatter: { type: Function as PropType<(item: Item) => string> }, }, -
Methods and computed properties should have explicit type annotations.
-
Nuxt
asyncDatarequires special treatment, seewithCustomFieldsfromlib/utils.ts
- Starter project comes with Bootstrap 4. You can remove it if you don't need it, or use as an example of how to add another framework.
- Project-wide styles go to
~/assets/main.scss. - Inside SCSS files and style sections, refer to other files as:
~@/assets/...for project files.~bootstrap/...for files fromnode_modules.
- Inside SFC, prefer
@usewith namespaces instead of@import.<style lang="scss" scoped> @use "~@/assets/vars" as vars; @use "~@/assets/plugins/bootstrap/vars" as bvars; .text { color: vars.$primary; background-color: bvars.$gray-100; } </style>
- API typings are generated based on Swagger/OpenAPI provided by backend. See
generate:apiscript insidepackage.json. - Use
$taxiosinstead of$axios. WebStorm provides auto-completion and type checking for url, body, params and query. See taxios docs for details. - If you encounter an issue with generated types, you should:
- Report about Swagger/OpenAPI issue.
- Temporary use
$taxios.unsafe.<method>instead of$taxios.<method>until Swagger/OpenAPI is fixed.
- Nuxt automatically supports
.envfile.- In development, you can copy
.env.exampleto.envand modify it. - In continuous integration,
.envcan be created programmatically.
- In development, you can copy
- To access environmental variables, you have to whitelist them in
nuxt.config.ts. There are two options for this:(public|private)RuntimeConfig(docs) maps tothis.$configandctx.$config. It only supports start-time variables.// nuxt.config.ts publicRuntimeConfig: { SERVICE_URL: process.env.SERVICE_URL || 'http://localhost:4000', },// some-component.vue asyncData(ctx) { console.log(ctx.$config); }, mounted() { console.log(this.$config); },env(docs) maps toprocess.env.<NAME>and can be used to eliminate code. It only supports build-time variables.// nuxt.config.ts env: { NODE_ENV: process.env.NODE_ENV, },// some-component.vue mounted() { if (process.env.NODE_ENV === 'development') { console.log('You are in development mode'); } },
-
Hard error renders the stack trace page in development and
views/error.htmlpage in production. It is triggered by uncaught exception during SSR.asyncData() { throw new Error('Hard error'); }, -
Soft error renders
layouts/error.vuepage. It is triggered bythis.$nuxt.error,ctx.errorand by uncaught exception after SSR.asyncData(ctx) { // Option 1: ctx.error({ message: 'Soft error', statusCode: 500 }); // Code can be different // Option 2: ctx.error({ message: 'Soft error' }); // Default status code is 500 // Option 3: ctx.error(new Error('Soft error')); // Same as above }, -
@TODO: Notification error.
Features:
- TypeScript support everywhere, including Nuxt config and server middleware.
Source code lives inside
./src. - Production does not depend on TypeScript and other build-time dependencies.
TypeScript files that are not bundled by Nuxt are compiled into
./build. During start,srcDiris chosen based on location of Nuxt config.
With Docker:
- Produce
.envfile (optional). - Build with
docker build(docs). - Deliver docker image to production.
- Start with
docker-compose(docs) ordocker run(docs).
Without Docker:
- Produce
.envfile (optional). - Build with
npm ci && npm run build. - Deliver files to production:
build/**.nuxt/**.envpackage.jsonpackage-lock.json
- Start with
npm ci --production && npm run start.
- On Windows 10:
- Install Docker for Windows: https://docs.docker.com/docker-for-windows/install/.
- Turn on "Expose daemon on tcp://localhost:2375 without TLS" in Docker Desktop under Settings > General.
- Add
.exeextension to the end of "Docker executable" and "Docker Compose executable" in WebStorm under Settings > Build, Execution, Deployment > Docker > Tools. - Start services from
docker-compose.dev.yml.
- On GNU/Linux:
- @TODO.
- Named exports from SFC are not supported, so interface types should be defined in separate
.tsfiles.- See vuejs/vue-loader#1281
- Waiting for something like https://github.com/ktsn/vuetype
- Type checking and auto-completion is not supported inside template section, neither in IDE nor during compilation.
- Waiting for https://github.com/znck/vue-developer-experience
- Vue 2 composition API has some limitations: https://github.com/vuejs/composition-api#limitations.
- Vuelidate does not have Composition API + TypeScript support yet, see vuelidate/vuelidate#684.