unplugin-isolated-decl 0.5.0 → 0.5.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
@@ -90,6 +90,9 @@ export interface Options {
90
90
  /** Only for typescript transformer */
91
91
  transformOptions?: TranspileOptions
92
92
  ignoreErrors?: boolean
93
+
94
+ /** An extra directory layer for output files. */
95
+ extraOutdir?: string
93
96
  }
94
97
  ```
95
98
 
@@ -5,11 +5,12 @@ export type Options = {
5
5
  exclude?: FilterPattern;
6
6
  enforce?: "pre" | "post" | undefined;
7
7
  ignoreErrors?: boolean;
8
+ extraOutdir?: string;
8
9
  } & ({transformer?: "oxc" | "swc"} | {
9
10
  transformer: "typescript";
10
11
  transformOptions?: TranspileOptions;
11
12
  });
12
13
  type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
13
- export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce">>;
14
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce" | "extraOutdir">>;
14
15
  export declare function resolveOptions(options: Options): OptionsResolved;
15
16
  export {};
@@ -5,11 +5,12 @@ export type Options = {
5
5
  exclude?: FilterPattern;
6
6
  enforce?: "pre" | "post" | undefined;
7
7
  ignoreErrors?: boolean;
8
+ extraOutdir?: string;
8
9
  } & ({transformer?: "oxc" | "swc"} | {
9
10
  transformer: "typescript";
10
11
  transformOptions?: TranspileOptions;
11
12
  });
12
13
  type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
13
- export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce">>;
14
+ export type OptionsResolved = Overwrite<Required<Options>, Pick<Options, "enforce" | "extraOutdir">>;
14
15
  export declare function resolveOptions(options: Options): OptionsResolved;
15
16
  export {};
package/dist/esbuild.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  require('./transformer-ZZscAG9G.cjs');
3
- const require_src_index = require('./src_index-2j2jy32a.cjs');
3
+ const require_src_index = require('./src_index-xmclay4j.cjs');
4
4
 
5
5
  //#region src/esbuild.ts
6
6
  var esbuild_default = require_src_index.IsolatedDecl.esbuild;
package/dist/esbuild.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-2hVUBpkW.js";
2
- import { IsolatedDecl } from "./src_index-GOBsoA4f.js";
2
+ import { IsolatedDecl } from "./src_index-0hJHBHCG.js";
3
3
 
4
4
  //#region src/esbuild.ts
5
5
  var esbuild_default = IsolatedDecl.esbuild;
package/dist/index.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  require('./transformer-ZZscAG9G.cjs');
3
- const require_src_index = require('./src_index-2j2jy32a.cjs');
3
+ const require_src_index = require('./src_index-xmclay4j.cjs');
4
4
 
5
5
  Object.defineProperty(exports, 'IsolatedDecl', {
6
6
  enumerable: true,
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import "./transformer-2hVUBpkW.js";
2
- import { IsolatedDecl, lowestCommonAncestor } from "./src_index-GOBsoA4f.js";
2
+ import { IsolatedDecl, lowestCommonAncestor } from "./src_index-0hJHBHCG.js";
3
3
 
4
4
  export { IsolatedDecl, lowestCommonAncestor };
package/dist/rolldown.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  require('./transformer-ZZscAG9G.cjs');
3
- const require_src_index = require('./src_index-2j2jy32a.cjs');
3
+ const require_src_index = require('./src_index-xmclay4j.cjs');
4
4
 
5
5
  //#region src/rolldown.ts
6
6
  var rolldown_default = require_src_index.IsolatedDecl.rolldown;
package/dist/rolldown.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-2hVUBpkW.js";
2
- import { IsolatedDecl } from "./src_index-GOBsoA4f.js";
2
+ import { IsolatedDecl } from "./src_index-0hJHBHCG.js";
3
3
 
4
4
  //#region src/rolldown.ts
5
5
  var rolldown_default = IsolatedDecl.rolldown;
package/dist/rollup.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  require('./transformer-ZZscAG9G.cjs');
3
- const require_src_index = require('./src_index-2j2jy32a.cjs');
3
+ const require_src_index = require('./src_index-xmclay4j.cjs');
4
4
 
5
5
  //#region src/rollup.ts
6
6
  var rollup_default = require_src_index.IsolatedDecl.rollup;
package/dist/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-2hVUBpkW.js";
2
- import { IsolatedDecl } from "./src_index-GOBsoA4f.js";
2
+ import { IsolatedDecl } from "./src_index-0hJHBHCG.js";
3
3
 
4
4
  //#region src/rollup.ts
5
5
  var rollup_default = IsolatedDecl.rollup;
@@ -12,13 +12,14 @@ function resolveOptions(options) {
12
12
  exclude: options.exclude || [/node_modules/],
13
13
  enforce: "enforce" in options ? options.enforce : "pre",
14
14
  transformer: options.transformer || "oxc",
15
- ignoreErrors: options.ignoreErrors || false
15
+ ignoreErrors: options.ignoreErrors || false,
16
+ extraOutdir: options.extraOutdir
16
17
  };
17
18
  }
18
19
 
19
20
  //#endregion
20
21
  //#region src/index.ts
21
- const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
22
+ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
22
23
  const options = resolveOptions(rawOptions);
23
24
  const filter = createFilter(options.include, options.exclude);
24
25
  const outputFiles = {};
@@ -35,7 +36,10 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
35
36
  if (typeof outputOptions.entryFileNames !== "string") {
36
37
  return this.error("entryFileNames must be a string");
37
38
  }
38
- const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
39
+ let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
40
+ if (options.extraOutdir) {
41
+ entryFileNames = path.join(options.extraOutdir, entryFileNames);
42
+ }
39
43
  for (const [filename, source] of Object.entries(outputFiles)) {
40
44
  this.emitFile({
41
45
  type: "asset",
@@ -50,52 +54,9 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
50
54
  return filter(id);
51
55
  },
52
56
  transform(code, id) {
53
- return transform.call(this, code, id);
57
+ return transform(this, code, id);
54
58
  },
55
- esbuild: { setup(build) {
56
- build.onEnd(async (result) => {
57
- const esbuildOptions = build.initialOptions;
58
- const entries = esbuildOptions.entryPoints;
59
- if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
60
- const outBase = lowestCommonAncestor(...entries);
61
- const jsExt = esbuildOptions.outExtension?.[".js"];
62
- let outExt;
63
- switch (jsExt) {
64
- case ".cjs":
65
- outExt = "cts";
66
- break;
67
- case ".mjs":
68
- outExt = "mts";
69
- break;
70
- default:
71
- outExt = "ts";
72
- break;
73
- }
74
- const write = build.initialOptions.write ?? true;
75
- if (write) {
76
- if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
77
- } else {
78
- result.outputFiles ||= [];
79
- }
80
- const textEncoder = new TextEncoder();
81
- for (const [filename, source] of Object.entries(outputFiles)) {
82
- const outDir = build.initialOptions.outdir;
83
- const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
84
- const filePath = outDir ? path.resolve(outDir, outFile) : outFile;
85
- if (write) {
86
- await mkdir(path.dirname(filePath), { recursive: true });
87
- await writeFile(filePath, source);
88
- } else {
89
- result.outputFiles.push({
90
- path: filePath,
91
- contents: textEncoder.encode(source),
92
- hash: "",
93
- text: source
94
- });
95
- }
96
- }
97
- });
98
- } },
59
+ esbuild: { setup: esbuildSetup },
99
60
  rollup,
100
61
  rolldown: rollup,
101
62
  vite: {
@@ -104,7 +65,7 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
104
65
  ...rollup
105
66
  }
106
67
  };
107
- async function transform(code, id) {
68
+ async function transform(context, code, id) {
108
69
  let result;
109
70
  switch (options.transformer) {
110
71
  case "oxc":
@@ -118,9 +79,9 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
118
79
  const { code: sourceText, errors } = result;
119
80
  if (errors.length) {
120
81
  if (options.ignoreErrors) {
121
- this.warn(errors[0]);
82
+ context.warn(errors[0]);
122
83
  } else {
123
- this.error(errors[0]);
84
+ context.error(errors[0]);
124
85
  return;
125
86
  }
126
87
  }
@@ -136,18 +97,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
136
97
  if (node.importKind === "type") return true;
137
98
  return (node.specifiers || []).every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
138
99
  });
139
- const resolve = async (id$1, importer) => {
140
- if (meta.framework === "esbuild") {
141
- return (await meta.build.resolve(id$1, {
142
- importer,
143
- resolveDir: path.dirname(importer),
144
- kind: "import-statement"
145
- })).path;
146
- }
147
- return (await this.resolve(id$1, importer))?.id;
148
- };
149
100
  for (const i of typeImports) {
150
- const resolved = await resolve(i.source.value, id);
101
+ const resolved = await resolve(context, i.source.value, id);
151
102
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
152
103
  let source;
153
104
  try {
@@ -155,11 +106,69 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
155
106
  } catch {
156
107
  continue;
157
108
  }
158
- await transform.call(this, source, resolved);
109
+ await transform(context, source, resolved);
159
110
  }
160
111
  }
161
112
  }
113
+ function esbuildSetup(build) {
114
+ build.onEnd(async (result) => {
115
+ const esbuildOptions = build.initialOptions;
116
+ const entries = esbuildOptions.entryPoints;
117
+ if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
118
+ const outBase = lowestCommonAncestor(...entries);
119
+ const jsExt = esbuildOptions.outExtension?.[".js"];
120
+ let outExt;
121
+ switch (jsExt) {
122
+ case ".cjs":
123
+ outExt = "cts";
124
+ break;
125
+ case ".mjs":
126
+ outExt = "mts";
127
+ break;
128
+ default:
129
+ outExt = "ts";
130
+ break;
131
+ }
132
+ const write = build.initialOptions.write ?? true;
133
+ if (write) {
134
+ if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
135
+ } else {
136
+ result.outputFiles ||= [];
137
+ }
138
+ const textEncoder = new TextEncoder();
139
+ for (const [filename, source] of Object.entries(outputFiles)) {
140
+ const outDir = build.initialOptions.outdir;
141
+ let outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
142
+ if (options.extraOutdir) {
143
+ outFile = path.join(options.extraOutdir, outFile);
144
+ }
145
+ const filePath = outDir ? path.resolve(outDir, outFile) : outFile;
146
+ if (write) {
147
+ await mkdir(path.dirname(filePath), { recursive: true });
148
+ await writeFile(filePath, source);
149
+ } else {
150
+ result.outputFiles.push({
151
+ path: filePath,
152
+ contents: textEncoder.encode(source),
153
+ hash: "",
154
+ text: source
155
+ });
156
+ }
157
+ }
158
+ });
159
+ }
162
160
  });
161
+ const resolve = async (context, id, importer) => {
162
+ const nativeContext = context.getNativeBuildContext?.();
163
+ if (nativeContext?.framework === "esbuild") {
164
+ return (await nativeContext.build.resolve(id, {
165
+ importer,
166
+ resolveDir: path.dirname(importer),
167
+ kind: "import-statement"
168
+ })).path;
169
+ }
170
+ return (await context.resolve(id, importer))?.id;
171
+ };
163
172
  function stripExt(filename) {
164
173
  return filename.replace(/\.(.?)[jt]s$/, "");
165
174
  }
@@ -13,13 +13,14 @@ function resolveOptions(options) {
13
13
  exclude: options.exclude || [/node_modules/],
14
14
  enforce: "enforce" in options ? options.enforce : "pre",
15
15
  transformer: options.transformer || "oxc",
16
- ignoreErrors: options.ignoreErrors || false
16
+ ignoreErrors: options.ignoreErrors || false,
17
+ extraOutdir: options.extraOutdir
17
18
  };
18
19
  }
19
20
 
20
21
  //#endregion
21
22
  //#region src/index.ts
22
- const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
23
+ const IsolatedDecl = createUnplugin((rawOptions = {}) => {
23
24
  const options = resolveOptions(rawOptions);
24
25
  const filter = createFilter(options.include, options.exclude);
25
26
  const outputFiles = {};
@@ -36,7 +37,10 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
36
37
  if (typeof outputOptions.entryFileNames !== "string") {
37
38
  return this.error("entryFileNames must be a string");
38
39
  }
39
- const entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
40
+ let entryFileNames = outputOptions.entryFileNames.replace(/\.(.)?[jt]s$/, (_, s) => `.d.${s || ""}ts`);
41
+ if (options.extraOutdir) {
42
+ entryFileNames = path.join(options.extraOutdir, entryFileNames);
43
+ }
40
44
  for (const [filename, source] of Object.entries(outputFiles)) {
41
45
  this.emitFile({
42
46
  type: "asset",
@@ -51,52 +55,9 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
51
55
  return filter(id);
52
56
  },
53
57
  transform(code, id) {
54
- return transform.call(this, code, id);
58
+ return transform(this, code, id);
55
59
  },
56
- esbuild: { setup(build) {
57
- build.onEnd(async (result) => {
58
- const esbuildOptions = build.initialOptions;
59
- const entries = esbuildOptions.entryPoints;
60
- if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
61
- const outBase = lowestCommonAncestor(...entries);
62
- const jsExt = esbuildOptions.outExtension?.[".js"];
63
- let outExt;
64
- switch (jsExt) {
65
- case ".cjs":
66
- outExt = "cts";
67
- break;
68
- case ".mjs":
69
- outExt = "mts";
70
- break;
71
- default:
72
- outExt = "ts";
73
- break;
74
- }
75
- const write = build.initialOptions.write ?? true;
76
- if (write) {
77
- if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
78
- } else {
79
- result.outputFiles ||= [];
80
- }
81
- const textEncoder = new TextEncoder();
82
- for (const [filename, source] of Object.entries(outputFiles)) {
83
- const outDir = build.initialOptions.outdir;
84
- const outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
85
- const filePath = outDir ? path.resolve(outDir, outFile) : outFile;
86
- if (write) {
87
- await mkdir(path.dirname(filePath), { recursive: true });
88
- await writeFile(filePath, source);
89
- } else {
90
- result.outputFiles.push({
91
- path: filePath,
92
- contents: textEncoder.encode(source),
93
- hash: "",
94
- text: source
95
- });
96
- }
97
- }
98
- });
99
- } },
60
+ esbuild: { setup: esbuildSetup },
100
61
  rollup,
101
62
  rolldown: rollup,
102
63
  vite: {
@@ -105,7 +66,7 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
105
66
  ...rollup
106
67
  }
107
68
  };
108
- async function transform(code, id) {
69
+ async function transform(context, code, id) {
109
70
  let result;
110
71
  switch (options.transformer) {
111
72
  case "oxc":
@@ -119,9 +80,9 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
119
80
  const { code: sourceText, errors } = result;
120
81
  if (errors.length) {
121
82
  if (options.ignoreErrors) {
122
- this.warn(errors[0]);
83
+ context.warn(errors[0]);
123
84
  } else {
124
- this.error(errors[0]);
85
+ context.error(errors[0]);
125
86
  return;
126
87
  }
127
88
  }
@@ -137,18 +98,8 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
137
98
  if (node.importKind === "type") return true;
138
99
  return (node.specifiers || []).every((spec) => spec.type === "ImportSpecifier" && spec.importKind === "type");
139
100
  });
140
- const resolve = async (id$1, importer) => {
141
- if (meta.framework === "esbuild") {
142
- return (await meta.build.resolve(id$1, {
143
- importer,
144
- resolveDir: path.dirname(importer),
145
- kind: "import-statement"
146
- })).path;
147
- }
148
- return (await this.resolve(id$1, importer))?.id;
149
- };
150
101
  for (const i of typeImports) {
151
- const resolved = await resolve(i.source.value, id);
102
+ const resolved = await resolve(context, i.source.value, id);
152
103
  if (resolved && filter(resolved) && !outputFiles[stripExt(resolved)]) {
153
104
  let source;
154
105
  try {
@@ -156,11 +107,69 @@ const IsolatedDecl = createUnplugin((rawOptions = {}, meta) => {
156
107
  } catch {
157
108
  continue;
158
109
  }
159
- await transform.call(this, source, resolved);
110
+ await transform(context, source, resolved);
160
111
  }
161
112
  }
162
113
  }
114
+ function esbuildSetup(build) {
115
+ build.onEnd(async (result) => {
116
+ const esbuildOptions = build.initialOptions;
117
+ const entries = esbuildOptions.entryPoints;
118
+ if (!(entries && Array.isArray(entries) && entries.every((entry) => typeof entry === "string"))) throw new Error("unsupported entryPoints, must be an string[]");
119
+ const outBase = lowestCommonAncestor(...entries);
120
+ const jsExt = esbuildOptions.outExtension?.[".js"];
121
+ let outExt;
122
+ switch (jsExt) {
123
+ case ".cjs":
124
+ outExt = "cts";
125
+ break;
126
+ case ".mjs":
127
+ outExt = "mts";
128
+ break;
129
+ default:
130
+ outExt = "ts";
131
+ break;
132
+ }
133
+ const write = build.initialOptions.write ?? true;
134
+ if (write) {
135
+ if (!build.initialOptions.outdir) throw new Error("outdir is required when write is true");
136
+ } else {
137
+ result.outputFiles ||= [];
138
+ }
139
+ const textEncoder = new TextEncoder();
140
+ for (const [filename, source] of Object.entries(outputFiles)) {
141
+ const outDir = build.initialOptions.outdir;
142
+ let outFile = `${path.relative(outBase, filename)}.d.${outExt}`;
143
+ if (options.extraOutdir) {
144
+ outFile = path.join(options.extraOutdir, outFile);
145
+ }
146
+ const filePath = outDir ? path.resolve(outDir, outFile) : outFile;
147
+ if (write) {
148
+ await mkdir(path.dirname(filePath), { recursive: true });
149
+ await writeFile(filePath, source);
150
+ } else {
151
+ result.outputFiles.push({
152
+ path: filePath,
153
+ contents: textEncoder.encode(source),
154
+ hash: "",
155
+ text: source
156
+ });
157
+ }
158
+ }
159
+ });
160
+ }
163
161
  });
162
+ const resolve = async (context, id, importer) => {
163
+ const nativeContext = context.getNativeBuildContext?.();
164
+ if (nativeContext?.framework === "esbuild") {
165
+ return (await nativeContext.build.resolve(id, {
166
+ importer,
167
+ resolveDir: path.dirname(importer),
168
+ kind: "import-statement"
169
+ })).path;
170
+ }
171
+ return (await context.resolve(id, importer))?.id;
172
+ };
164
173
  function stripExt(filename) {
165
174
  return filename.replace(/\.(.?)[jt]s$/, "");
166
175
  }
package/dist/vite.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  require('./transformer-ZZscAG9G.cjs');
3
- const require_src_index = require('./src_index-2j2jy32a.cjs');
3
+ const require_src_index = require('./src_index-xmclay4j.cjs');
4
4
 
5
5
  //#region src/vite.ts
6
6
  var vite_default = require_src_index.IsolatedDecl.vite;
package/dist/vite.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import "./transformer-2hVUBpkW.js";
2
- import { IsolatedDecl } from "./src_index-GOBsoA4f.js";
2
+ import { IsolatedDecl } from "./src_index-0hJHBHCG.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.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "A blazing-fast tool for generating isolated declarations, powered by Oxc.",
5
5
  "type": "module",
6
6
  "keywords": [