|
| 1 | +--- |
| 2 | +title: Configuring Vite |
| 3 | +description: NativeScript apps can be bundled with Vite. |
| 4 | +contributors: |
| 5 | + - NathanWalker |
| 6 | +--- |
| 7 | + |
| 8 | +All NativeScript apps can be bundled using [Vite](https://vite.dev/). To manage the required configuration, we maintain the `@nativescript/vite` package. |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +Install the plugin. |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install @nativescript/vite |
| 16 | +``` |
| 17 | + |
| 18 | +## Quick start (`init`) |
| 19 | + |
| 20 | +To bootstrap an existing NativeScript app for Vite, run from your app root: |
| 21 | + |
| 22 | +```bash |
| 23 | +npx nativescript-vite init |
| 24 | +``` |
| 25 | + |
| 26 | +This will: |
| 27 | + |
| 28 | +- Generate a `vite.config.ts` using the detected project flavor (Angular, Vue, React, Solid, TypeScript, or JavaScript) and the corresponding helper from `@nativescript/vite`. |
| 29 | +- Add (or update) the following npm scripts in your app `package.json`: |
| 30 | + - `dev:ios` |
| 31 | + - `dev:android` |
| 32 | + - `dev:server:ios` |
| 33 | + - `dev:server:android` |
| 34 | + - `ios` |
| 35 | + - `android` |
| 36 | +- Add the devDependencies `concurrently` and `wait-on`. |
| 37 | +- Add the dependency `@valor/nativescript-websockets`. |
| 38 | +- Append `.ns-vite-build` to `.gitignore` if it is not already present. |
| 39 | + |
| 40 | +After running `init`, you now have two ways to work with Vite: |
| 41 | + |
| 42 | +1. HMR workflow |
| 43 | + |
| 44 | +```bash |
| 45 | +npm run dev:ios |
| 46 | +``` |
| 47 | + |
| 48 | +2. Standard dev workflow (non-HMR) |
| 49 | + |
| 50 | +```bash |
| 51 | +ns debug ios --no-hmr |
| 52 | +ns debug android --no-hmr |
| 53 | +``` |
| 54 | + |
| 55 | +## Configure |
| 56 | + |
| 57 | +The plugin comes with several framework integrations. |
| 58 | + |
| 59 | +### Vue |
| 60 | + |
| 61 | +```ts |
| 62 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 63 | +import { vueConfig } from '@nativescript/vite'; |
| 64 | + |
| 65 | +export default defineConfig(({ mode }): UserConfig => { |
| 66 | + return mergeConfig(vueConfig({ mode }), {}); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +### Angular |
| 71 | + |
| 72 | +```ts |
| 73 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 74 | +import { angularConfig } from '@nativescript/vite'; |
| 75 | + |
| 76 | +export default defineConfig(({ mode }): UserConfig => { |
| 77 | + return mergeConfig(angularConfig({ mode }), {}); |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +### Solid |
| 82 | + |
| 83 | +```ts |
| 84 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 85 | +import { solidConfig } from '@nativescript/vite'; |
| 86 | + |
| 87 | +export default defineConfig(({ mode }): UserConfig => { |
| 88 | + return mergeConfig(solidConfig({ mode }), {}); |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +### Svelte |
| 93 | + |
| 94 | +```ts |
| 95 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 96 | +import { solidConfig } from '@nativescript/vite'; |
| 97 | + |
| 98 | +export default defineConfig(({ mode }): UserConfig => { |
| 99 | + return mergeConfig(solidConfig({ mode }), {}); |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +### React |
| 104 | + |
| 105 | +```ts |
| 106 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 107 | +import { reactConfig } from '@nativescript/vite'; |
| 108 | + |
| 109 | +export default defineConfig(({ mode }): UserConfig => { |
| 110 | + return mergeConfig(reactConfig({ mode }), {}); |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +### TypeScript (XML view) |
| 115 | + |
| 116 | +```ts |
| 117 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 118 | +import { typescriptConfig } from '@nativescript/vite'; |
| 119 | + |
| 120 | +export default defineConfig(({ mode }): UserConfig => { |
| 121 | + return mergeConfig(typescriptConfig({ mode }), {}); |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +The above config configures most things required to bundle a NativeScript application. |
| 126 | + |
| 127 | +This page contains examples of common things you might want to change in the [Examples of configurations section](#configuration-examples) - for anything else not mentioned here, refer to the [Vite documentation](https://vite.dev/config/). |
| 128 | + |
| 129 | +## CLI Flags |
| 130 | + |
| 131 | +When running a NativeScript app the following flags have an effect on the final vite config |
| 132 | + |
| 133 | +### --no-hmr |
| 134 | + |
| 135 | +Disable HMR (enabled by default) |
| 136 | + |
| 137 | +### --env.verbose |
| 138 | + |
| 139 | +Prints verbose logs and the internal config before building |
| 140 | + |
| 141 | +### Additional flags |
| 142 | + |
| 143 | +Additional env flags that are usually passed by the CLI automatically |
| 144 | + |
| 145 | +- `--env.appPath` - path to the app source (same as `appPath` in the `nativescript.config.ts`) |
| 146 | +- `--env.appResourcesPath` - path to App_Resources (same as `appResourcesPath` in the `nativescript.config.ts`) |
| 147 | +- `--env.nativescriptLibPath` - path to the currently running CLI's library. |
| 148 | +- `--env.android` - `true` when running on android |
| 149 | +- `--env.ios` - `true` when running on ios |
| 150 | +- `--env.platform=<platform>` - for specifying the platform to use. Can be `android` or `ios`, or a custom platform in the future. |
| 151 | +- `--env.hmr` - `true` when building with HMR enabled |
| 152 | + |
| 153 | +## Global "magic" variables |
| 154 | + |
| 155 | +We define a few useful globally available variables that you can use to alter logic in your applications. |
| 156 | + |
| 157 | +- `__DEV__` - `true` when webpack is building in development mode |
| 158 | + ```ts |
| 159 | + if (__DEV__) { |
| 160 | + // we are running a dev build |
| 161 | + } |
| 162 | + ``` |
| 163 | +- `__ANDROID__`, `true` when the platform is Android |
| 164 | + ```ts |
| 165 | + if (global.isAndroid) { |
| 166 | + // we are running on android |
| 167 | + } |
| 168 | + ``` |
| 169 | +- `__IOS__`, `true` when the platform is iOS |
| 170 | + ```ts |
| 171 | + if (__IOS__) { |
| 172 | + // we are running on iOS |
| 173 | + } |
| 174 | + ``` |
| 175 | + |
| 176 | +::: details The following variables are also defined, but are primarily intended to be used by NativeScript Core internally, or plugins that wish to use these. |
| 177 | + |
| 178 | +- `__NS_ENV_VERBOSE__` - `true` when `--env.verbose` is set |
| 179 | +- `__CSS_PARSER__` - the CSS parser used by NativeScript Core. The value is set based on the `cssParser` value in the `nativescript.config.ts` and defaults to `css-tree` |
| 180 | +- `__UI_USE_XML_PARSER__` - a flag used by NativeScript Core to disable the XML parser when it's not used |
| 181 | +- `__UI_USE_EXTERNAL_RENDERER__` - a flag used by NativeScript Core to disable registering global modules when an external renderer is used. |
| 182 | + |
| 183 | +::: |
| 184 | + |
| 185 | +## Configuration examples |
| 186 | + |
| 187 | +Here are some common examples of things you may want to do in your `vite.config.ts`. |
| 188 | + |
| 189 | +### Adding a copy rule |
| 190 | + |
| 191 | +```ts |
| 192 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 193 | +import { typescriptConfig } from '@nativescript/vite'; |
| 194 | +import path from 'path'; |
| 195 | +import { viteStaticCopy } from 'vite-plugin-static-copy'; |
| 196 | + |
| 197 | +export default defineConfig(({ mode }): UserConfig => { |
| 198 | + const base = typescriptConfig({ mode }); |
| 199 | + const projectRoot = path.resolve(__dirname); |
| 200 | + const testImagePath = path.resolve(projectRoot, 'src/ui/image/700x50.png'); |
| 201 | + return mergeConfig(base, { |
| 202 | + plugins: [ |
| 203 | + viteStaticCopy({ |
| 204 | + targets: [{ src: testImagePath, dest: 'ui/image' }], |
| 205 | + }) |
| 206 | + ], |
| 207 | + }); |
| 208 | +}); |
| 209 | + |
| 210 | +``` |
| 211 | + |
| 212 | +## Plugin API |
| 213 | + |
| 214 | +NativeScript plugins can provide a `nativescript.vite.mjs` file in their root folder (next to `package.json`), and `@nativescript/vite` will include these configs when resolving the final config. |
| 215 | + |
| 216 | +For example, a plugin could return custom processing: |
| 217 | + |
| 218 | +```js |
| 219 | +import { createRequire } from "module"; |
| 220 | +const require = createRequire(import.meta.url); |
| 221 | + |
| 222 | +let postcssConfig = "./postcss.config.js"; |
| 223 | + |
| 224 | +try { |
| 225 | + const twV4 = require("@tailwindcss/postcss"); |
| 226 | + const nsTailwind = require("@nativescript/tailwind"); |
| 227 | + postcssConfig = { plugins: [twV4, nsTailwind] }; |
| 228 | +} catch (e2) { |
| 229 | + console.warn( |
| 230 | + "Inline PostCSS unavailable, falling back to ./postcss.config.js" |
| 231 | + ); |
| 232 | +} |
| 233 | + |
| 234 | +export default () => { |
| 235 | + return { |
| 236 | + css: { |
| 237 | + postcss: postcssConfig, |
| 238 | + }, |
| 239 | + }; |
| 240 | +}; |
| 241 | +``` |
0 commit comments