unplugin-isolated-decl 0.0.1-beta.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2024-PRESENT 三咲智子 (https://github.com/sxzz)
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 ADDED
@@ -0,0 +1,65 @@
1
+ # unplugin-isolated-decl [![npm](https://img.shields.io/npm/v/unplugin-isolated-decl.svg)](https://npmjs.com/package/unplugin-isolated-decl)
2
+
3
+ [![Unit Test](https://github.com/unplugin/unplugin-isolated-decl/actions/workflows/unit-test.yml/badge.svg)](https://github.com/unplugin/unplugin-isolated-decl/actions/workflows/unit-test.yml)
4
+
5
+ Generate isolated declaration.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm i -D unplugin-isolated-decl
11
+ ```
12
+
13
+ <details>
14
+ <summary>Vite</summary><br>
15
+
16
+ ```ts
17
+ // vite.config.ts
18
+ import UnpluginIsolatedDecl from 'unplugin-isolated-decl/vite'
19
+
20
+ export default defineConfig({
21
+ plugins: [UnpluginIsolatedDecl()],
22
+ })
23
+ ```
24
+
25
+ <br></details>
26
+
27
+ <details>
28
+ <summary>Rollup</summary><br>
29
+
30
+ ```ts
31
+ // rollup.config.js
32
+ import UnpluginIsolatedDecl from 'unplugin-isolated-decl/rollup'
33
+
34
+ export default {
35
+ plugins: [UnpluginIsolatedDecl()],
36
+ }
37
+ ```
38
+
39
+ <br></details>
40
+
41
+ <details>
42
+ <summary>esbuild</summary><br>
43
+
44
+ ```ts
45
+ // esbuild.config.js
46
+ import { build } from 'esbuild'
47
+
48
+ build({
49
+ plugins: [require('unplugin-isolated-decl/esbuild')()],
50
+ })
51
+ ```
52
+
53
+ <br></details>
54
+
55
+ ## Sponsors
56
+
57
+ <p align="center">
58
+ <a href="https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg">
59
+ <img src='https://cdn.jsdelivr.net/gh/sxzz/sponsors/sponsors.svg'/>
60
+ </a>
61
+ </p>
62
+
63
+ ## License
64
+
65
+ [MIT](./LICENSE) License © 2023-PRESENT [三咲智子](https://github.com/sxzz)
package/dist/api.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";
package/dist/api.d.cts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/api.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/api.js ADDED
File without changes
@@ -0,0 +1,124 @@
1
+ // src/index.ts
2
+ import path from "node:path";
3
+ import {
4
+ createUnplugin
5
+ } from "unplugin";
6
+ import { isolatedDeclaration } from "oxc-transform";
7
+ import { createFilter } from "@rollup/pluginutils";
8
+
9
+ // src/core/options.ts
10
+ function resolveOptions(options) {
11
+ return {
12
+ include: options.include || [/\.[cm]?[jt]sx?$/],
13
+ exclude: options.exclude || [/node_modules/],
14
+ enforce: "enforce" in options ? options.enforce : "pre",
15
+ outDir: options.outDir || "dist",
16
+ outExt: options.outExt || "ts"
17
+ };
18
+ }
19
+
20
+ // src/index.ts
21
+ var plugin = createUnplugin((rawOptions = {}) => {
22
+ const options = resolveOptions(rawOptions);
23
+ const filter = createFilter(options.include, options.exclude);
24
+ const contexts = [];
25
+ let id = 0;
26
+ return {
27
+ name: "unplugin-isolated-decl",
28
+ transformInclude(id2) {
29
+ return filter(id2);
30
+ },
31
+ transform(code, id2) {
32
+ const context = contexts[this.id || 0];
33
+ const { sourceText, errors } = isolatedDeclaration(id2, code);
34
+ if (errors.length) {
35
+ throw new AggregateError(
36
+ errors,
37
+ "TypeScript Isolated Declarations Error"
38
+ );
39
+ }
40
+ if (!context.outDir) {
41
+ throw new Error("outDir is not set");
42
+ }
43
+ const outFile = path.relative(context.outBase, id2).replace(/\.(.?)ts$/, `.d.${context.outExt}`);
44
+ this.emitFile({
45
+ type: "asset",
46
+ fileName: outFile,
47
+ source: sourceText
48
+ });
49
+ },
50
+ esbuild: {
51
+ config(esbuildOptions) {
52
+ const entries = esbuildOptions.entryPoints;
53
+ if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string")))
54
+ throw new Error("unsupported entryPoints, must be an string[]");
55
+ const outBase = lowestCommonAncestor(...entries);
56
+ const outDir = esbuildOptions.outdir || options.outDir;
57
+ if (!outDir) {
58
+ throw new Error("outDir is not set");
59
+ }
60
+ const jsExt = esbuildOptions.outExtension?.[".js"];
61
+ let outExt;
62
+ switch (jsExt) {
63
+ case ".cjs":
64
+ outExt = "cts";
65
+ break;
66
+ case ".mjs":
67
+ outExt = "mts";
68
+ break;
69
+ default:
70
+ outExt = "ts";
71
+ break;
72
+ }
73
+ this.id = id++;
74
+ contexts.push({
75
+ outBase,
76
+ outExt,
77
+ outDir
78
+ });
79
+ }
80
+ },
81
+ rollup: {
82
+ options(rollupOptions) {
83
+ let outBase = "";
84
+ const input = rollupOptions.input;
85
+ if (Array.isArray(input)) {
86
+ outBase = lowestCommonAncestor(...input);
87
+ } else if (typeof input === "string") {
88
+ outBase = path.dirname(input);
89
+ }
90
+ if (!contexts.length) contexts.push({});
91
+ contexts[0] = {
92
+ outExt: options.outExt,
93
+ outDir: options.outDir,
94
+ outBase
95
+ };
96
+ }
97
+ }
98
+ };
99
+ });
100
+ var src_default = plugin;
101
+ function lowestCommonAncestor(...filepaths) {
102
+ if (filepaths.length <= 1) return "";
103
+ const [first, ...rest] = filepaths;
104
+ let ancestor = first.split(path.sep);
105
+ for (const filepath of rest) {
106
+ const directories = filepath.split(path.sep, ancestor.length);
107
+ let index = 0;
108
+ for (const directory of directories) {
109
+ if (directory === ancestor[index]) {
110
+ index += 1;
111
+ } else {
112
+ ancestor = ancestor.slice(0, index);
113
+ break;
114
+ }
115
+ }
116
+ ancestor = ancestor.slice(0, index);
117
+ }
118
+ return ancestor.length <= 1 && ancestor[0] === "" ? path.sep + ancestor[0] : ancestor.join(path.sep);
119
+ }
120
+
121
+ export {
122
+ plugin,
123
+ src_default
124
+ };
@@ -0,0 +1,124 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts
2
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
3
+
4
+
5
+ var _unplugin = require('unplugin');
6
+ var _oxctransform = require('oxc-transform');
7
+ var _pluginutils = require('@rollup/pluginutils');
8
+
9
+ // src/core/options.ts
10
+ function resolveOptions(options) {
11
+ return {
12
+ include: options.include || [/\.[cm]?[jt]sx?$/],
13
+ exclude: options.exclude || [/node_modules/],
14
+ enforce: "enforce" in options ? options.enforce : "pre",
15
+ outDir: options.outDir || "dist",
16
+ outExt: options.outExt || "ts"
17
+ };
18
+ }
19
+
20
+ // src/index.ts
21
+ var plugin = _unplugin.createUnplugin.call(void 0, (rawOptions = {}) => {
22
+ const options = resolveOptions(rawOptions);
23
+ const filter = _pluginutils.createFilter.call(void 0, options.include, options.exclude);
24
+ const contexts = [];
25
+ let id = 0;
26
+ return {
27
+ name: "unplugin-isolated-decl",
28
+ transformInclude(id2) {
29
+ return filter(id2);
30
+ },
31
+ transform(code, id2) {
32
+ const context = contexts[this.id || 0];
33
+ const { sourceText, errors } = _oxctransform.isolatedDeclaration.call(void 0, id2, code);
34
+ if (errors.length) {
35
+ throw new AggregateError(
36
+ errors,
37
+ "TypeScript Isolated Declarations Error"
38
+ );
39
+ }
40
+ if (!context.outDir) {
41
+ throw new Error("outDir is not set");
42
+ }
43
+ const outFile = _path2.default.relative(context.outBase, id2).replace(/\.(.?)ts$/, `.d.${context.outExt}`);
44
+ this.emitFile({
45
+ type: "asset",
46
+ fileName: outFile,
47
+ source: sourceText
48
+ });
49
+ },
50
+ esbuild: {
51
+ config(esbuildOptions) {
52
+ const entries = esbuildOptions.entryPoints;
53
+ if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string")))
54
+ throw new Error("unsupported entryPoints, must be an string[]");
55
+ const outBase = lowestCommonAncestor(...entries);
56
+ const outDir = esbuildOptions.outdir || options.outDir;
57
+ if (!outDir) {
58
+ throw new Error("outDir is not set");
59
+ }
60
+ const jsExt = _optionalChain([esbuildOptions, 'access', _ => _.outExtension, 'optionalAccess', _2 => _2[".js"]]);
61
+ let outExt;
62
+ switch (jsExt) {
63
+ case ".cjs":
64
+ outExt = "cts";
65
+ break;
66
+ case ".mjs":
67
+ outExt = "mts";
68
+ break;
69
+ default:
70
+ outExt = "ts";
71
+ break;
72
+ }
73
+ this.id = id++;
74
+ contexts.push({
75
+ outBase,
76
+ outExt,
77
+ outDir
78
+ });
79
+ }
80
+ },
81
+ rollup: {
82
+ options(rollupOptions) {
83
+ let outBase = "";
84
+ const input = rollupOptions.input;
85
+ if (Array.isArray(input)) {
86
+ outBase = lowestCommonAncestor(...input);
87
+ } else if (typeof input === "string") {
88
+ outBase = _path2.default.dirname(input);
89
+ }
90
+ if (!contexts.length) contexts.push({});
91
+ contexts[0] = {
92
+ outExt: options.outExt,
93
+ outDir: options.outDir,
94
+ outBase
95
+ };
96
+ }
97
+ }
98
+ };
99
+ });
100
+ var src_default = plugin;
101
+ function lowestCommonAncestor(...filepaths) {
102
+ if (filepaths.length <= 1) return "";
103
+ const [first, ...rest] = filepaths;
104
+ let ancestor = first.split(_path2.default.sep);
105
+ for (const filepath of rest) {
106
+ const directories = filepath.split(_path2.default.sep, ancestor.length);
107
+ let index = 0;
108
+ for (const directory of directories) {
109
+ if (directory === ancestor[index]) {
110
+ index += 1;
111
+ } else {
112
+ ancestor = ancestor.slice(0, index);
113
+ break;
114
+ }
115
+ }
116
+ ancestor = ancestor.slice(0, index);
117
+ }
118
+ return ancestor.length <= 1 && ancestor[0] === "" ? _path2.default.sep + ancestor[0] : ancestor.join(_path2.default.sep);
119
+ }
120
+
121
+
122
+
123
+
124
+ exports.plugin = plugin; exports.src_default = src_default;
@@ -0,0 +1,12 @@
1
+ import type { FilterPattern } from '@rollup/pluginutils';
2
+ export interface Options {
3
+ include?: FilterPattern;
4
+ exclude?: FilterPattern;
5
+ enforce?: (('pre') | ('post') | (undefined));
6
+ outDir?: string;
7
+ outExt?: string;
8
+ }
9
+ type Overwrite<T, U> = ((Pick<T, Exclude<keyof T, keyof U>>) & (U));
10
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce'>>;
11
+ export declare function resolveOptions(options: Options): OptionsResolved;
12
+ export {};
@@ -0,0 +1,12 @@
1
+ import type { FilterPattern } from '@rollup/pluginutils';
2
+ export interface Options {
3
+ include?: FilterPattern;
4
+ exclude?: FilterPattern;
5
+ enforce?: (('pre') | ('post') | (undefined));
6
+ outDir?: string;
7
+ outExt?: string;
8
+ }
9
+ type Overwrite<T, U> = ((Pick<T, Exclude<keyof T, keyof U>>) & (U));
10
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce'>>;
11
+ export declare function resolveOptions(options: Options): OptionsResolved;
12
+ export {};
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkXXL4LKYMcjs = require('./chunk-XXL4LKYM.cjs');
4
+
5
+ // src/esbuild.ts
6
+ var esbuild_default = _chunkXXL4LKYMcjs.src_default.esbuild;
7
+
8
+
9
+ exports.default = esbuild_default;
10
+
11
+ module.exports = exports.default;
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.esbuild;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.esbuild;
3
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import {
2
+ src_default
3
+ } from "./chunk-TQTDSJS3.js";
4
+
5
+ // src/esbuild.ts
6
+ var esbuild_default = src_default.esbuild;
7
+ export {
8
+ esbuild_default as default
9
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+ var _chunkXXL4LKYMcjs = require('./chunk-XXL4LKYM.cjs');
5
+
6
+
7
+
8
+ exports.default = _chunkXXL4LKYMcjs.src_default; exports.plugin = _chunkXXL4LKYMcjs.plugin;
@@ -0,0 +1,4 @@
1
+ import { type UnpluginInstance } from 'unplugin';
2
+ import { type Options } from './core/options';
3
+ export declare const plugin: UnpluginInstance<((Options) | (undefined)), false>;
4
+ export default plugin;
@@ -0,0 +1,4 @@
1
+ import { type UnpluginInstance } from 'unplugin';
2
+ import { type Options } from './core/options';
3
+ export declare const plugin: UnpluginInstance<((Options) | (undefined)), false>;
4
+ export default plugin;
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import {
2
+ plugin,
3
+ src_default
4
+ } from "./chunk-TQTDSJS3.js";
5
+ export {
6
+ src_default as default,
7
+ plugin
8
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkXXL4LKYMcjs = require('./chunk-XXL4LKYM.cjs');
4
+
5
+ // src/rollup.ts
6
+ var rollup_default = _chunkXXL4LKYMcjs.src_default.rollup;
7
+
8
+
9
+ exports.default = rollup_default;
10
+
11
+ module.exports = exports.default;
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.rollup;
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.rollup;
3
+ export default _default;
package/dist/rollup.js ADDED
@@ -0,0 +1,9 @@
1
+ import {
2
+ src_default
3
+ } from "./chunk-TQTDSJS3.js";
4
+
5
+ // src/rollup.ts
6
+ var rollup_default = src_default.rollup;
7
+ export {
8
+ rollup_default as default
9
+ };
package/dist/vite.cjs ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkXXL4LKYMcjs = require('./chunk-XXL4LKYM.cjs');
4
+
5
+ // src/vite.ts
6
+ var vite_default = _chunkXXL4LKYMcjs.src_default.vite;
7
+
8
+
9
+ exports.default = vite_default;
10
+
11
+ module.exports = exports.default;
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.vite;
3
+ export default _default;
package/dist/vite.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import unplugin from '.';
2
+ declare const _default: typeof unplugin.vite;
3
+ export default _default;
package/dist/vite.js ADDED
@@ -0,0 +1,9 @@
1
+ import {
2
+ src_default
3
+ } from "./chunk-TQTDSJS3.js";
4
+
5
+ // src/vite.ts
6
+ var vite_default = src_default.vite;
7
+ export {
8
+ vite_default as default
9
+ };
package/package.json ADDED
@@ -0,0 +1,104 @@
1
+ {
2
+ "name": "unplugin-isolated-decl",
3
+ "version": "0.0.1-beta.1",
4
+ "description": "Generate isolated declaration.",
5
+ "type": "module",
6
+ "keywords": [
7
+ "unplugin",
8
+ "rollup",
9
+ "vite",
10
+ "esbuild",
11
+ "webpack"
12
+ ],
13
+ "license": "MIT",
14
+ "homepage": "https://github.com/unplugin/unplugin-isolated-decl#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/unplugin/unplugin-isolated-decl/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/unplugin/unplugin-isolated-decl.git"
21
+ },
22
+ "author": "三咲智子 <sxzz@sxzz.moe>",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "main": "./dist/index.js",
27
+ "module": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "require": "./dist/index.cjs",
32
+ "import": "./dist/index.js"
33
+ },
34
+ "./vite": {
35
+ "require": "./dist/vite.cjs",
36
+ "import": "./dist/vite.js"
37
+ },
38
+ "./webpack": {
39
+ "require": "./dist/webpack.cjs",
40
+ "import": "./dist/webpack.js"
41
+ },
42
+ "./rollup": {
43
+ "require": "./dist/rollup.cjs",
44
+ "import": "./dist/rollup.js"
45
+ },
46
+ "./esbuild": {
47
+ "require": "./dist/esbuild.cjs",
48
+ "import": "./dist/esbuild.js"
49
+ },
50
+ "./api": {
51
+ "require": "./dist/api.cjs",
52
+ "import": "./dist/api.js"
53
+ },
54
+ "./*": [
55
+ "./*",
56
+ "./*.d.ts"
57
+ ]
58
+ },
59
+ "typesVersions": {
60
+ "*": {
61
+ "*": [
62
+ "./dist/*",
63
+ "./*"
64
+ ]
65
+ }
66
+ },
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "dependencies": {
71
+ "@rollup/pluginutils": "^5.1.0",
72
+ "magic-string": "^0.30.10",
73
+ "oxc-transform": "^0.15.1",
74
+ "unplugin": "^1.10.1"
75
+ },
76
+ "devDependencies": {
77
+ "@sxzz/eslint-config": "^3.13.0",
78
+ "@sxzz/prettier-config": "^2.0.2",
79
+ "@types/node": "^20.14.8",
80
+ "bumpp": "^9.4.1",
81
+ "eslint": "^9.5.0",
82
+ "fast-glob": "^3.3.2",
83
+ "prettier": "^3.3.2",
84
+ "rollup": "^4.18.0",
85
+ "tsup": "^8.1.0",
86
+ "tsx": "^4.15.7",
87
+ "typescript": "^5.5.2",
88
+ "vite": "^5.3.1",
89
+ "vitest": "^1.6.0"
90
+ },
91
+ "engines": {
92
+ "node": ">=18.12.0"
93
+ },
94
+ "prettier": "@sxzz/prettier-config",
95
+ "scripts": {
96
+ "lint": "eslint --cache .",
97
+ "lint:fix": "pnpm run lint --fix",
98
+ "build": "tsup",
99
+ "dev": "tsup --watch",
100
+ "test": "vitest",
101
+ "typecheck": "tsc --noEmit",
102
+ "release": "bumpp && pnpm publish"
103
+ }
104
+ }