|
| 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 | +## Configure |
| 19 | + |
| 20 | +The plugin comes with several framework integrations. |
| 21 | + |
| 22 | +### Vue |
| 23 | + |
| 24 | +```ts |
| 25 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 26 | +import { vueConfig } from '@nativescript/vite'; |
| 27 | + |
| 28 | +export default defineConfig(({ mode }): UserConfig => { |
| 29 | + return mergeConfig(vueConfig({ mode }), {}); |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +### Angular |
| 34 | + |
| 35 | +```ts |
| 36 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 37 | +import { angularConfig } from '@nativescript/vite'; |
| 38 | + |
| 39 | +export default defineConfig(({ mode }): UserConfig => { |
| 40 | + return mergeConfig(angularConfig({ mode }), {}); |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### Solid |
| 45 | + |
| 46 | +```ts |
| 47 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 48 | +import { solidConfig } from '@nativescript/vite'; |
| 49 | + |
| 50 | +export default defineConfig(({ mode }): UserConfig => { |
| 51 | + return mergeConfig(solidConfig({ mode }), {}); |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +### Svelte |
| 56 | + |
| 57 | +```ts |
| 58 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 59 | +import { solidConfig } from '@nativescript/vite'; |
| 60 | + |
| 61 | +export default defineConfig(({ mode }): UserConfig => { |
| 62 | + return mergeConfig(solidConfig({ mode }), {}); |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +### React |
| 67 | + |
| 68 | +```ts |
| 69 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 70 | +import { reactConfig } from '@nativescript/vite'; |
| 71 | + |
| 72 | +export default defineConfig(({ mode }): UserConfig => { |
| 73 | + return mergeConfig(reactConfig({ mode }), {}); |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +### TypeScript (XML view) |
| 78 | + |
| 79 | +```ts |
| 80 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 81 | +import { typescriptConfig } from '@nativescript/vite'; |
| 82 | + |
| 83 | +export default defineConfig(({ mode }): UserConfig => { |
| 84 | + return mergeConfig(typescriptConfig({ mode }), {}); |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +The above config configures most things required to bundle a NativeScript application. |
| 89 | + |
| 90 | +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/). |
| 91 | + |
| 92 | +## CLI Flags |
| 93 | + |
| 94 | +When running a NativeScript app the following flags have an effect on the final vite config |
| 95 | + |
| 96 | +### --no-hmr |
| 97 | + |
| 98 | +Disable HMR (enabled by default) |
| 99 | + |
| 100 | +### --env.verbose |
| 101 | + |
| 102 | +Prints verbose logs and the internal config before building |
| 103 | + |
| 104 | +### Additional flags |
| 105 | + |
| 106 | +Additional env flags that are usually passed by the CLI automatically |
| 107 | + |
| 108 | +- `--env.appPath` - path to the app source (same as `appPath` in the `nativescript.config.ts`) |
| 109 | +- `--env.appResourcesPath` - path to App_Resources (same as `appResourcesPath` in the `nativescript.config.ts`) |
| 110 | +- `--env.nativescriptLibPath` - path to the currently running CLI's library. |
| 111 | +- `--env.android` - `true` when running on android |
| 112 | +- `--env.ios` - `true` when running on ios |
| 113 | +- `--env.platform=<platform>` - for specifying the platform to use. Can be `android` or `ios`, or a custom platform in the future. |
| 114 | +- `--env.hmr` - `true` when building with HMR enabled |
| 115 | + |
| 116 | +## Global "magic" variables |
| 117 | + |
| 118 | +We define a few useful globally available variables that you can use to alter logic in your applications. |
| 119 | + |
| 120 | +- `__DEV__` - `true` when webpack is building in development mode |
| 121 | + ```ts |
| 122 | + if (__DEV__) { |
| 123 | + // we are running a dev build |
| 124 | + } |
| 125 | + ``` |
| 126 | +- `__ANDROID__`, `true` when the platform is Android |
| 127 | + ```ts |
| 128 | + if (global.isAndroid) { |
| 129 | + // we are running on android |
| 130 | + } |
| 131 | + ``` |
| 132 | +- `__IOS__`, `true` when the platform is iOS |
| 133 | + ```ts |
| 134 | + if (__IOS__) { |
| 135 | + // we are running on iOS |
| 136 | + } |
| 137 | + ``` |
| 138 | + |
| 139 | +::: 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. |
| 140 | + |
| 141 | +- `__NS_ENV_VERBOSE__` - `true` when `--env.verbose` is set |
| 142 | +- `__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` |
| 143 | +- `__UI_USE_XML_PARSER__` - a flag used by NativeScript Core to disable the XML parser when it's not used |
| 144 | +- `__UI_USE_EXTERNAL_RENDERER__` - a flag used by NativeScript Core to disable registering global modules when an external renderer is used. |
| 145 | + |
| 146 | +::: |
| 147 | + |
| 148 | +## Configuration examples |
| 149 | + |
| 150 | +Here are some common examples of things you may want to do in your `vite.config.ts`. |
| 151 | + |
| 152 | +### Adding a copy rule |
| 153 | + |
| 154 | +```ts |
| 155 | +import { defineConfig, mergeConfig, UserConfig } from 'vite'; |
| 156 | +import { typescriptConfig } from '@nativescript/vite'; |
| 157 | +import path from 'path'; |
| 158 | +import { viteStaticCopy } from 'vite-plugin-static-copy'; |
| 159 | + |
| 160 | +export default defineConfig(({ mode }): UserConfig => { |
| 161 | + const base = typescriptConfig({ mode }); |
| 162 | + const projectRoot = path.resolve(__dirname); |
| 163 | + const testImagePath = path.resolve(projectRoot, 'src/ui/image/700x50.png'); |
| 164 | + return mergeConfig(base, { |
| 165 | + plugins: [ |
| 166 | + viteStaticCopy({ |
| 167 | + targets: [{ src: testImagePath, dest: 'ui/image' }], |
| 168 | + }) |
| 169 | + ], |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +``` |
| 174 | + |
| 175 | +## Plugin API |
| 176 | + |
| 177 | +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. |
| 178 | + |
| 179 | +For example, a plugin could return custom processing: |
| 180 | + |
| 181 | +```js |
| 182 | +import { createRequire } from "module"; |
| 183 | +const require = createRequire(import.meta.url); |
| 184 | + |
| 185 | +let postcssConfig = "./postcss.config.js"; |
| 186 | + |
| 187 | +try { |
| 188 | + const twV4 = require("@tailwindcss/postcss"); |
| 189 | + const nsTailwind = require("@nativescript/tailwind"); |
| 190 | + postcssConfig = { plugins: [twV4, nsTailwind] }; |
| 191 | +} catch (e2) { |
| 192 | + console.warn( |
| 193 | + "Inline PostCSS unavailable, falling back to ./postcss.config.js" |
| 194 | + ); |
| 195 | +} |
| 196 | + |
| 197 | +export default () => { |
| 198 | + return { |
| 199 | + css: { |
| 200 | + postcss: postcssConfig, |
| 201 | + }, |
| 202 | + }; |
| 203 | +}; |
| 204 | +``` |
0 commit comments