@sdeverywhere/plugin-vite 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -1
- package/dist/index.d.ts +40 -38
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -40
- package/dist/index.js.map +1 -1
- package/package.json +5 -8
- package/dist/index.cjs +0 -72
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -40
package/README.md
CHANGED
|
@@ -22,7 +22,152 @@ yarn add -D @sdeverywhere/plugin-vite
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
_Note:_ If you followed the "Quick Start" instructions above and/or are using one of the standard project templates provided by SDEverywhere, the `sde.config.js` file should already be set up to use `plugin-vite`.
|
|
26
|
+
Reading these instructions can still be helpful if you are setting up a project manually or want to understand how `plugin-vite` can be integrated into your project.
|
|
27
|
+
|
|
28
|
+
### Why use this plugin?
|
|
29
|
+
|
|
30
|
+
Most SDEverywhere projects include an application (or a library) that is built around the generated model.
|
|
31
|
+
Rather than running `sde bundle` and `vite build` as two separate steps, this plugin folds the Vite build into the `sde` build process, which means:
|
|
32
|
+
|
|
33
|
+
- your app is rebuilt automatically whenever the model is regenerated; and
|
|
34
|
+
- in development mode (`sde dev`), a single command starts a Vite dev server that reloads the app when either the model or your app sources change.
|
|
35
|
+
|
|
36
|
+
The plugin does not replace your Vite configuration; it simply runs Vite using the config you provide.
|
|
37
|
+
|
|
38
|
+
### Steps
|
|
39
|
+
|
|
40
|
+
1. Add `@sdeverywhere/plugin-vite` as a project "dev" dependency:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
cd your-model-project
|
|
44
|
+
npm install --save-dev @sdeverywhere/plugin-vite
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
2. Update your `sde.config.js` file to use `vitePlugin`. Both the `name` (used in log messages) and `config` (the Vite config) options are required:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import { dirname, join as joinPath } from 'path'
|
|
51
|
+
import { fileURLToPath } from 'url'
|
|
52
|
+
|
|
53
|
+
import { vitePlugin } from '@sdeverywhere/plugin-vite'
|
|
54
|
+
|
|
55
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
56
|
+
const appPath = (...parts) => joinPath(__dirname, 'packages', 'app', ...parts)
|
|
57
|
+
|
|
58
|
+
export async function config() {
|
|
59
|
+
return {
|
|
60
|
+
modelFiles: ['model/example.mdl'],
|
|
61
|
+
|
|
62
|
+
// ...
|
|
63
|
+
|
|
64
|
+
plugins: [
|
|
65
|
+
// ...
|
|
66
|
+
|
|
67
|
+
// Build or serve the model explorer app
|
|
68
|
+
vitePlugin({
|
|
69
|
+
name: 'app',
|
|
70
|
+
apply: {
|
|
71
|
+
// Run the Vite dev server when `sde dev` is used
|
|
72
|
+
development: 'serve'
|
|
73
|
+
},
|
|
74
|
+
config: {
|
|
75
|
+
configFile: appPath('vite.config.js')
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
3. Run `sde bundle` to build your app, or `sde dev` to serve it locally with live reload.
|
|
84
|
+
|
|
85
|
+
### Choosing the `apply` behavior
|
|
86
|
+
|
|
87
|
+
The `apply` option controls when (and how) Vite runs for each `sde` build mode.
|
|
88
|
+
Both `apply.development` and `apply.production` default to `'post-build'`.
|
|
89
|
+
|
|
90
|
+
For a **web application** that you want to view while you work on the model, use `'serve'` in development mode.
|
|
91
|
+
Vite starts a dev server and refreshes the browser when changes are detected:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
vitePlugin({
|
|
95
|
+
name: 'app',
|
|
96
|
+
apply: {
|
|
97
|
+
development: 'serve'
|
|
98
|
+
},
|
|
99
|
+
config: {
|
|
100
|
+
configFile: appPath('vite.config.js')
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For a **library** that other packages depend on (for example, a `core` package that wraps the generated model), use `'watch'` in development mode so that Vite rebuilds the library whenever its sources change:
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
vitePlugin({
|
|
109
|
+
name: 'core',
|
|
110
|
+
apply: {
|
|
111
|
+
development: 'watch'
|
|
112
|
+
},
|
|
113
|
+
config: {
|
|
114
|
+
configFile: corePath('vite.config.js')
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The full set of values is:
|
|
120
|
+
|
|
121
|
+
| Value | Development | Production | Behavior |
|
|
122
|
+
| ----------------- | :---------: | :--------: | -------------------------------------------------------- |
|
|
123
|
+
| `'skip'` | ✓ | ✓ | Don't run the plugin. |
|
|
124
|
+
| `'post-generate'` | ✓ | ✓ | Run `vite build` in the `postGenerate` phase. |
|
|
125
|
+
| `'post-build'` | ✓ | ✓ | Run `vite build` in the `postBuild` phase (the default). |
|
|
126
|
+
| `'watch'` | ✓ | | Run `vite build` in watch mode; useful for libraries. |
|
|
127
|
+
| `'serve'` | ✓ | | Run the Vite dev server; useful for applications. |
|
|
128
|
+
|
|
129
|
+
Use `'post-generate'` when a later plugin needs the output of this Vite build; use `'post-build'` (the default) otherwise.
|
|
130
|
+
|
|
131
|
+
### Using more than one instance
|
|
132
|
+
|
|
133
|
+
A project can include as many `vitePlugin` instances as it needs; give each one a distinct `name` so that log messages are easy to follow.
|
|
134
|
+
Because plugins run in order, list a library before the app that depends on it:
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
plugins: [
|
|
138
|
+
// Build the `core` library that wraps the generated model
|
|
139
|
+
vitePlugin({
|
|
140
|
+
name: 'core',
|
|
141
|
+
apply: { development: 'watch' },
|
|
142
|
+
config: { configFile: corePath('vite.config.js') }
|
|
143
|
+
}),
|
|
144
|
+
|
|
145
|
+
// Build or serve the app that depends on the `core` library
|
|
146
|
+
vitePlugin({
|
|
147
|
+
name: 'app',
|
|
148
|
+
apply: { development: 'serve' },
|
|
149
|
+
config: { configFile: appPath('vite.config.js') }
|
|
150
|
+
})
|
|
151
|
+
]
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Providing the Vite config inline
|
|
155
|
+
|
|
156
|
+
The `config` option is a Vite [`InlineConfig`](https://vite.dev/config/), so you can define the configuration directly in `sde.config.js` instead of pointing at a separate config file:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
vitePlugin({
|
|
160
|
+
name: 'app',
|
|
161
|
+
config: {
|
|
162
|
+
configFile: false,
|
|
163
|
+
root: appPath(),
|
|
164
|
+
build: {
|
|
165
|
+
outDir: appPath('dist'),
|
|
166
|
+
emptyOutDir: true
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
```
|
|
26
171
|
|
|
27
172
|
## Documentation
|
|
28
173
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,42 @@
|
|
|
1
|
-
import { InlineConfig } from
|
|
2
|
-
import { Plugin } from
|
|
3
|
-
|
|
1
|
+
import { InlineConfig } from "vite";
|
|
2
|
+
import { Plugin } from "@sdeverywhere/build";
|
|
3
|
+
//#region src/options.d.ts
|
|
4
4
|
interface VitePluginOptions {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
5
|
+
/** The name to include in log messages. */
|
|
6
|
+
name: string;
|
|
7
|
+
/** The Vite config to use. */
|
|
8
|
+
config: InlineConfig;
|
|
9
|
+
/** Specifies the behavior of the plugin for different `sde` build modes. */
|
|
10
|
+
apply?: {
|
|
11
|
+
/**
|
|
12
|
+
* The behavior of the plugin when sde is configured for development mode.
|
|
13
|
+
*
|
|
14
|
+
* If left undefined, defaults to 'post-build'.
|
|
15
|
+
*
|
|
16
|
+
* - `skip`: Don't run the plugin.
|
|
17
|
+
* - `post-generate`: Run `vite build` in the `postGenerate` phase.
|
|
18
|
+
* - `post-build`: Run `vite build` in the `postBuild` phase.
|
|
19
|
+
* - `watch`: Run `vite build` in the `watch` callback (rebuilds the library when
|
|
20
|
+
* changes are detected in source files); useful for libraries.
|
|
21
|
+
* - `serve`: Run `vite dev` (sets up local server and refreshes the app
|
|
22
|
+
* automatically when changes are detected); useful for applications.
|
|
23
|
+
*/
|
|
24
|
+
development?: 'skip' | 'post-generate' | 'post-build' | 'watch' | 'serve';
|
|
25
|
+
/**
|
|
26
|
+
* The behavior of the plugin when sde is configured for production mode.
|
|
27
|
+
*
|
|
28
|
+
* If left undefined, defaults to 'post-build'.
|
|
29
|
+
*
|
|
30
|
+
* - `skip`: Don't run the plugin.
|
|
31
|
+
* - `post-generate`: Run `vite build` in the `postGenerate` phase.
|
|
32
|
+
* - `post-build`: Run `vite build` in the `postBuild` phase.
|
|
33
|
+
*/
|
|
34
|
+
production?: 'skip' | 'post-generate' | 'post-build';
|
|
35
|
+
};
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/plugin.d.ts
|
|
39
|
+
export declare function vitePlugin(options: VitePluginOptions): Plugin;
|
|
40
|
+
//#endregion
|
|
41
|
+
export type { VitePluginOptions };
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/options.d.ts","../src/plugin.d.ts"],"mappings":";;;UACiB;;EAEb;;EAEA,QAAQ;;EAER;;;;;;;;;;;;;;IAcI;;;;;;;;;;IAUA;;;;;wBC7BgB,WAAW,SAAS,oBAAoB"}
|
package/dist/index.js
CHANGED
|
@@ -1,46 +1,39 @@
|
|
|
1
|
-
// src/plugin.ts
|
|
2
1
|
import { build, createServer } from "vite";
|
|
2
|
+
//#region src/plugin.ts
|
|
3
3
|
function vitePlugin(options) {
|
|
4
|
-
|
|
4
|
+
return new VitePlugin(options);
|
|
5
5
|
}
|
|
6
6
|
var VitePlugin = class {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
watch: {}
|
|
36
|
-
},
|
|
37
|
-
...this.options.config
|
|
38
|
-
};
|
|
39
|
-
await build(config);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
export {
|
|
44
|
-
vitePlugin
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
async postGenerate(context) {
|
|
11
|
+
return this.buildIfNeeded(context, "post-generate");
|
|
12
|
+
}
|
|
13
|
+
async postBuild(context) {
|
|
14
|
+
return this.buildIfNeeded(context, "post-build");
|
|
15
|
+
}
|
|
16
|
+
async buildIfNeeded(context, caller) {
|
|
17
|
+
const applyDev = this.options.apply?.development || "post-build";
|
|
18
|
+
const applyProd = this.options.apply?.production || "post-build";
|
|
19
|
+
if (context.config.mode === "development" && applyDev === caller || context.config.mode === "production" && applyProd === caller) {
|
|
20
|
+
context.log("info", `Building ${this.options.name}`);
|
|
21
|
+
await build(this.options.config);
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
async watch() {
|
|
26
|
+
if (this.options.apply?.development === "serve") await (await createServer(this.options.config)).listen();
|
|
27
|
+
else if (this.options.apply?.development === "watch") {
|
|
28
|
+
const config = {
|
|
29
|
+
build: { watch: {} },
|
|
30
|
+
...this.options.config
|
|
31
|
+
};
|
|
32
|
+
await build(config);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
45
35
|
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { vitePlugin };
|
|
38
|
+
|
|
46
39
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugin.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport type { InlineConfig, ViteDevServer } from 'vite'\nimport { build, createServer } from 'vite'\n\nimport type { BuildContext, Plugin } from '@sdeverywhere/build'\n\nimport type { VitePluginOptions } from './options'\n\nexport function vitePlugin(options: VitePluginOptions): Plugin {\n return new VitePlugin(options)\n}\n\nclass VitePlugin implements Plugin {\n constructor(private readonly options: VitePluginOptions) {}\n\n async postGenerate(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-generate'\n return this.buildIfNeeded(context, 'post-generate')\n }\n\n async postBuild(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-build'\n return this.buildIfNeeded(context, 'post-build')\n }\n\n private async buildIfNeeded(context: BuildContext, caller: 'post-generate' | 'post-build'): Promise<boolean> {\n // The apply values default to 'post-build' when left undefined\n const applyDev = this.options.apply?.development || 'post-build'\n const applyProd = this.options.apply?.production || 'post-build'\n\n // Run \"vite build\" only if configured for this mode\n const shouldBuild =\n (context.config.mode === 'development' && applyDev === caller) ||\n (context.config.mode === 'production' && applyProd === caller)\n if (shouldBuild) {\n context.log('info', `Building ${this.options.name}`)\n await build(this.options.config)\n // context.log('info', 'Done!')\n }\n return true\n }\n\n async watch(): Promise<void> {\n if (this.options.apply?.development === 'serve') {\n // Run \"vite dev\", which starts the app in a local server and\n // refreshes when source files are changed\n const server: ViteDevServer = await createServer(this.options.config)\n await server.listen()\n } else if (this.options.apply?.development === 'watch') {\n // Run \"vite build\" in watch mode so that it rebuilds when source\n // files are changed\n const config: InlineConfig = {\n build: {\n // Enable watch mode\n // TODO: Only do this if not already set up in the given config?\n watch: {}\n },\n ...this.options.config\n }\n await build(config)\n }\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport type { InlineConfig, ViteDevServer } from 'vite'\nimport { build, createServer } from 'vite'\n\nimport type { BuildContext, Plugin } from '@sdeverywhere/build'\n\nimport type { VitePluginOptions } from './options'\n\nexport function vitePlugin(options: VitePluginOptions): Plugin {\n return new VitePlugin(options)\n}\n\nclass VitePlugin implements Plugin {\n constructor(private readonly options: VitePluginOptions) {}\n\n async postGenerate(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-generate'\n return this.buildIfNeeded(context, 'post-generate')\n }\n\n async postBuild(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-build'\n return this.buildIfNeeded(context, 'post-build')\n }\n\n private async buildIfNeeded(context: BuildContext, caller: 'post-generate' | 'post-build'): Promise<boolean> {\n // The apply values default to 'post-build' when left undefined\n const applyDev = this.options.apply?.development || 'post-build'\n const applyProd = this.options.apply?.production || 'post-build'\n\n // Run \"vite build\" only if configured for this mode\n const shouldBuild =\n (context.config.mode === 'development' && applyDev === caller) ||\n (context.config.mode === 'production' && applyProd === caller)\n if (shouldBuild) {\n context.log('info', `Building ${this.options.name}`)\n await build(this.options.config)\n // context.log('info', 'Done!')\n }\n return true\n }\n\n async watch(): Promise<void> {\n if (this.options.apply?.development === 'serve') {\n // Run \"vite dev\", which starts the app in a local server and\n // refreshes when source files are changed\n const server: ViteDevServer = await createServer(this.options.config)\n await server.listen()\n } else if (this.options.apply?.development === 'watch') {\n // Run \"vite build\" in watch mode so that it rebuilds when source\n // files are changed\n const config: InlineConfig = {\n build: {\n // Enable watch mode\n // TODO: Only do this if not already set up in the given config?\n watch: {}\n },\n ...this.options.config\n }\n await build(config)\n }\n }\n}\n"],"mappings":";;AASA,SAAgB,WAAW,SAAoC;CAC7D,OAAO,IAAI,WAAW,OAAO;AAC/B;AAEA,IAAM,aAAN,MAAmC;CACjC,YAAY,SAA6C;EAA5B,KAAA,UAAA;CAA6B;CAE1D,MAAM,aAAa,SAAyC;EAE1D,OAAO,KAAK,cAAc,SAAS,eAAe;CACpD;CAEA,MAAM,UAAU,SAAyC;EAEvD,OAAO,KAAK,cAAc,SAAS,YAAY;CACjD;CAEA,MAAc,cAAc,SAAuB,QAA0D;EAE3G,MAAM,WAAW,KAAK,QAAQ,OAAO,eAAe;EACpD,MAAM,YAAY,KAAK,QAAQ,OAAO,cAAc;EAMpD,IAFG,QAAQ,OAAO,SAAS,iBAAiB,aAAa,UACtD,QAAQ,OAAO,SAAS,gBAAgB,cAAc,QACxC;GACf,QAAQ,IAAI,QAAQ,YAAY,KAAK,QAAQ,MAAM;GACnD,MAAM,MAAM,KAAK,QAAQ,MAAM;EAEjC;EACA,OAAO;CACT;CAEA,MAAM,QAAuB;EAC3B,IAAI,KAAK,QAAQ,OAAO,gBAAgB,SAItC,OAAM,MAD8B,aAAa,KAAK,QAAQ,MAAM,EAAA,CACvD,OAAO;OACf,IAAI,KAAK,QAAQ,OAAO,gBAAgB,SAAS;GAGtD,MAAM,SAAuB;IAC3B,OAAO,EAGL,OAAO,CAAC,EACV;IACA,GAAG,KAAK,QAAQ;GAClB;GACA,MAAM,MAAM,MAAM;EACpB;CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdeverywhere/plugin-vite",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist/**"
|
|
6
6
|
],
|
|
7
7
|
"type": "module",
|
|
8
|
-
"main": "dist/index.cjs",
|
|
9
8
|
"module": "dist/index.js",
|
|
10
9
|
"types": "dist/index.d.ts",
|
|
11
10
|
"exports": {
|
|
12
11
|
".": {
|
|
13
12
|
"types": "./dist/index.d.ts",
|
|
14
|
-
"import": "./dist/index.js"
|
|
15
|
-
"require": "./dist/index.cjs"
|
|
13
|
+
"import": "./dist/index.js"
|
|
16
14
|
}
|
|
17
15
|
},
|
|
18
16
|
"peerDependencies": {
|
|
19
17
|
"@sdeverywhere/build": "^0.3.10",
|
|
20
|
-
"vite": "^
|
|
18
|
+
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
21
19
|
},
|
|
22
20
|
"devDependencies": {
|
|
23
|
-
"
|
|
24
|
-
"vite": "^7.1.12"
|
|
21
|
+
"vite": "^8.3.0"
|
|
25
22
|
},
|
|
26
23
|
"author": "Climate Interactive",
|
|
27
24
|
"license": "MIT",
|
|
@@ -44,7 +41,7 @@
|
|
|
44
41
|
"test:watch": "echo No tests yet",
|
|
45
42
|
"test:ci": "echo No tests yet",
|
|
46
43
|
"type-check": "tsc --noEmit -p tsconfig-test.json",
|
|
47
|
-
"build": "
|
|
44
|
+
"build": "tsdown",
|
|
48
45
|
"docs": "../../scripts/gen-docs.js",
|
|
49
46
|
"ci:build": "run-s clean lint prettier:check test:ci type-check build docs"
|
|
50
47
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/index.ts
|
|
20
|
-
var index_exports = {};
|
|
21
|
-
__export(index_exports, {
|
|
22
|
-
vitePlugin: () => vitePlugin
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(index_exports);
|
|
25
|
-
|
|
26
|
-
// src/plugin.ts
|
|
27
|
-
var import_vite = require("vite");
|
|
28
|
-
function vitePlugin(options) {
|
|
29
|
-
return new VitePlugin(options);
|
|
30
|
-
}
|
|
31
|
-
var VitePlugin = class {
|
|
32
|
-
constructor(options) {
|
|
33
|
-
this.options = options;
|
|
34
|
-
}
|
|
35
|
-
async postGenerate(context) {
|
|
36
|
-
return this.buildIfNeeded(context, "post-generate");
|
|
37
|
-
}
|
|
38
|
-
async postBuild(context) {
|
|
39
|
-
return this.buildIfNeeded(context, "post-build");
|
|
40
|
-
}
|
|
41
|
-
async buildIfNeeded(context, caller) {
|
|
42
|
-
const applyDev = this.options.apply?.development || "post-build";
|
|
43
|
-
const applyProd = this.options.apply?.production || "post-build";
|
|
44
|
-
const shouldBuild = context.config.mode === "development" && applyDev === caller || context.config.mode === "production" && applyProd === caller;
|
|
45
|
-
if (shouldBuild) {
|
|
46
|
-
context.log("info", `Building ${this.options.name}`);
|
|
47
|
-
await (0, import_vite.build)(this.options.config);
|
|
48
|
-
}
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
async watch() {
|
|
52
|
-
if (this.options.apply?.development === "serve") {
|
|
53
|
-
const server = await (0, import_vite.createServer)(this.options.config);
|
|
54
|
-
await server.listen();
|
|
55
|
-
} else if (this.options.apply?.development === "watch") {
|
|
56
|
-
const config = {
|
|
57
|
-
build: {
|
|
58
|
-
// Enable watch mode
|
|
59
|
-
// TODO: Only do this if not already set up in the given config?
|
|
60
|
-
watch: {}
|
|
61
|
-
},
|
|
62
|
-
...this.options.config
|
|
63
|
-
};
|
|
64
|
-
await (0, import_vite.build)(config);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
69
|
-
0 && (module.exports = {
|
|
70
|
-
vitePlugin
|
|
71
|
-
});
|
|
72
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/plugin.ts"],"sourcesContent":["// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nexport type { VitePluginOptions } from './options'\nexport { vitePlugin } from './plugin'\n","// Copyright (c) 2022 Climate Interactive / New Venture Fund\n\nimport type { InlineConfig, ViteDevServer } from 'vite'\nimport { build, createServer } from 'vite'\n\nimport type { BuildContext, Plugin } from '@sdeverywhere/build'\n\nimport type { VitePluginOptions } from './options'\n\nexport function vitePlugin(options: VitePluginOptions): Plugin {\n return new VitePlugin(options)\n}\n\nclass VitePlugin implements Plugin {\n constructor(private readonly options: VitePluginOptions) {}\n\n async postGenerate(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-generate'\n return this.buildIfNeeded(context, 'post-generate')\n }\n\n async postBuild(context: BuildContext): Promise<boolean> {\n // Only build if the plugin is configured to run for 'post-build'\n return this.buildIfNeeded(context, 'post-build')\n }\n\n private async buildIfNeeded(context: BuildContext, caller: 'post-generate' | 'post-build'): Promise<boolean> {\n // The apply values default to 'post-build' when left undefined\n const applyDev = this.options.apply?.development || 'post-build'\n const applyProd = this.options.apply?.production || 'post-build'\n\n // Run \"vite build\" only if configured for this mode\n const shouldBuild =\n (context.config.mode === 'development' && applyDev === caller) ||\n (context.config.mode === 'production' && applyProd === caller)\n if (shouldBuild) {\n context.log('info', `Building ${this.options.name}`)\n await build(this.options.config)\n // context.log('info', 'Done!')\n }\n return true\n }\n\n async watch(): Promise<void> {\n if (this.options.apply?.development === 'serve') {\n // Run \"vite dev\", which starts the app in a local server and\n // refreshes when source files are changed\n const server: ViteDevServer = await createServer(this.options.config)\n await server.listen()\n } else if (this.options.apply?.development === 'watch') {\n // Run \"vite build\" in watch mode so that it rebuilds when source\n // files are changed\n const config: InlineConfig = {\n build: {\n // Enable watch mode\n // TODO: Only do this if not already set up in the given config?\n watch: {}\n },\n ...this.options.config\n }\n await build(config)\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,kBAAoC;AAM7B,SAAS,WAAW,SAAoC;AAC7D,SAAO,IAAI,WAAW,OAAO;AAC/B;AAEA,IAAM,aAAN,MAAmC;AAAA,EACjC,YAA6B,SAA4B;AAA5B;AAAA,EAA6B;AAAA,EAE1D,MAAM,aAAa,SAAyC;AAE1D,WAAO,KAAK,cAAc,SAAS,eAAe;AAAA,EACpD;AAAA,EAEA,MAAM,UAAU,SAAyC;AAEvD,WAAO,KAAK,cAAc,SAAS,YAAY;AAAA,EACjD;AAAA,EAEA,MAAc,cAAc,SAAuB,QAA0D;AAE3G,UAAM,WAAW,KAAK,QAAQ,OAAO,eAAe;AACpD,UAAM,YAAY,KAAK,QAAQ,OAAO,cAAc;AAGpD,UAAM,cACH,QAAQ,OAAO,SAAS,iBAAiB,aAAa,UACtD,QAAQ,OAAO,SAAS,gBAAgB,cAAc;AACzD,QAAI,aAAa;AACf,cAAQ,IAAI,QAAQ,YAAY,KAAK,QAAQ,IAAI,EAAE;AACnD,gBAAM,mBAAM,KAAK,QAAQ,MAAM;AAAA,IAEjC;AACA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,QAAuB;AAC3B,QAAI,KAAK,QAAQ,OAAO,gBAAgB,SAAS;AAG/C,YAAM,SAAwB,UAAM,0BAAa,KAAK,QAAQ,MAAM;AACpE,YAAM,OAAO,OAAO;AAAA,IACtB,WAAW,KAAK,QAAQ,OAAO,gBAAgB,SAAS;AAGtD,YAAM,SAAuB;AAAA,QAC3B,OAAO;AAAA;AAAA;AAAA,UAGL,OAAO,CAAC;AAAA,QACV;AAAA,QACA,GAAG,KAAK,QAAQ;AAAA,MAClB;AACA,gBAAM,mBAAM,MAAM;AAAA,IACpB;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.cts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { InlineConfig } from 'vite';
|
|
2
|
-
import { Plugin } from '@sdeverywhere/build';
|
|
3
|
-
|
|
4
|
-
interface VitePluginOptions {
|
|
5
|
-
/** The name to include in log messages. */
|
|
6
|
-
name: string;
|
|
7
|
-
/** The Vite config to use. */
|
|
8
|
-
config: InlineConfig;
|
|
9
|
-
/** Specifies the behavior of the plugin for different `sde` build modes. */
|
|
10
|
-
apply?: {
|
|
11
|
-
/**
|
|
12
|
-
* The behavior of the plugin when sde is configured for development mode.
|
|
13
|
-
*
|
|
14
|
-
* If left undefined, defaults to 'post-build'.
|
|
15
|
-
*
|
|
16
|
-
* - `skip`: Don't run the plugin.
|
|
17
|
-
* - `post-generate`: Run `vite build` in the `postGenerate` phase.
|
|
18
|
-
* - `post-build`: Run `vite build` in the `postBuild` phase.
|
|
19
|
-
* - `watch`: Run `vite build` in the `watch` callback (rebuilds the library when
|
|
20
|
-
* changes are detected in source files); useful for libraries.
|
|
21
|
-
* - `serve`: Run `vite dev` (sets up local server and refreshes the app
|
|
22
|
-
* automatically when changes are detected); useful for applications.
|
|
23
|
-
*/
|
|
24
|
-
development?: 'skip' | 'post-generate' | 'post-build' | 'watch' | 'serve';
|
|
25
|
-
/**
|
|
26
|
-
* The behavior of the plugin when sde is configured for production mode.
|
|
27
|
-
*
|
|
28
|
-
* If left undefined, defaults to 'post-build'.
|
|
29
|
-
*
|
|
30
|
-
* - `skip`: Don't run the plugin.
|
|
31
|
-
* - `post-generate`: Run `vite build` in the `postGenerate` phase.
|
|
32
|
-
* - `post-build`: Run `vite build` in the `postBuild` phase.
|
|
33
|
-
*/
|
|
34
|
-
production?: 'skip' | 'post-generate' | 'post-build';
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
declare function vitePlugin(options: VitePluginOptions): Plugin;
|
|
39
|
-
|
|
40
|
-
export { type VitePluginOptions, vitePlugin };
|