@ata-project/unplugin 0.3.0 → 0.4.0
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 +10 -5
- package/package.json +3 -3
- package/src/core.js +1 -1
- package/src/index.d.ts +1 -1
- package/src/index.js +7 -2
package/README.md
CHANGED
|
@@ -6,6 +6,11 @@ The generated module imports nothing. A small schema compiles to about 1.3 KB gz
|
|
|
6
6
|
|
|
7
7
|
Schemas can be authored as `.json`, `.js` or `.ts`.
|
|
8
8
|
|
|
9
|
+
It also compiles away `new Validator(schema)` in your own code wherever the schema
|
|
10
|
+
is known at build time and the result is the same, so the runtime compiler stays
|
|
11
|
+
out of the bundle: for the test entry, 122.5 KB gzipped becomes 16.7 KB. See
|
|
12
|
+
[compileAway](#compileaway-keep-new-validator-drop-the-compiler).
|
|
13
|
+
|
|
9
14
|
## Install
|
|
10
15
|
|
|
11
16
|
```bash
|
|
@@ -145,12 +150,12 @@ Other sources (`.json` without the `.schema` suffix, `.js`, `.ts`) produce
|
|
|
145
150
|
| `nameFromFile` | file name in PascalCase | Type name for schemas without `title` or `$id`. |
|
|
146
151
|
| `root` | from the bundler | Where the globs resolve from. Vite's root, Webpack's and Rspack's `context` and esbuild's `absWorkingDir` are read; Rollup and Rolldown need it passed. |
|
|
147
152
|
| `alias` | Vite's `resolve.alias` | Import aliases inside `.ts` schema files. tsconfig `paths` work without configuration. |
|
|
148
|
-
| `compileAway` | `
|
|
153
|
+
| `compileAway` | `true` | Replace `new Validator(schema)` in your code with a validator compiled at build time, where that gives the same results. See below. Needs ata-validator 1.36.0; with an older version the runtime stays, and a warning is printed only if you set the option yourself. |
|
|
149
154
|
|
|
150
155
|
## compileAway: keep `new Validator`, drop the compiler
|
|
151
156
|
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
Code written against the runtime API is compiled at build time without being
|
|
158
|
+
changed. This is on by default; `compileAway: false` turns it off.
|
|
154
159
|
|
|
155
160
|
```js
|
|
156
161
|
import { Validator } from 'ata-validator'
|
|
@@ -168,8 +173,8 @@ The plugin puts a compiled validator in place of the `new Validator(...)` call,
|
|
|
168
173
|
and once nothing else in the file uses the `ata-validator` import, the import
|
|
169
174
|
goes with it, so the runtime compiler is not in the bundle. For the three-schema
|
|
170
175
|
entry in `test/fixtures/compile-away`, one of them with defaults, a minified Vite
|
|
171
|
-
library build is
|
|
172
|
-
ata-validator 1.
|
|
176
|
+
library build is 122.5 KB gzipped without it and 16.7 KB with it (gzip level 9), on
|
|
177
|
+
ata-validator 1.39.2. Across the 977 schemas of SchemaStore, 725 can be compiled
|
|
173
178
|
away this way.
|
|
174
179
|
|
|
175
180
|
The replacement answers `validate()`, `isValidObject()`, `validateJSON()` and
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ata-project/unplugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Build-time JSON Schema for Vite, Webpack, Rollup, Rolldown, esbuild and Rspack. compileAway takes the runtime compiler out of the bundle.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@rspack/core": "^1.7.0",
|
|
81
|
-
"ata-validator": "^1.
|
|
81
|
+
"ata-validator": "^1.39.2",
|
|
82
82
|
"esbuild": "^0.28.0",
|
|
83
83
|
"rolldown": "^1.2.8",
|
|
84
84
|
"rollup": "^4.60.0",
|
package/src/core.js
CHANGED
|
@@ -41,7 +41,7 @@ const DEFAULT_OPTIONS = {
|
|
|
41
41
|
format: 'esm',
|
|
42
42
|
abortEarly: false,
|
|
43
43
|
types: true,
|
|
44
|
-
compileAway:
|
|
44
|
+
compileAway: true,
|
|
45
45
|
nameFromFile: (file) => {
|
|
46
46
|
const base = path.basename(file, path.extname(file)).replace(/\.schema$/i, '')
|
|
47
47
|
return pascal(base)
|
package/src/index.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export interface Options {
|
|
|
27
27
|
* only used through validate(), isValidObject(), validateJSON() and
|
|
28
28
|
* isValidJSON(), so the runtime compiler stays out of the bundle. Results are
|
|
29
29
|
* the ones the runtime gives. Needs ata-validator 1.35.0 or later.
|
|
30
|
-
* Default: `false
|
|
30
|
+
* Default: `true`; `false` turns it off.
|
|
31
31
|
*/
|
|
32
32
|
compileAway?: boolean
|
|
33
33
|
}
|
package/src/index.js
CHANGED
|
@@ -34,8 +34,11 @@ export const unpluginFactory = (userOptions = {}) => {
|
|
|
34
34
|
// compileAway: `new Validator(schema)` with a schema the build can read
|
|
35
35
|
// becomes a validator compiled here, and the runtime compiler leaves the
|
|
36
36
|
// bundle. See src/compile-away.js for what is replaced and what is not.
|
|
37
|
+
// On unless the caller turned it off. It replaces a call only where the
|
|
38
|
+
// compiled validator answers exactly as the runtime would, so being on by
|
|
39
|
+
// default can cost a saving but not change a result.
|
|
37
40
|
transformInclude(id) {
|
|
38
|
-
if (
|
|
41
|
+
if (userOptions.compileAway === false) return false
|
|
39
42
|
const file = id.split('?')[0]
|
|
40
43
|
return SOURCE.test(file) && !file.split(/[\\/]/).includes('node_modules')
|
|
41
44
|
},
|
|
@@ -44,7 +47,9 @@ export const unpluginFactory = (userOptions = {}) => {
|
|
|
44
47
|
if (!code.includes('ata-validator')) return null
|
|
45
48
|
const api = await session.api()
|
|
46
49
|
if (!api.compiledModuleFor) {
|
|
47
|
-
|
|
50
|
+
// Only a caller who asked for it hears that it could not happen; on
|
|
51
|
+
// by default, an older ata-validator just keeps the runtime.
|
|
52
|
+
if (userOptions.compileAway === true && !session.warnedCompileAway) {
|
|
48
53
|
session.warnedCompileAway = true
|
|
49
54
|
session.logger?.warn?.('[unplugin-ata] compileAway needs ata-validator 1.36.0 or later; nothing was replaced')
|
|
50
55
|
}
|