unplugin-isolated-decl 0.6.0 → 0.6.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/README.md CHANGED
@@ -93,9 +93,27 @@ export interface Options {
93
93
 
94
94
  /** An extra directory layer for output files. */
95
95
  extraOutdir?: string
96
+ /** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
97
+ autoAddExts?: boolean
96
98
  }
97
99
  ```
98
100
 
101
+ ### `autoAddExts`
102
+
103
+ Automatically add `.js` extension to resolve in Node 16+ ESM mode.
104
+
105
+ ```ts
106
+ // index.d.ts
107
+ import {} from './foo'
108
+ ```
109
+
110
+ With `autoAddExts`, it will be transformed to:
111
+
112
+ ```ts
113
+ // index.d.ts
114
+ import {} from './foo.js'
115
+ ```
116
+
99
117
  ## Sponsors
100
118
 
101
119
  <p align="center">
@@ -107,3 +125,7 @@ export interface Options {
107
125
  ## License
108
126
 
109
127
  [MIT](./LICENSE) License © 2024-PRESENT [三咲智子](https://github.com/sxzz)
128
+
129
+ ```
130
+
131
+ ```
package/dist/api.d.cts CHANGED
@@ -1 +1 @@
1
- export { oxcTransform, swcTransform, tsTransform } from './core/transformer';
1
+ export { oxcTransform, swcTransform, tsTransform } from "./core/transformer.js";
package/dist/api.d.ts CHANGED
@@ -1 +1 @@
1
- export { oxcTransform, swcTransform, tsTransform } from './core/transformer';
1
+ export { oxcTransform, swcTransform, tsTransform } from "./core/transformer.js";
@@ -7,6 +7,8 @@ export type Options = {
7
7
  ignoreErrors?: boolean;
8
8
  /** An extra directory layer for output files. */
9
9
  extraOutdir?: string;
10
+ /** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
11
+ autoAddExts?: boolean;
10
12
  } & ({
11
13
  /**
12
14
  * `oxc-transform` or `@swc/core` should be installed yourself
@@ -7,6 +7,8 @@ export type Options = {
7
7
  ignoreErrors?: boolean;
8
8
  /** An extra directory layer for output files. */
9
9
  extraOutdir?: string;
10
+ /** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
11
+ autoAddExts?: boolean;
10
12
  } & ({
11
13
  /**
12
14
  * `oxc-transform` or `@swc/core` should be installed yourself
package/dist/esbuild.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index--m5BPU5W.cjs');
2
+ const require_src_index = require('./src_index-Q6qrrUBW.cjs');
3
3
  require('./transformer-ZHFQTLaY.cjs');
4
4
 
5
5
  //#region src/esbuild.ts
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.esbuild;
3
3
  export default _default;
package/dist/esbuild.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.esbuild;
3
3
  export default _default;
package/dist/esbuild.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-YxBC7dLI.js";
2
- import { IsolatedDecl } from "./src_index-pbl3XXPp.js";
2
+ import { IsolatedDecl } from "./src_index-g0Uyr2cn.js";
3
3
 
4
4
  //#region src/esbuild.ts
5
5
  var esbuild_default = IsolatedDecl.esbuild;
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index--m5BPU5W.cjs');
2
+ const require_src_index = require('./src_index-Q6qrrUBW.cjs');
3
3
  require('./transformer-ZHFQTLaY.cjs');
4
4
 
5
5
  Object.defineProperty(exports, 'IsolatedDecl', {
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type UnpluginInstance } from 'unplugin';
2
- import { type Options } from './core/options';
2
+ import { type Options } from "./core/options.js";
3
3
  export type { Options };
4
4
  export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
5
5
  export declare function lowestCommonAncestor(...filepaths: string[]): string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type UnpluginInstance } from 'unplugin';
2
- import { type Options } from './core/options';
2
+ import { type Options } from "./core/options.js";
3
3
  export type { Options };
4
4
  export declare const IsolatedDecl: UnpluginInstance<Options | undefined, false>;
5
5
  export declare function lowestCommonAncestor(...filepaths: string[]): string;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./transformer-YxBC7dLI.js";
2
- import { IsolatedDecl, lowestCommonAncestor } from "./src_index-pbl3XXPp.js";
2
+ import { IsolatedDecl, lowestCommonAncestor } from "./src_index-g0Uyr2cn.js";
3
3
 
4
4
  export { IsolatedDecl, lowestCommonAncestor };
package/dist/rolldown.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index--m5BPU5W.cjs');
2
+ const require_src_index = require('./src_index-Q6qrrUBW.cjs');
3
3
  require('./transformer-ZHFQTLaY.cjs');
4
4
 
5
5
  //#region src/rolldown.ts
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.rolldown;
3
3
  export default _default;
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.rolldown;
3
3
  export default _default;
package/dist/rolldown.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-YxBC7dLI.js";
2
- import { IsolatedDecl } from "./src_index-pbl3XXPp.js";
2
+ import { IsolatedDecl } from "./src_index-g0Uyr2cn.js";
3
3
 
4
4
  //#region src/rolldown.ts
5
5
  var rolldown_default = IsolatedDecl.rolldown;
package/dist/rollup.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index--m5BPU5W.cjs');
2
+ const require_src_index = require('./src_index-Q6qrrUBW.cjs');
3
3
  require('./transformer-ZHFQTLaY.cjs');
4
4
 
5
5
  //#region src/rollup.ts
package/dist/rollup.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.rollup;
3
3
  export default _default;
package/dist/rollup.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.rollup;
3
3
  export default _default;
package/dist/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-YxBC7dLI.js";
2
- import { IsolatedDecl } from "./src_index-pbl3XXPp.js";
2
+ import { IsolatedDecl } from "./src_index-g0Uyr2cn.js";
3
3
 
4
4
  //#region src/rollup.ts
5
5
  var rollup_default = IsolatedDecl.rollup;
@@ -26,6 +26,7 @@ const require_transformer = require('./transformer-ZHFQTLaY.cjs');
26
26
  const { mkdir, readFile, writeFile } = __toESM(require("node:fs/promises"));
27
27
  const { default: path } = __toESM(require("node:path"));
28
28
  const { createFilter } = __toESM(require("@rollup/pluginutils"));
29
+ const { default: MagicString } = __toESM(require("magic-string"));
29
30
  const { parseAsync } = __toESM(require("oxc-parser"));
30
31
  const { createUnplugin } = __toESM(require("unplugin"));
31
32
 
@@ -37,7 +38,8 @@ function resolveOptions(options) {
37
38
  enforce: "enforce" in options ? options.enforce : "pre",
38
39
  transformer: options.transformer || "typescript",
39
40
  ignoreErrors: options.ignoreErrors || false,
40
- extraOutdir: options.extraOutdir
41
+ extraOutdir: options.extraOutdir,
42
+ autoAddExts: options.autoAddExts || false
41
43
  };
42
44
  }
43
45
 
@@ -90,6 +92,25 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
90
92
  }
91
93
  };
92
94
  async function transform(context, code, id) {
95
+ let program;
96
+ try {
97
+ program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
98
+ } catch {}
99
+ if (options.autoAddExts && program) {
100
+ const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
101
+ const s = new MagicString(code);
102
+ for (const i of imports) {
103
+ if (!i.source || path.basename(i.source.value).includes(".")) {
104
+ continue;
105
+ }
106
+ const resolved = await resolve(context, i.source.value, id);
107
+ if (!resolved || resolved.external) continue;
108
+ if (resolved.id.endsWith(".ts")) {
109
+ s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
110
+ }
111
+ }
112
+ code = s.toString();
113
+ }
93
114
  let result;
94
115
  switch (options.transformer) {
95
116
  case "oxc":
@@ -110,19 +131,21 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
110
131
  }
111
132
  }
112
133
  addOutput(id, sourceText);
113
- let program;
114
- try {
115
- program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
116
- } catch {
117
- return;
118
- }
134
+ if (!program) return;
119
135
  const typeImports = program.body.filter((node) => {
120
- if (node.type !== "ImportDeclaration") return false;
121
- if (node.importKind === "type") return true;
122
- return (node.specifiers || []).every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
136
+ if (node.type === "ImportDeclaration") {
137
+ if (node.importKind === "type") return true;
138
+ return node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
139
+ }
140
+ if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") {
141
+ if (node.exportKind === "type") return true;
142
+ return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.type === "ExportSpecifier" && spec.exportKind === "type");
143
+ }
144
+ return false;
123
145
  });
124
146
  for (const i of typeImports) {
125
- const resolved = await resolve(context, i.source.value, id);
147
+ if (!i.source) continue;
148
+ const resolved = (await resolve(context, i.source.value, id))?.id;
126
149
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
127
150
  let source;
128
151
  try {
@@ -182,17 +205,26 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
182
205
  });
183
206
  }
184
207
  });
185
- const resolve = async (context, id, importer) => {
208
+ async function resolve(context, id, importer) {
186
209
  const nativeContext = context.getNativeBuildContext?.();
187
210
  if (nativeContext?.framework === "esbuild") {
188
- return (await nativeContext.build.resolve(id, {
211
+ const resolved$1 = await nativeContext.build.resolve(id, {
189
212
  importer,
190
213
  resolveDir: path.dirname(importer),
191
214
  kind: "import-statement"
192
- })).path;
215
+ });
216
+ return {
217
+ id: resolved$1.path,
218
+ external: resolved$1.external
219
+ };
193
220
  }
194
- return (await context.resolve(id, importer))?.id;
195
- };
221
+ const resolved = await context.resolve(id, importer);
222
+ if (!resolved) return;
223
+ return {
224
+ id: resolved.id,
225
+ external: !!resolved.external
226
+ };
227
+ }
196
228
  function stripExt(filename) {
197
229
  return filename.replace(/\.(.?)[jt]s$/, "");
198
230
  }
@@ -2,6 +2,7 @@ import { oxcTransform, swcTransform, tsTransform } from "./transformer-YxBC7dLI.
2
2
  import { mkdir, readFile, writeFile } from "node:fs/promises";
3
3
  import { default as path } from "node:path";
4
4
  import { createFilter } from "@rollup/pluginutils";
5
+ import { default as MagicString } from "magic-string";
5
6
  import { parseAsync } from "oxc-parser";
6
7
  import { createUnplugin } from "unplugin";
7
8
 
@@ -13,7 +14,8 @@ function resolveOptions(options) {
13
14
  enforce: "enforce" in options ? options.enforce : "pre",
14
15
  transformer: options.transformer || "typescript",
15
16
  ignoreErrors: options.ignoreErrors || false,
16
- extraOutdir: options.extraOutdir
17
+ extraOutdir: options.extraOutdir,
18
+ autoAddExts: options.autoAddExts || false
17
19
  };
18
20
  }
19
21
 
@@ -66,6 +68,25 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
66
68
  }
67
69
  };
68
70
  async function transform(context, code, id) {
71
+ let program;
72
+ try {
73
+ program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
74
+ } catch {}
75
+ if (options.autoAddExts && program) {
76
+ const imports = program.body.filter((node) => node.type === "ImportDeclaration" || node.type === "ExportAllDeclaration" || node.type === "ExportNamedDeclaration");
77
+ const s = new MagicString(code);
78
+ for (const i of imports) {
79
+ if (!i.source || path.basename(i.source.value).includes(".")) {
80
+ continue;
81
+ }
82
+ const resolved = await resolve(context, i.source.value, id);
83
+ if (!resolved || resolved.external) continue;
84
+ if (resolved.id.endsWith(".ts")) {
85
+ s.overwrite(i.source.start, i.source.end, JSON.stringify(`${i.source.value}.js`));
86
+ }
87
+ }
88
+ code = s.toString();
89
+ }
69
90
  let result;
70
91
  switch (options.transformer) {
71
92
  case "oxc":
@@ -86,19 +107,21 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
86
107
  }
87
108
  }
88
109
  addOutput(id, sourceText);
89
- let program;
90
- try {
91
- program = JSON.parse((await parseAsync(code, { sourceFilename: id })).program);
92
- } catch {
93
- return;
94
- }
110
+ if (!program) return;
95
111
  const typeImports = program.body.filter((node) => {
96
- if (node.type !== "ImportDeclaration") return false;
97
- if (node.importKind === "type") return true;
98
- return (node.specifiers || []).every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
112
+ if (node.type === "ImportDeclaration") {
113
+ if (node.importKind === "type") return true;
114
+ return node.specifiers && node.specifiers.every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
115
+ }
116
+ if (node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") {
117
+ if (node.exportKind === "type") return true;
118
+ return node.type === "ExportNamedDeclaration" && node.specifiers && node.specifiers.every((spec) => spec.type === "ExportSpecifier" && spec.exportKind === "type");
119
+ }
120
+ return false;
99
121
  });
100
122
  for (const i of typeImports) {
101
- const resolved = await resolve(context, i.source.value, id);
123
+ if (!i.source) continue;
124
+ const resolved = (await resolve(context, i.source.value, id))?.id;
102
125
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
103
126
  let source;
104
127
  try {
@@ -158,17 +181,26 @@ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
158
181
  });
159
182
  }
160
183
  });
161
- const resolve = async (context, id, importer) => {
184
+ async function resolve(context, id, importer) {
162
185
  const nativeContext = context.getNativeBuildContext?.();
163
186
  if (nativeContext?.framework === "esbuild") {
164
- return (await nativeContext.build.resolve(id, {
187
+ const resolved$1 = await nativeContext.build.resolve(id, {
165
188
  importer,
166
189
  resolveDir: path.dirname(importer),
167
190
  kind: "import-statement"
168
- })).path;
191
+ });
192
+ return {
193
+ id: resolved$1.path,
194
+ external: resolved$1.external
195
+ };
169
196
  }
170
- return (await context.resolve(id, importer))?.id;
171
- };
197
+ const resolved = await context.resolve(id, importer);
198
+ if (!resolved) return;
199
+ return {
200
+ id: resolved.id,
201
+ external: !!resolved.external
202
+ };
203
+ }
172
204
  function stripExt(filename) {
173
205
  return filename.replace(/\.(.?)[jt]s$/, "");
174
206
  }
package/dist/vite.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- const require_src_index = require('./src_index--m5BPU5W.cjs');
2
+ const require_src_index = require('./src_index-Q6qrrUBW.cjs');
3
3
  require('./transformer-ZHFQTLaY.cjs');
4
4
 
5
5
  //#region src/vite.ts
package/dist/vite.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.vite;
3
3
  export default _default;
package/dist/vite.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { IsolatedDecl } from './index';
1
+ import { IsolatedDecl } from "./index.js";
2
2
  declare const _default: typeof IsolatedDecl.vite;
3
3
  export default _default;
package/dist/vite.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-YxBC7dLI.js";
2
- import { IsolatedDecl } from "./src_index-pbl3XXPp.js";
2
+ import { IsolatedDecl } from "./src_index-g0Uyr2cn.js";
3
3
 
4
4
  //#region src/vite.ts
5
5
  var vite_default = IsolatedDecl.vite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-isolated-decl",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "A blazing-fast tool for generating isolated declarations, powered by Oxc.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -85,6 +85,7 @@
85
85
  },
86
86
  "dependencies": {
87
87
  "@rollup/pluginutils": "^5.1.0",
88
+ "magic-string": "^0.30.11",
88
89
  "oxc-parser": "^0.27.0",
89
90
  "unplugin": "^1.14.0"
90
91
  },
@@ -93,6 +94,7 @@
93
94
  "@sxzz/eslint-config": "^4.2.0",
94
95
  "@sxzz/prettier-config": "^2.0.2",
95
96
  "@types/node": "^22.5.4",
97
+ "@typescript-eslint/typescript-estree": "^8.5.0",
96
98
  "bumpp": "^9.5.2",
97
99
  "esbuild": "^0.23.1",
98
100
  "eslint": "^9.10.0",