@polyfill-prune/vite-plugin 0.0.0-stage → 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # @polyfill-prune/vite-plugin
2
+
3
+ ## 0.1.0
4
+
5
+ - Wrap the native library for development transforms, dependency optimization, and production chunks.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Justinidlerz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,45 @@
1
- # Temporary Holding Version
1
+ # @polyfill-prune/vite-plugin
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A polyfill pruning wrapper for Vite 6–8. Compatibility checks and AST rules run in the Rust API provided by `@polyfill-prune/core`. Requires Node.js >= 22.12.0 and compliance with the Node.js requirements of your Vite version.
4
+
5
+ ## Setup
6
+
7
+ Install from the public npm registry with `pnpm add -D @polyfill-prune/vite-plugin`. To work from this repository, run `pnpm install && pnpm build`, followed by `pnpm example:vite`.
8
+
9
+ ```ts
10
+ import { defineConfig } from 'vite';
11
+ import polyfillPrune from '@polyfill-prune/vite-plugin';
12
+
13
+ export default defineConfig({
14
+ build: { target: ['chrome100', 'safari15.4'] },
15
+ plugins: [polyfillPrune()],
16
+ });
17
+ ```
18
+
19
+ ## Options
20
+
21
+ | Option | Default | Description |
22
+ | --------------------------- | ----------------------- | ---------------------------------------------------------------------------------------------------- |
23
+ | `targets` | Resolved `build.target` | Explicit runtime target array; can override a build target that specifies only ES syntax |
24
+ | `keep` | `[]` | Preserve APIs and their descendants |
25
+ | `sourceMap` | `true` | Generate transformation maps for Vite to compose |
26
+ | `filter(id)` | JS/TS module extensions | Receives module IDs during development and chunk filenames during production builds |
27
+ | `optimizeDeps` | `true` | Run the core API during development dependency prebundling |
28
+ | `onResult(result, context)` | None | Receive each analysis result; context contains `filename` and `stage`; async callbacks are supported |
29
+
30
+ `stage` is `dev`, `dependency`, or `build`. Virtual modules and requests with `?raw` or `?url` are skipped. If the filter captures mutable external state, use Vite's `--force` option to refresh the dependency cache after that state changes.
31
+
32
+ ## Execution stages
33
+
34
+ | Scenario | Hook | Output |
35
+ | -------------------------------- | -------------------- | -------------------------------------------------------------------------- |
36
+ | Development source | `transform`, post | Pruned code for requests and HMR updates |
37
+ | Vite 6/7 dependency optimization | esbuild `onLoad` | Pruned JavaScript input passed to esbuild |
38
+ | Vite 8 dependency optimization | Rolldown `transform` | Pruned code with source map chaining |
39
+ | Production build | `renderChunk`, pre | Pruned bundled chunks used by subsequent minification and hash calculation |
40
+
41
+ Production pruning runs on output chunks. If another output plugin injects polyfills after this plugin, adjust its hook order. Filtering chunk filenames does not filter individual source modules within those chunks.
42
+
43
+ Only the `client` environment is processed. SSR, worker, and other environments are skipped. The development dependency cache includes targets, `keep`, the filter's source text, and core/data versions in the plugin identifier. In Vite 6/7, an earlier custom esbuild loader that handles a file prevents this plugin's `onLoad` hook from processing it again.
44
+
45
+ The plugin removes only branches the core library can prove unnecessary. It does not guarantee removal of arbitrary bundled core-js code or custom installers. Syntax-only build targets such as `esnext` or `es2015` require an explicit `targets` option.
@@ -0,0 +1,16 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { PruneOptions, PruneResult } from '@polyfill-prune/core';
3
+ export interface PolyfillPruneOptions extends Omit<PruneOptions, 'filename' | 'targets'> {
4
+ /** Defaults to the resolved Vite build.target. Syntax-only targets require an override. */
5
+ targets?: string[];
6
+ /** Defaults to JS/TS module files. Receives module IDs in dev and chunk filenames in build. */
7
+ filter?: (id: string) => boolean;
8
+ /** Process dependency optimizer inputs during development. Default: true. */
9
+ optimizeDeps?: boolean;
10
+ onResult?: (result: PruneResult, context: {
11
+ filename: string;
12
+ stage: 'dev' | 'dependency' | 'build';
13
+ }) => void | Promise<void>;
14
+ }
15
+ export declare function polyfillPrune(options?: PolyfillPruneOptions): Plugin;
16
+ export default polyfillPrune;
package/dist/index.js ADDED
@@ -0,0 +1,106 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { dirname } from 'node:path';
4
+ import * as vite from 'vite';
5
+ import { prune, version } from '@polyfill-prune/core';
6
+ export function polyfillPrune(options = {}) {
7
+ let config;
8
+ let targets;
9
+ let active = true;
10
+ const accepts = (id) => !id.includes('\0') &&
11
+ !/[?&](raw|url)(?:&|$)/.test(id) &&
12
+ (options.filter?.(id) ?? /\.[cm]?[jt]sx?(?:\?|$)/.test(id));
13
+ async function transform(code, id, stage) {
14
+ if (!active || !accepts(id))
15
+ return null;
16
+ const result = await prune(code, {
17
+ targets,
18
+ filename: id.split('?')[0],
19
+ keep: options.keep,
20
+ sourceMap: options.sourceMap,
21
+ });
22
+ for (const diagnostic of result.diagnostics) {
23
+ config.logger.warn(`[polyfill-prune] ${id}: ${diagnostic}`);
24
+ }
25
+ await options.onResult?.(result, { filename: id, stage });
26
+ return result.changed ? { code: result.code, map: result.map } : null;
27
+ }
28
+ // The optimizer hashes plugin names. Include native/data versions and options
29
+ // after config resolution so a target change cannot reuse stale dependency code.
30
+ const optimizer = {
31
+ name: 'polyfill-prune:dependencies',
32
+ transform: (code, id) => transform(code, id, 'dependency'),
33
+ };
34
+ const legacyOptimizer = {
35
+ name: 'polyfill-prune:dependencies',
36
+ setup(build) {
37
+ build.onLoad({ filter: /\.[cm]?js$/, namespace: 'file' }, async ({ path }) => {
38
+ if (!active || !accepts(path))
39
+ return;
40
+ const result = await transform(await readFile(path, 'utf8'), path, 'dependency');
41
+ if (!result)
42
+ return;
43
+ const comment = '//# source' + 'MappingURL=data:application/json;base64,';
44
+ const map = result.map
45
+ ? `\n${comment}${Buffer.from(JSON.stringify(result.map)).toString('base64')}`
46
+ : '';
47
+ return { contents: result.code + map, loader: 'js', resolveDir: dirname(path) };
48
+ });
49
+ },
50
+ };
51
+ return {
52
+ name: '@polyfill-prune/vite-plugin',
53
+ enforce: 'post',
54
+ applyToEnvironment(environment) {
55
+ return environment.name === 'client';
56
+ },
57
+ configEnvironment(name, _, env) {
58
+ if (name !== 'client' || env.command !== 'serve' || options.optimizeDeps === false)
59
+ return;
60
+ return 'rolldownVersion' in vite
61
+ ? { optimizeDeps: { rolldownOptions: { plugins: [optimizer] } } }
62
+ : { optimizeDeps: { esbuildOptions: { plugins: [legacyOptimizer] } } };
63
+ },
64
+ async configResolved(resolved) {
65
+ config = resolved;
66
+ active = !resolved.build.ssr && !resolved.isWorker;
67
+ if (!active)
68
+ return;
69
+ const target = options.targets ?? resolved.build.target;
70
+ targets = typeof target === 'string' ? [target] : target || [];
71
+ // Rust owns target parsing and validates even when no module needs pruning.
72
+ await prune('', { targets, sourceMap: false });
73
+ const fingerprint = createHash('sha256')
74
+ .update(JSON.stringify({
75
+ targets,
76
+ keep: options.keep,
77
+ sourceMap: options.sourceMap,
78
+ filter: options.filter?.toString(),
79
+ version: version(),
80
+ }))
81
+ .digest('hex');
82
+ optimizer.name += `:${fingerprint}`;
83
+ legacyOptimizer.name = optimizer.name;
84
+ },
85
+ transform: {
86
+ order: 'post',
87
+ handler(code, id, transformOptions) {
88
+ if (config.command !== 'serve' ||
89
+ transformOptions?.ssr ||
90
+ this.environment?.config.consumer === 'server')
91
+ return null;
92
+ return transform(code, id, 'dev');
93
+ },
94
+ },
95
+ renderChunk: {
96
+ order: 'pre',
97
+ handler(code, chunk) {
98
+ if (config.command !== 'build' || this.environment?.config.consumer === 'server')
99
+ return null;
100
+ return transform(code, chunk.fileName, 'build');
101
+ },
102
+ },
103
+ };
104
+ }
105
+ export default polyfillPrune;
106
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAE7B,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAgBtD,MAAM,UAAU,aAAa,CAAC,UAAgC,EAAE;IAC9D,IAAI,MAAsB,CAAC;IAC3B,IAAI,OAAiB,CAAC;IACtB,IAAI,MAAM,GAAG,IAAI,CAAC;IAClB,MAAM,OAAO,GAAG,CAAC,EAAU,EAAE,EAAE,CAC7B,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC;QAClB,CAAC,sBAAsB,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,IAAI,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IAE9D,KAAK,UAAU,SAAS,CAAC,IAAY,EAAE,EAAU,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,IAAI,EAAE,EAAE,EAAE,KAAK,CAAC,CAAC;YACpC,CAAC;SACF;QACD,WAAW,EAAE;YACX,KAAK,EAAE,KAAK;YACZ,OAAO,CAAC,IAAI,EAAE,KAAK;gBACjB,IAAI,MAAM,CAAC,OAAO,KAAK,OAAO,IAAI,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,QAAQ,KAAK,QAAQ;oBAC9E,OAAO,IAAI,CAAC;gBACd,OAAO,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;YAClD,CAAC;SACF;KACF,CAAC;AACJ,CAAC;AAED,eAAe,aAAa,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@polyfill-prune/vite-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Vite integration for the polyfill-prune Rust library",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": ">=22.12.0"
9
+ },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "README.md",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "dependencies": {
25
+ "@polyfill-prune/core": "0.1.0"
26
+ },
27
+ "peerDependencies": {
28
+ "vite": ">=6 <9"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^22.19.0",
32
+ "typescript": "^5.9.3",
33
+ "vite": "^8.2.1"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public",
37
+ "registry": "https://registry.npmjs.org/",
38
+ "provenance": true
39
+ },
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/Justinidlerz/polyfill-prune.git",
43
+ "directory": "packages/vite-plugin"
44
+ },
45
+ "scripts": {
46
+ "build": "tsc -p tsconfig.json",
47
+ "typecheck": "tsc --noEmit -p tsconfig.json"
48
+ }
6
49
  }