red
+ + + +``` + +### Usage with Composition API + +The injected classes can be accessed in `setup()` and ` + + +``` + +The syntax works with [` + + +hello
+ + + +``` + +The actual value will be compiled into a hashed CSS custom property, so the CSS is still static. The custom property will be applied to the component's root element via inline styles and reactively updated if the source value changes. diff --git a/src/api/sfc-tooling.md b/src/api/sfc-tooling.md new file mode 100644 index 0000000000..f38bc59292 --- /dev/null +++ b/src/api/sfc-tooling.md @@ -0,0 +1,94 @@ +# SFC Tooling + +## Online Playgrounds + +You don't need to install anything on your machine to try out Vue SFCs - there are many online playgrounds that allow you to do so right in the browser: + +- [Vue SFC Playground](https://sfc.vuejs.org) (official, deployed from latest commit) +- [VueUse Playground](https://play.vueuse.org) +- [Vue on CodeSandbox](https://codesandbox.io/s/vue-3) +- [Vue on Repl.it](https://replit.com/@templates/VueJS-with-Vite) +- [Vue on Codepen](https://codepen.io/pen/editor/vue) +- [Vue on StackBlitz](https://stackblitz.com/fork/vue) + +It is also recommended to use these online playgrounds to provide reproductions when reporting bugs. + +## Project Scaffolding + +### Vite + +[Vite](https://vitejs.dev/) is a lightweight and fast build tool with first-class Vue SFC support. It is created by Evan You, who is also the author of Vue itself! To get started with Vite + Vue, simply run: + +```sh +npm init vite@latest +``` + +Then select the Vue template and follow the instructions. + +- To learn more about Vite, check out the [Vite docs](https://vitejs.dev/guide/). +- To configure Vue-specific behavior in a Vite project, for example passing options to the Vue compiler, check out the docs for [@vitejs/plugin-vue](https://github.com/vitejs/vite/tree/main/packages/plugin-vue#readme). + +The [SFC Playground](https://sfc.vuejs.org/) also supports downloading the files as a Vite project. + +### Vue CLI + +[Vue CLI](https://cli.vuejs.org/) is the official webpack-based build tool for Vue projects. To get started with Vue CLI: + +```sh +npm install -g @vue/cli +vue create hello-vue +``` + +- To learn more about Vue CLI, check out [Vue CLI docs](https://cli.vuejs.org/guide/installation.html). + +### Vite or Vue CLI? + +We recommend starting new projects with Vite as it offers significantly better development experience in terms of dev server startup and HMR update performance ([details](https://vitejs.dev/guide/why.html)). Only go with Vue CLI if you rely on specific webpack features (e.g. Module Federation). + +If you are a [Rollup](https://rollupjs.org/) user, you can safely adopt Vite as it uses Rollup for production builds and supports a Rollup-compatible plugin system. [Even Rollup's maintainer recommends Vite as THE web development wrapper for Rollup](https://twitter.com/lukastaegert/status/1412119729431584774). + +## IDE Support + +The recommended IDE setup is [VSCode](https://code.visualstudio.com/) + the [Volar](https://github.com/johnsoncodehk/volar) extension. Volar provides syntax highlighting and advanced IntelliSense for template expressions, component props and even slots validation. We strongly recommend this setup if you want to get the best possible experience with Vue SFCs, especially if you are also using TypeScript. + +[WebStorm](https://www.jetbrains.com/webstorm/) also provides decent support for Vue SFCs. However, do note as of now its support for ` -```html - -{{ greeting }}
- - -``` - -## Getting Started - -### Example Sandbox - -If you want to dive right in and start playing with single-file components, check out [this simple todo app](https://codesandbox.io/s/vue-todo-list-app-with-single-file-component-vzkl3?file=/src/App.vue) on CodeSandbox. - -### For Users New to Module Build Systems in JavaScript - -With `.vue` components, we're entering the realm of advanced JavaScript applications. That means learning to use a few additional tools if you haven't already: -- **Node Package Manager (npm)**: Read the [Getting Started guide](https://docs.npmjs.com/packages-and-modules/getting-packages-from-the-registry) section about how to get packages from the registry. - -- **Modern JavaScript with ES2015/16**: Read through Babel's [Learn ES2015 guide](https://babeljs.io/docs/en/learn). You don't have to memorize every feature right now, but keep this page as a reference you can come back to. - -After you've taken a day to dive into these resources, we recommend checking out [Vue CLI](https://cli.vuejs.org/). Follow the instructions and you should have a Vue project with `.vue` components, ES2015, webpack and hot-reloading in no time! - -### For Advanced Users - -The CLI takes care of most of the tooling configurations for you, but also allows fine-grained customization through its own [config options](https://cli.vuejs.org/config/). - -In case you prefer setting up your own build setup from scratch, you will need to manually configure webpack with [vue-loader](https://vue-loader.vuejs.org). To learn more about webpack itself, check out [their official docs](https://webpack.js.org/configuration/) and [webpack learning academy](https://webpack.academy/p/the-core-concepts). - -### Building with rollup - -Most of the time when developing a third-party library we want to build it in a way that allows the consumers of the library to [tree shake](https://webpack.js.org/guides/tree-shaking/) it. To enable tree-shaking we need to build `esm` modules. Since webpack and, in turn, vue-cli do not support building `esm` modules we need to rely on [rollup](https://rollupjs.org/). - -#### Installing Rollup - -We will need to install Rollup and a few dependencies: - -```bash -npm install --save-dev rollup @rollup/plugin-commonjs rollup-plugin-vue + ``` -These are the minimal amount of rollup plugins that we need to use to compile the code in an `esm` module. We may want to also add [rollup-plugin-babel](https://github.com/rollup/plugins/tree/master/packages/babel) to transpile their code and [node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve) if we use dependencies that we want to bundle with our library. +As we can see, Vue SFC is a natural extension of the classic trio of HTML, CSS and JavaScript. Each `*.vue` file consists of three types of top-level language blocks: ``, `