unplugin-isolated-decl 0.1.0 → 0.2.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 +16 -0
- package/dist/core/options.d.cts +0 -2
- package/dist/core/options.d.ts +0 -2
- package/dist/esbuild.cjs +1 -1
- package/dist/esbuild.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/rolldown.cjs +1 -1
- package/dist/rolldown.js +1 -1
- package/dist/rollup.cjs +1 -1
- package/dist/rollup.js +1 -1
- package/dist/src_index--yc62mkz.js +117 -0
- package/dist/src_index-ff_kRS8x.cjs +127 -0
- package/dist/vite.cjs +1 -1
- package/dist/vite.js +1 -1
- package/package.json +5 -5
- package/dist/src_index-SDbc5FOn.cjs +0 -140
- package/dist/src_index-X4PTeCWf.js +0 -130
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
npm i -D unplugin-isolated-decl
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
19
21
|
<details>
|
|
20
22
|
<summary>Vite</summary><br>
|
|
21
23
|
|
|
@@ -44,6 +46,20 @@ export default {
|
|
|
44
46
|
|
|
45
47
|
<br></details>
|
|
46
48
|
|
|
49
|
+
<details>
|
|
50
|
+
<summary>Rolldown</summary><br>
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
// rolldown.config.js
|
|
54
|
+
import UnpluginIsolatedDecl from 'unplugin-isolated-decl/rolldown'
|
|
55
|
+
|
|
56
|
+
export default {
|
|
57
|
+
plugins: [UnpluginIsolatedDecl()],
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
<br></details>
|
|
62
|
+
|
|
47
63
|
<details>
|
|
48
64
|
<summary>esbuild</summary><br>
|
|
49
65
|
|
package/dist/core/options.d.cts
CHANGED
|
@@ -3,8 +3,6 @@ export interface Options {
|
|
|
3
3
|
include?: FilterPattern;
|
|
4
4
|
exclude?: FilterPattern;
|
|
5
5
|
enforce?: (('pre') | ('post') | (undefined));
|
|
6
|
-
outDir?: string;
|
|
7
|
-
outExt?: string;
|
|
8
6
|
}
|
|
9
7
|
type Overwrite<T, U> = ((Pick<T, Exclude<keyof T, keyof U>>) & (U));
|
|
10
8
|
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce'>>;
|
package/dist/core/options.d.ts
CHANGED
|
@@ -3,8 +3,6 @@ export interface Options {
|
|
|
3
3
|
include?: FilterPattern;
|
|
4
4
|
exclude?: FilterPattern;
|
|
5
5
|
enforce?: (('pre') | ('post') | (undefined));
|
|
6
|
-
outDir?: string;
|
|
7
|
-
outExt?: string;
|
|
8
6
|
}
|
|
9
7
|
type Overwrite<T, U> = ((Pick<T, Exclude<keyof T, keyof U>>) & (U));
|
|
10
8
|
export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, 'enforce'>>;
|
package/dist/esbuild.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { __export, __toCommonJS } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { src_index_default } = require("./src_index-
|
|
4
|
+
const { src_index_default } = require("./src_index-ff_kRS8x.cjs");
|
|
5
5
|
|
|
6
6
|
//#region src/esbuild.ts
|
|
7
7
|
var esbuild_ns = {};
|
package/dist/esbuild.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { __toCommonJS } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { plugin, src_index_default, src_index_ns } = require("./src_index-
|
|
4
|
+
const { plugin, src_index_default, src_index_ns } = require("./src_index-ff_kRS8x.cjs");
|
|
5
5
|
|
|
6
6
|
module.exports = __toCommonJS(src_index_ns)
|
package/dist/index.js
CHANGED
package/dist/rolldown.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { __export, __toCommonJS } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { src_index_default } = require("./src_index-
|
|
4
|
+
const { src_index_default } = require("./src_index-ff_kRS8x.cjs");
|
|
5
5
|
|
|
6
6
|
//#region src/rolldown.ts
|
|
7
7
|
var rolldown_ns = {};
|
package/dist/rolldown.js
CHANGED
package/dist/rollup.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { __export, __toCommonJS } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { src_index_default } = require("./src_index-
|
|
4
|
+
const { src_index_default } = require("./src_index-ff_kRS8x.cjs");
|
|
5
5
|
|
|
6
6
|
//#region src/rollup.ts
|
|
7
7
|
var rollup_ns = {};
|
package/dist/rollup.js
CHANGED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { default as path } from "node:path";
|
|
2
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createUnplugin } from "unplugin";
|
|
4
|
+
import { isolatedDeclaration } from "oxc-transform";
|
|
5
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
6
|
+
|
|
7
|
+
//#region src/core/options.ts
|
|
8
|
+
function resolveOptions(options) {
|
|
9
|
+
return {
|
|
10
|
+
include: options.include || [/\.[cm]?ts$/],
|
|
11
|
+
exclude: options.exclude || [/node_modules/],
|
|
12
|
+
enforce: 'enforce'in options ? options.enforce : 'pre'
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/index.ts
|
|
18
|
+
const plugin = createUnplugin((rawOptions = {}, meta) => {
|
|
19
|
+
const options = resolveOptions(rawOptions);
|
|
20
|
+
const filter = createFilter(options.include, options.exclude);
|
|
21
|
+
const outputFiles = {};
|
|
22
|
+
function addOutput(filename, source) {
|
|
23
|
+
outputFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
24
|
+
}
|
|
25
|
+
const rollup = {renderStart(outputOptions, inputOptions) {
|
|
26
|
+
let outBase = '';
|
|
27
|
+
let input = inputOptions.input;
|
|
28
|
+
input = typeof input === 'string' ? [input] : input;
|
|
29
|
+
if (Array.isArray(input)) {
|
|
30
|
+
outBase = lowestCommonAncestor(...input);
|
|
31
|
+
}
|
|
32
|
+
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
33
|
+
return this.error('entryFileNames must be a string');
|
|
34
|
+
}
|
|
35
|
+
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
36
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
37
|
+
this.emitFile({
|
|
38
|
+
type: 'asset',
|
|
39
|
+
fileName: entryFileNames.replace('[name]', path.relative(outBase, filename)),
|
|
40
|
+
source
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}};
|
|
44
|
+
return {
|
|
45
|
+
name: 'unplugin-isolated-decl',
|
|
46
|
+
transformInclude(id) {
|
|
47
|
+
return filter(id);
|
|
48
|
+
},
|
|
49
|
+
transform(code, id) {
|
|
50
|
+
const { sourceText, errors } = isolatedDeclaration(id, code);
|
|
51
|
+
if (errors.length) {
|
|
52
|
+
this.error(errors[0]);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
addOutput(id, sourceText);
|
|
56
|
+
},
|
|
57
|
+
async buildEnd() {
|
|
58
|
+
if (meta.framework === 'esbuild') {
|
|
59
|
+
const esbuildOptions = meta.build.initialOptions;
|
|
60
|
+
const entries = esbuildOptions.entryPoints;
|
|
61
|
+
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
62
|
+
const outBase = lowestCommonAncestor(...entries);
|
|
63
|
+
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
64
|
+
let outExt;
|
|
65
|
+
switch (jsExt) {
|
|
66
|
+
case '.cjs':
|
|
67
|
+
outExt = 'cts';
|
|
68
|
+
break;
|
|
69
|
+
case '.mjs':
|
|
70
|
+
outExt = 'mts';
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
outExt = 'ts';
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
const build = meta.build;
|
|
77
|
+
if (build.initialOptions.outdir && (build.initialOptions.write ?? true)) for (const [filename, source] of Object.entries(outputFiles)) {
|
|
78
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
79
|
+
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
80
|
+
await mkdir(path.dirname(filePath), {recursive: true});
|
|
81
|
+
await writeFile(filePath, source);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
rollup,
|
|
86
|
+
rolldown: rollup,
|
|
87
|
+
vite: {
|
|
88
|
+
apply: 'build',
|
|
89
|
+
enforce: 'pre',
|
|
90
|
+
...rollup
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
var src_index_default = plugin;
|
|
95
|
+
function lowestCommonAncestor(...filepaths) {
|
|
96
|
+
if (filepaths.length === 0) return '';
|
|
97
|
+
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
98
|
+
const [first, ...rest] = filepaths;
|
|
99
|
+
let ancestor = first.split(path.sep);
|
|
100
|
+
for (const filepath of rest) {
|
|
101
|
+
const directories = filepath.split(path.sep, ancestor.length);
|
|
102
|
+
let index = 0;
|
|
103
|
+
for (const directory of directories) {
|
|
104
|
+
if (directory === ancestor[index]) {
|
|
105
|
+
index += 1;
|
|
106
|
+
} else {
|
|
107
|
+
ancestor = ancestor.slice(0, index);
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
ancestor = ancestor.slice(0, index);
|
|
112
|
+
}
|
|
113
|
+
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
export { plugin, src_index_default };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { __export, __toESM } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
+
const { default: path } = __toESM(require("node:path"));
|
|
5
|
+
const { mkdir, writeFile } = __toESM(require("node:fs/promises"));
|
|
6
|
+
const { createUnplugin } = __toESM(require("unplugin"));
|
|
7
|
+
const { isolatedDeclaration } = __toESM(require("oxc-transform"));
|
|
8
|
+
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
9
|
+
|
|
10
|
+
//#region src/core/options.ts
|
|
11
|
+
function resolveOptions(options) {
|
|
12
|
+
return {
|
|
13
|
+
include: options.include || [/\.[cm]?ts$/],
|
|
14
|
+
exclude: options.exclude || [/node_modules/],
|
|
15
|
+
enforce: 'enforce'in options ? options.enforce : 'pre'
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/index.ts
|
|
21
|
+
var src_index_ns = {};
|
|
22
|
+
__export(src_index_ns, {
|
|
23
|
+
default: () => src_index_default,
|
|
24
|
+
plugin: () => plugin
|
|
25
|
+
});
|
|
26
|
+
const plugin = createUnplugin((rawOptions = {}, meta) => {
|
|
27
|
+
const options = resolveOptions(rawOptions);
|
|
28
|
+
const filter = createFilter(options.include, options.exclude);
|
|
29
|
+
const outputFiles = {};
|
|
30
|
+
function addOutput(filename, source) {
|
|
31
|
+
outputFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
32
|
+
}
|
|
33
|
+
const rollup = {renderStart(outputOptions, inputOptions) {
|
|
34
|
+
let outBase = '';
|
|
35
|
+
let input = inputOptions.input;
|
|
36
|
+
input = typeof input === 'string' ? [input] : input;
|
|
37
|
+
if (Array.isArray(input)) {
|
|
38
|
+
outBase = lowestCommonAncestor(...input);
|
|
39
|
+
}
|
|
40
|
+
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
41
|
+
return this.error('entryFileNames must be a string');
|
|
42
|
+
}
|
|
43
|
+
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
44
|
+
for (const [filename, source] of Object.entries(outputFiles)) {
|
|
45
|
+
this.emitFile({
|
|
46
|
+
type: 'asset',
|
|
47
|
+
fileName: entryFileNames.replace('[name]', path.relative(outBase, filename)),
|
|
48
|
+
source
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}};
|
|
52
|
+
return {
|
|
53
|
+
name: 'unplugin-isolated-decl',
|
|
54
|
+
transformInclude(id) {
|
|
55
|
+
return filter(id);
|
|
56
|
+
},
|
|
57
|
+
transform(code, id) {
|
|
58
|
+
const { sourceText, errors } = isolatedDeclaration(id, code);
|
|
59
|
+
if (errors.length) {
|
|
60
|
+
this.error(errors[0]);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
addOutput(id, sourceText);
|
|
64
|
+
},
|
|
65
|
+
async buildEnd() {
|
|
66
|
+
if (meta.framework === 'esbuild') {
|
|
67
|
+
const esbuildOptions = meta.build.initialOptions;
|
|
68
|
+
const entries = esbuildOptions.entryPoints;
|
|
69
|
+
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
70
|
+
const outBase = lowestCommonAncestor(...entries);
|
|
71
|
+
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
72
|
+
let outExt;
|
|
73
|
+
switch (jsExt) {
|
|
74
|
+
case '.cjs':
|
|
75
|
+
outExt = 'cts';
|
|
76
|
+
break;
|
|
77
|
+
case '.mjs':
|
|
78
|
+
outExt = 'mts';
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
outExt = 'ts';
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
const build = meta.build;
|
|
85
|
+
if (build.initialOptions.outdir && (build.initialOptions.write ?? true)) for (const [filename, source] of Object.entries(outputFiles)) {
|
|
86
|
+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
|
|
87
|
+
const filePath = path.resolve(build.initialOptions.outdir, outFile);
|
|
88
|
+
await mkdir(path.dirname(filePath), {recursive: true});
|
|
89
|
+
await writeFile(filePath, source);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
rollup,
|
|
94
|
+
rolldown: rollup,
|
|
95
|
+
vite: {
|
|
96
|
+
apply: 'build',
|
|
97
|
+
enforce: 'pre',
|
|
98
|
+
...rollup
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
var src_index_default = plugin;
|
|
103
|
+
function lowestCommonAncestor(...filepaths) {
|
|
104
|
+
if (filepaths.length === 0) return '';
|
|
105
|
+
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
106
|
+
const [first, ...rest] = filepaths;
|
|
107
|
+
let ancestor = first.split(path.sep);
|
|
108
|
+
for (const filepath of rest) {
|
|
109
|
+
const directories = filepath.split(path.sep, ancestor.length);
|
|
110
|
+
let index = 0;
|
|
111
|
+
for (const directory of directories) {
|
|
112
|
+
if (directory === ancestor[index]) {
|
|
113
|
+
index += 1;
|
|
114
|
+
} else {
|
|
115
|
+
ancestor = ancestor.slice(0, index);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
ancestor = ancestor.slice(0, index);
|
|
120
|
+
}
|
|
121
|
+
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
exports.plugin = plugin;
|
|
126
|
+
exports.src_index_default = src_index_default;
|
|
127
|
+
exports.src_index_ns = src_index_ns;
|
package/dist/vite.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
const { __export, __toCommonJS } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { src_index_default } = require("./src_index-
|
|
4
|
+
const { src_index_default } = require("./src_index-ff_kRS8x.cjs");
|
|
5
5
|
|
|
6
6
|
//#region src/vite.ts
|
|
7
7
|
var vite_ns = {};
|
package/dist/vite.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-isolated-decl",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A blazing-fast tool for generating isolated declarations, powered by Oxc.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -69,7 +69,6 @@
|
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@rollup/pluginutils": "^5.1.0",
|
|
72
|
-
"magic-string": "^0.30.10",
|
|
73
72
|
"oxc-transform": "^0.15.1",
|
|
74
73
|
"unplugin": "^1.10.1"
|
|
75
74
|
},
|
|
@@ -78,14 +77,15 @@
|
|
|
78
77
|
"@sxzz/prettier-config": "^2.0.2",
|
|
79
78
|
"@types/node": "^20.14.9",
|
|
80
79
|
"bumpp": "^9.4.1",
|
|
80
|
+
"esbuild": "^0.21.5",
|
|
81
81
|
"eslint": "^9.5.0",
|
|
82
82
|
"fast-glob": "^3.3.2",
|
|
83
83
|
"prettier": "^3.3.2",
|
|
84
84
|
"rollup": "^4.18.0",
|
|
85
|
-
"tsdown": "^0.2.
|
|
86
|
-
"tsx": "^4.15.
|
|
85
|
+
"tsdown": "^0.2.2",
|
|
86
|
+
"tsx": "^4.15.8",
|
|
87
87
|
"typescript": "^5.5.2",
|
|
88
|
-
"vite": "^5.3.
|
|
88
|
+
"vite": "^5.3.2",
|
|
89
89
|
"vitest": "^1.6.0"
|
|
90
90
|
},
|
|
91
91
|
"engines": {
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { __export, __toESM } = require("./chunk-J8LkbbVi.cjs");
|
|
4
|
-
const { default: path } = __toESM(require("node:path"));
|
|
5
|
-
const { createUnplugin } = __toESM(require("unplugin"));
|
|
6
|
-
const { isolatedDeclaration } = __toESM(require("oxc-transform"));
|
|
7
|
-
const { createFilter } = __toESM(require("@rollup/pluginutils"));
|
|
8
|
-
|
|
9
|
-
//#region 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
|
-
//#endregion
|
|
21
|
-
//#region src/index.ts
|
|
22
|
-
var src_index_ns = {};
|
|
23
|
-
__export(src_index_ns, {
|
|
24
|
-
default: () => src_index_default,
|
|
25
|
-
plugin: () => plugin
|
|
26
|
-
});
|
|
27
|
-
const plugin = createUnplugin((rawOptions = {}, { framework }) => {
|
|
28
|
-
const options = resolveOptions(rawOptions);
|
|
29
|
-
const filter = createFilter(options.include, options.exclude);
|
|
30
|
-
const contexts = [];
|
|
31
|
-
let id = 0;
|
|
32
|
-
const emitFiles = {};
|
|
33
|
-
function emit(ctx, filename, source, outExt) {
|
|
34
|
-
if (framework === 'esbuild') {
|
|
35
|
-
ctx.emitFile({
|
|
36
|
-
type: 'asset',
|
|
37
|
-
fileName: filename.replace(/\.(.?)ts$/, `.d.${outExt}`),
|
|
38
|
-
source
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
emitFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const rollup = {
|
|
45
|
-
options(rollupOptions) {
|
|
46
|
-
let outBase = '';
|
|
47
|
-
let input = rollupOptions.input;
|
|
48
|
-
input = typeof input === 'string' ? [input] : input;
|
|
49
|
-
if (Array.isArray(input)) {
|
|
50
|
-
outBase = lowestCommonAncestor(...input);
|
|
51
|
-
}
|
|
52
|
-
if (!contexts.length) contexts.push({});
|
|
53
|
-
contexts[0] = {
|
|
54
|
-
outExt: options.outExt,
|
|
55
|
-
outBase
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
renderStart(outputOptions) {
|
|
59
|
-
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
60
|
-
return this.error('entryFileNames must be a string');
|
|
61
|
-
}
|
|
62
|
-
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
63
|
-
for (const [filename, source] of Object.entries(emitFiles)) {
|
|
64
|
-
this.emitFile({
|
|
65
|
-
type: 'asset',
|
|
66
|
-
fileName: entryFileNames.replace('[name]', filename),
|
|
67
|
-
source
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
return {
|
|
73
|
-
name: 'unplugin-isolated-decl',
|
|
74
|
-
transformInclude(id$1) {
|
|
75
|
-
return filter(id$1);
|
|
76
|
-
},
|
|
77
|
-
transform(code, id$1) {
|
|
78
|
-
const context = contexts[this.id || 0];
|
|
79
|
-
const { sourceText, errors } = isolatedDeclaration(id$1, code);
|
|
80
|
-
if (errors.length) {
|
|
81
|
-
this.error(errors[0]);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const outFile = path.relative(context.outBase, id$1);
|
|
85
|
-
emit(this, outFile, sourceText, context.outExt);
|
|
86
|
-
},
|
|
87
|
-
esbuild: {config(esbuildOptions) {
|
|
88
|
-
const entries = esbuildOptions.entryPoints;
|
|
89
|
-
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
90
|
-
const outBase = lowestCommonAncestor(...entries);
|
|
91
|
-
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
92
|
-
let outExt;
|
|
93
|
-
switch (jsExt) {
|
|
94
|
-
case '.cjs':
|
|
95
|
-
outExt = 'cts';
|
|
96
|
-
break;
|
|
97
|
-
case '.mjs':
|
|
98
|
-
outExt = 'mts';
|
|
99
|
-
break;
|
|
100
|
-
default:
|
|
101
|
-
outExt = 'ts';
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
this.id = id++;
|
|
105
|
-
contexts.push({
|
|
106
|
-
outBase,
|
|
107
|
-
outExt
|
|
108
|
-
});
|
|
109
|
-
}},
|
|
110
|
-
rollup,
|
|
111
|
-
rolldown: rollup,
|
|
112
|
-
vite: rollup
|
|
113
|
-
};
|
|
114
|
-
});
|
|
115
|
-
var src_index_default = plugin;
|
|
116
|
-
function lowestCommonAncestor(...filepaths) {
|
|
117
|
-
if (filepaths.length === 0) return '';
|
|
118
|
-
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
119
|
-
const [first, ...rest] = filepaths;
|
|
120
|
-
let ancestor = first.split(path.sep);
|
|
121
|
-
for (const filepath of rest) {
|
|
122
|
-
const directories = filepath.split(path.sep, ancestor.length);
|
|
123
|
-
let index = 0;
|
|
124
|
-
for (const directory of directories) {
|
|
125
|
-
if (directory === ancestor[index]) {
|
|
126
|
-
index += 1;
|
|
127
|
-
} else {
|
|
128
|
-
ancestor = ancestor.slice(0, index);
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
ancestor = ancestor.slice(0, index);
|
|
133
|
-
}
|
|
134
|
-
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
//#endregion
|
|
138
|
-
exports.plugin = plugin;
|
|
139
|
-
exports.src_index_default = src_index_default;
|
|
140
|
-
exports.src_index_ns = src_index_ns;
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { default as path } from "node:path";
|
|
2
|
-
import { createUnplugin } from "unplugin";
|
|
3
|
-
import { isolatedDeclaration } from "oxc-transform";
|
|
4
|
-
import { createFilter } from "@rollup/pluginutils";
|
|
5
|
-
|
|
6
|
-
//#region src/core/options.ts
|
|
7
|
-
function resolveOptions(options) {
|
|
8
|
-
return {
|
|
9
|
-
include: options.include || [/\.[cm]?[jt]sx?$/],
|
|
10
|
-
exclude: options.exclude || [/node_modules/],
|
|
11
|
-
enforce: 'enforce'in options ? options.enforce : 'pre',
|
|
12
|
-
outDir: options.outDir || 'dist',
|
|
13
|
-
outExt: options.outExt || 'ts'
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
//#region src/index.ts
|
|
19
|
-
const plugin = createUnplugin((rawOptions = {}, { framework }) => {
|
|
20
|
-
const options = resolveOptions(rawOptions);
|
|
21
|
-
const filter = createFilter(options.include, options.exclude);
|
|
22
|
-
const contexts = [];
|
|
23
|
-
let id = 0;
|
|
24
|
-
const emitFiles = {};
|
|
25
|
-
function emit(ctx, filename, source, outExt) {
|
|
26
|
-
if (framework === 'esbuild') {
|
|
27
|
-
ctx.emitFile({
|
|
28
|
-
type: 'asset',
|
|
29
|
-
fileName: filename.replace(/\.(.?)ts$/, `.d.${outExt}`),
|
|
30
|
-
source
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
emitFiles[filename.replace(/\.(.?)[jt]s$/, '')] = source;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
const rollup = {
|
|
37
|
-
options(rollupOptions) {
|
|
38
|
-
let outBase = '';
|
|
39
|
-
let input = rollupOptions.input;
|
|
40
|
-
input = typeof input === 'string' ? [input] : input;
|
|
41
|
-
if (Array.isArray(input)) {
|
|
42
|
-
outBase = lowestCommonAncestor(...input);
|
|
43
|
-
}
|
|
44
|
-
if (!contexts.length) contexts.push({});
|
|
45
|
-
contexts[0] = {
|
|
46
|
-
outExt: options.outExt,
|
|
47
|
-
outBase
|
|
48
|
-
};
|
|
49
|
-
},
|
|
50
|
-
renderStart(outputOptions) {
|
|
51
|
-
if (typeof outputOptions.entryFileNames !== 'string') {
|
|
52
|
-
return this.error('entryFileNames must be a string');
|
|
53
|
-
}
|
|
54
|
-
const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ''}ts`);
|
|
55
|
-
for (const [filename, source] of Object.entries(emitFiles)) {
|
|
56
|
-
this.emitFile({
|
|
57
|
-
type: 'asset',
|
|
58
|
-
fileName: entryFileNames.replace('[name]', filename),
|
|
59
|
-
source
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
return {
|
|
65
|
-
name: 'unplugin-isolated-decl',
|
|
66
|
-
transformInclude(id$1) {
|
|
67
|
-
return filter(id$1);
|
|
68
|
-
},
|
|
69
|
-
transform(code, id$1) {
|
|
70
|
-
const context = contexts[this.id || 0];
|
|
71
|
-
const { sourceText, errors } = isolatedDeclaration(id$1, code);
|
|
72
|
-
if (errors.length) {
|
|
73
|
-
this.error(errors[0]);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
const outFile = path.relative(context.outBase, id$1);
|
|
77
|
-
emit(this, outFile, sourceText, context.outExt);
|
|
78
|
-
},
|
|
79
|
-
esbuild: {config(esbuildOptions) {
|
|
80
|
-
const entries = esbuildOptions.entryPoints;
|
|
81
|
-
if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === 'string'))) throw new Error('unsupported entryPoints, must be an string[]');
|
|
82
|
-
const outBase = lowestCommonAncestor(...entries);
|
|
83
|
-
const jsExt = esbuildOptions.outExtension?.['.js'];
|
|
84
|
-
let outExt;
|
|
85
|
-
switch (jsExt) {
|
|
86
|
-
case '.cjs':
|
|
87
|
-
outExt = 'cts';
|
|
88
|
-
break;
|
|
89
|
-
case '.mjs':
|
|
90
|
-
outExt = 'mts';
|
|
91
|
-
break;
|
|
92
|
-
default:
|
|
93
|
-
outExt = 'ts';
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
this.id = id++;
|
|
97
|
-
contexts.push({
|
|
98
|
-
outBase,
|
|
99
|
-
outExt
|
|
100
|
-
});
|
|
101
|
-
}},
|
|
102
|
-
rollup,
|
|
103
|
-
rolldown: rollup,
|
|
104
|
-
vite: rollup
|
|
105
|
-
};
|
|
106
|
-
});
|
|
107
|
-
var src_index_default = plugin;
|
|
108
|
-
function lowestCommonAncestor(...filepaths) {
|
|
109
|
-
if (filepaths.length === 0) return '';
|
|
110
|
-
if (filepaths.length === 1) return path.dirname(filepaths[0]);
|
|
111
|
-
const [first, ...rest] = filepaths;
|
|
112
|
-
let ancestor = first.split(path.sep);
|
|
113
|
-
for (const filepath of rest) {
|
|
114
|
-
const directories = filepath.split(path.sep, ancestor.length);
|
|
115
|
-
let index = 0;
|
|
116
|
-
for (const directory of directories) {
|
|
117
|
-
if (directory === ancestor[index]) {
|
|
118
|
-
index += 1;
|
|
119
|
-
} else {
|
|
120
|
-
ancestor = ancestor.slice(0, index);
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
ancestor = ancestor.slice(0, index);
|
|
125
|
-
}
|
|
126
|
-
return ancestor.length <= 1 && ancestor[0] === '' ? path.sep + ancestor[0] : ancestor.join(path.sep);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
//#endregion
|
|
130
|
-
export { plugin, src_index_default };
|