@kubb/core 0.11.2 → 0.14.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/dist/index.d.ts CHANGED
@@ -18,11 +18,24 @@ declare class Emitter<TValue = unknown, TTopics = unknown> {
18
18
  }
19
19
 
20
20
  interface EmittedFile {
21
+ /**
22
+ * equal to importee when getting passed through resolveId
23
+ */
21
24
  id: string;
22
- source: string | undefined;
23
- name: string | undefined;
24
- code: string | undefined;
25
- meta?: Record<string, any>;
25
+ /**
26
+ * The importer is the fully resolved id of the importing module.
27
+ */
28
+ importer?: string;
29
+ /**
30
+ * Name to be used to dynamicly create the fileName(based on input.path)
31
+ */
32
+ name?: string;
33
+ /**
34
+ * FileName will be the end result so no input.path will not be added
35
+ */
36
+ fileName?: string;
37
+ source?: string;
38
+ options?: Record<string, any>;
26
39
  }
27
40
  type EmitFile = (emittedFile: EmittedFile) => void;
28
41
  type Topics = 'new' | 'delete' | 'end';
@@ -38,17 +51,6 @@ declare class FileEmitter {
38
51
  getEmittedFile: (fileReferenceId?: string | null) => EmittedFile | undefined;
39
52
  }
40
53
 
41
- interface SerializablePluginCache {
42
- [key: string]: [number, any];
43
- }
44
- interface Cache<TCache = any> {
45
- delete(id: string): boolean;
46
- get<T = TCache>(id: string): T;
47
- has(id: string): boolean;
48
- set<T = TCache>(id: string, value: T): void;
49
- }
50
- declare function createPluginCache(cache: SerializablePluginCache): Cache;
51
-
52
54
  /**
53
55
  * Get the type of the first argument in a function.
54
56
  * @example Arg0<(a: string, b: number) => void> -> string
@@ -56,16 +58,16 @@ declare function createPluginCache(cache: SerializablePluginCache): Cache;
56
58
  type Argument0<H extends keyof PluginLifecycle> = Parameters<PluginLifecycle[H]>[0];
57
59
  declare const hooks: [keyof PluginLifecycle];
58
60
  declare class PluginDriver {
59
- private readonly pluginContexts;
60
61
  plugins: Plugin[];
61
62
  readonly fileEmitter: FileEmitter;
62
63
  private readonly logger?;
63
64
  private readonly config;
64
- private readonly sortedPlugins;
65
- constructor(config: Api['config'], pluginCache: Record<string, SerializablePluginCache> | undefined, options: {
66
- logger: BuildContext['logger'];
65
+ readonly core: Plugin<CorePluginOptions> & {
66
+ api: CorePluginOptions['api'];
67
+ };
68
+ constructor(config: Api['config'], options: {
69
+ logger: BuildOptions['logger'];
67
70
  });
68
- get api(): PluginContext;
69
71
  emitFile(...params: Parameters<FileEmitter['emitFile']>): Promise<EmittedFile>;
70
72
  resolveId: (source: string, importer: string, meta: Record<string, any> | undefined) => Promise<string | null | undefined>;
71
73
  load: (id: string) => Promise<TransformResult>;
@@ -80,7 +82,7 @@ declare class PluginDriver {
80
82
  * @param args Arguments passed to the plugin hook.
81
83
  * @param plugin The actual pluginObject to run.
82
84
  */
83
- private runHook;
85
+ private run;
84
86
  /**
85
87
  * Run a sync plugin hook and return the result.
86
88
  * @param hookName Name of the plugin hook. Must be in `PluginHooks`.
@@ -88,21 +90,20 @@ declare class PluginDriver {
88
90
  * @param plugin The acutal plugin
89
91
  * @param replaceContext When passed, the plugin context can be overridden.
90
92
  */
91
- private runHookSync;
93
+ private runSync;
92
94
  }
93
95
 
96
+ interface Cache<TCache = any> {
97
+ delete(id: string): boolean;
98
+ get<T = TCache>(id: string): T;
99
+ has(id: string): boolean;
100
+ set<T = TCache>(id: string, value: T): void;
101
+ }
102
+ declare function createPluginCache(cache: any): Cache;
103
+
94
104
  declare const format: (text: string) => string;
95
105
 
96
- declare const getRelativePath: (from?: string | null, to?: string | null) => string | {
97
- (searchValue: string | RegExp, replaceValue: string): string;
98
- (searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
99
- (searchValue: {
100
- [Symbol.replace](string: string, replaceValue: string): string;
101
- }, replaceValue: string): string;
102
- (searchValue: {
103
- [Symbol.replace](string: string, replacer: (substring: string, ...args: any[]) => string): string;
104
- }, replacer: (substring: string, ...args: any[]) => string): string;
105
- };
106
+ declare const getRelativePath: (from?: string | null, to?: string | null) => string;
106
107
 
107
108
  type PluginOptions<UserOptions = unknown, Nested extends boolean = false, Api = any> = {
108
109
  userOptions: UserOptions;
@@ -110,23 +111,27 @@ type PluginOptions<UserOptions = unknown, Nested extends boolean = false, Api =
110
111
  api: Api;
111
112
  };
112
113
  type Plugin<TOptions extends PluginOptions = PluginOptions> = {
113
- name: PluginName | Omit<string, PluginName>;
114
- cacheKey?: string;
114
+ name: string;
115
115
  api?: TOptions['api'];
116
116
  } & Partial<PluginLifecycle>;
117
117
  type PluginFactory<TOptions extends PluginOptions = PluginOptions> = (options: TOptions['userOptions']) => TOptions['nested'] extends true ? Array<Plugin<TOptions>> : Plugin<TOptions>;
118
118
  declare function createPlugin<TOptions extends PluginOptions = PluginOptions>(factory: PluginFactory<TOptions>): (userOptions: TOptions['userOptions']) => TOptions["nested"] extends true ? Plugin<TOptions>[] : Plugin<TOptions>;
119
+ type Options = {
120
+ config: Api['config'];
121
+ fileEmitter: FileEmitter;
122
+ resolveId: Api['resolveId'];
123
+ load: Api['load'];
124
+ };
125
+ type CorePluginOptions = PluginOptions<Options, false, Api>;
119
126
 
120
- interface ConfigEnv {
121
- mode: 'development';
122
- }
123
127
  interface UserConfig {
124
128
  /**
125
129
  * Project root directory. Can be an absolute path, or a path relative from
126
130
  * the location of the config file itself.
127
131
  * @default process.cwd()
128
132
  */
129
- root?: string;
133
+ root: string;
134
+ clear?: boolean;
130
135
  mode?: 'single';
131
136
  input: {
132
137
  /**
@@ -149,8 +154,9 @@ interface UserConfig {
149
154
  */
150
155
  logLevel?: LogLevel;
151
156
  }
152
- type UserConfigFn = (env: ConfigEnv) => WithPromise<UserConfig>;
157
+ type UserConfigFn = (options: BuildOptions) => WithPromise<UserConfig>;
153
158
  type UserConfigExport = WithPromise<UserConfig> | UserConfigFn;
159
+ declare const defaultConfig: Partial<UserConfig>;
154
160
  /**
155
161
  * Type helper to make it easier to use kubb.config.ts
156
162
  * accepts a direct {@link UserConfig} object, or a function that returns it.
@@ -158,11 +164,10 @@ type UserConfigExport = WithPromise<UserConfig> | UserConfigFn;
158
164
  */
159
165
  declare function defineConfig(config: UserConfigExport): UserConfigExport;
160
166
 
161
- type PluginName = string;
162
167
  type PluginLifecycle = {
163
168
  validate: (this: PluginContext, plugins: Plugin[]) => WithPromise<ValidationResult>;
164
169
  buildStart: (this: PluginContext) => WithPromise<void>;
165
- resolveId: (this: PluginContext, source: string, importer: string | undefined, meta: Record<string, any> | undefined) => string | null | undefined;
170
+ resolveId: (this: PluginContext, importee: string, importer?: string, options?: Record<string, any>) => string | null | undefined;
166
171
  load: (this: PluginContext, id: string) => WithPromise<TransformResult | null>;
167
172
  transform: (this: PluginContext, code: string, id: string) => WithPromise<TransformResult>;
168
173
  writeFile: (this: PluginContext, code: string | undefined, id: string) => WithPromise<void>;
@@ -170,25 +175,18 @@ type PluginLifecycle = {
170
175
  };
171
176
  type PluginLifecycleHooks = keyof PluginLifecycle;
172
177
  type Api = {
173
- config: UserConfig & {
174
- root: string;
175
- };
176
- input: Path;
178
+ config: UserConfig;
179
+ cache: Cache;
177
180
  emitFile: FileEmitter['emitFile'];
178
- resolveId: (source: string, importer?: string, meta?: Record<string, any>) => WithPromise<string | null | undefined>;
181
+ resolveId: (importee: string, importer?: string, options?: Record<string, any>) => WithPromise<string | null | undefined>;
179
182
  load: (id: string) => WithPromise<TransformResult | void>;
180
- getEmittedFile: FileEmitter['getEmittedFile'];
181
- on: FileEmitter['on'];
182
- };
183
- type PluginContext = Api & {
184
- cache: Cache;
183
+ addToIndex: (emittedFile: EmittedFile) => void;
185
184
  };
185
+ type PluginContext = Api;
186
186
  type ValidationResult = true | {
187
187
  message: string;
188
188
  };
189
189
  type TransformResult = string | null;
190
- type Id = string;
191
- type Path = string;
192
190
  type LogType = 'error' | 'warn' | 'info';
193
191
  type LogLevel = LogType | 'silent';
194
192
 
@@ -203,16 +201,14 @@ type Spinner = {
203
201
  text: string;
204
202
  info: (text: string) => Spinner;
205
203
  };
206
- type BuildContext = {
204
+ type BuildOptions = {
205
+ config: Api['config'];
206
+ mode: 'development' | 'production';
207
207
  logger?: {
208
208
  log: (message: string, logLevel: LogLevel) => void;
209
209
  spinner?: Spinner;
210
210
  };
211
211
  };
212
- declare function build(this: BuildContext, userConfig: UserConfig, env: ConfigEnv): Promise<BuildOutput>;
213
-
214
- declare function getPluginContext(this: PluginDriver, plugin: Plugin, pluginCache: Record<string, SerializablePluginCache> | void): {
215
- cache: Cache<any>;
216
- };
212
+ declare function build(options: BuildOptions): Promise<BuildOutput>;
217
213
 
218
- export { Api, Argument0, BuildContext, Cache, ConfigEnv, EmitFile, EmittedFile, Emitter, FileEmitter, Id, Listener, LogLevel, LogType, Path, Plugin, PluginContext, PluginDriver, PluginFactory, PluginLifecycle, PluginLifecycleHooks, PluginName, PluginOptions, SerializablePluginCache, TransformResult, UserConfig, UserConfigExport, UserConfigFn, ValidationResult, WithPromise, build, createPlugin, createPluginCache, build as default, defineConfig, format, getPluginContext, getRelativePath, hooks, isPromise, write };
214
+ export { Api, Argument0, BuildOptions, Cache, EmitFile, EmittedFile, Emitter, FileEmitter, Listener, LogLevel, LogType, Plugin, PluginContext, PluginDriver, PluginFactory, PluginLifecycle, PluginLifecycleHooks, PluginOptions, TransformResult, UserConfig, UserConfigExport, UserConfigFn, ValidationResult, WithPromise, build, createPlugin, createPluginCache, build as default, defaultConfig, defineConfig, format, getRelativePath, hooks, isPromise, write };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var path2 = require('path');
6
- var fse2 = require('fs-extra');
6
+ var fse = require('fs-extra');
7
7
  var prettier = require('prettier');
8
8
 
9
9
  // src/build.ts
@@ -58,7 +58,7 @@ var FileEmitter = class {
58
58
  this.emitter.emit("new", emitedFile);
59
59
  return new Promise((resolve) => {
60
60
  const subscribe = this.emitter.on("delete", (deletedFile) => {
61
- if (deletedFile?.id === emitedFile.id) {
61
+ if (deletedFile?.id && deletedFile?.id === emitedFile.id) {
62
62
  resolve(deletedFile);
63
63
  return subscribe();
64
64
  }
@@ -93,6 +93,38 @@ var FileEmitter = class {
93
93
  };
94
94
  };
95
95
 
96
+ // src/utils/isPromise.ts
97
+ var isPromise = (result) => {
98
+ return typeof result?.then === "function";
99
+ };
100
+ var formatOptions = {
101
+ tabWidth: 2,
102
+ printWidth: 160,
103
+ parser: "typescript",
104
+ singleQuote: true,
105
+ semi: false,
106
+ bracketSameLine: false,
107
+ endOfLine: "auto"
108
+ };
109
+ var format = (text) => {
110
+ return prettier.format(text, formatOptions);
111
+ };
112
+
113
+ // src/utils/write.ts
114
+ var write = async (data, path4, options = { format: false }) => {
115
+ const formattedData = options.format ? format(data) : data;
116
+ try {
117
+ await fse.stat(path4);
118
+ const oldContent = await fse.readFile(path4, { encoding: "utf-8" });
119
+ if (oldContent?.toString() === formattedData) {
120
+ return;
121
+ }
122
+ } catch (_err) {
123
+ return fse.outputFile(path4, formattedData);
124
+ }
125
+ return fse.outputFile(path4, formattedData);
126
+ };
127
+
96
128
  // src/utils/cache.ts
97
129
  function createPluginCache(cache) {
98
130
  return {
@@ -118,15 +150,15 @@ function createPluginCache(cache) {
118
150
  }
119
151
  };
120
152
  }
153
+ var getRelativePath = (from, to) => {
154
+ if (!from || !to) {
155
+ throw new Error("From and to should be filled in when retrieving the relativePath");
156
+ }
157
+ const newPath = path2.relative(from, to).replace("../", "").replace(".ts", "").trimEnd();
158
+ return `./${newPath}`;
159
+ };
121
160
 
122
- // src/context.ts
123
- function getPluginContext(plugin, pluginCache) {
124
- const cacheKey = plugin.cacheKey || plugin.name;
125
- const cacheInstance = createPluginCache(pluginCache[cacheKey] || (pluginCache[cacheKey] = /* @__PURE__ */ Object.create(null)));
126
- return {
127
- cache: cacheInstance
128
- };
129
- }
161
+ // src/plugin.ts
130
162
  function createPlugin(factory) {
131
163
  return (userOptions) => {
132
164
  const plugin = factory(userOptions);
@@ -144,23 +176,46 @@ function createPlugin(factory) {
144
176
  var name = "core";
145
177
  var definePlugin = createPlugin((options) => {
146
178
  const { fileEmitter, resolveId, load } = options;
147
- const schema = fse2.readFileSync(path2.resolve(options.config.root, options.config.input.path), "utf-8");
179
+ const indexFiles = [];
148
180
  const api = {
149
181
  get config() {
150
182
  return options.config;
151
183
  },
152
- get input() {
153
- return schema;
184
+ async emitFile(emittedFile) {
185
+ const resolvedId = await resolveId(emittedFile.id, emittedFile.importer, emittedFile.options);
186
+ const id = resolvedId || emittedFile.importer || emittedFile.id;
187
+ return fileEmitter.emitFile({
188
+ ...emittedFile,
189
+ id
190
+ });
191
+ },
192
+ addToIndex: (emittedFile) => {
193
+ indexFiles.push(emittedFile);
154
194
  },
155
- on: fileEmitter.on.bind(fileEmitter),
156
- getEmittedFile: fileEmitter.getEmittedFile.bind(fileEmitter),
157
- emitFile: fileEmitter.emitFile.bind(fileEmitter),
158
195
  resolveId,
159
- load
196
+ load,
197
+ cache: createPluginCache(/* @__PURE__ */ Object.create(null))
160
198
  };
161
199
  return {
162
200
  name,
163
- api
201
+ api,
202
+ async buildEnd() {
203
+ if (!indexFiles.length) {
204
+ return;
205
+ }
206
+ let index = ``;
207
+ indexFiles.forEach((item) => {
208
+ index += `export * from "${getRelativePath(path2.resolve(this.config.root, this.config.output.path), item.id)}";
209
+ `;
210
+ });
211
+ await write(index, path2.resolve(this.config.root, this.config.output.path, "index.ts"), { format: true });
212
+ },
213
+ resolveId(importee, importer) {
214
+ if (!importer) {
215
+ return null;
216
+ }
217
+ return path2.resolve(importer, importee);
218
+ }
164
219
  };
165
220
  });
166
221
 
@@ -176,36 +231,17 @@ var hookNames = {
176
231
  };
177
232
  var hooks = Object.keys(hookNames);
178
233
  var PluginDriver = class {
179
- pluginContexts;
180
234
  plugins;
181
- fileEmitter;
235
+ fileEmitter = new FileEmitter();
182
236
  logger;
183
237
  config;
184
- sortedPlugins = /* @__PURE__ */ new Map();
185
- constructor(config, pluginCache, options) {
238
+ core;
239
+ constructor(config, options) {
186
240
  this.logger = options.logger;
187
241
  this.config = config;
188
- this.fileEmitter = new FileEmitter();
189
242
  this.emitFile = this.fileEmitter.emitFile.bind(this.fileEmitter);
190
- const corePlugin = definePlugin({ config, fileEmitter: this.fileEmitter, load: this.load, resolveId: this.resolveId });
191
- this.plugins = [corePlugin, ...config.plugins || []];
192
- const coreApi = corePlugin.api;
193
- this.pluginContexts = new Map(
194
- this.plugins.map((plugin) => {
195
- const context = {
196
- ...coreApi,
197
- ...getPluginContext.call(this, plugin, pluginCache)
198
- };
199
- return [plugin, context];
200
- })
201
- );
202
- }
203
- get api() {
204
- let context = {};
205
- this.pluginContexts.forEach((value) => {
206
- context = { ...context, ...value };
207
- });
208
- return context;
243
+ this.core = definePlugin({ config, fileEmitter: this.fileEmitter, load: this.load, resolveId: this.resolveId });
244
+ this.plugins = [this.core, ...config.plugins || []];
209
245
  }
210
246
  emitFile(...params) {
211
247
  return this.fileEmitter.emitFile(...params);
@@ -225,7 +261,7 @@ var PluginDriver = class {
225
261
  promise = promise.then((result) => {
226
262
  if (result != null)
227
263
  return result;
228
- return this.runHook("hookFirst", hookName, parameters, plugin);
264
+ return this.run("hookFirst", hookName, parameters, plugin);
229
265
  });
230
266
  }
231
267
  return promise;
@@ -236,9 +272,9 @@ var PluginDriver = class {
236
272
  if (plugin[hookName]?.sequential) {
237
273
  await Promise.all(parallelPromises);
238
274
  parallelPromises.length = 0;
239
- await this.runHook("hookParallel", hookName, parameters, plugin);
275
+ await this.run("hookParallel", hookName, parameters, plugin);
240
276
  } else {
241
- const promise = this.runHook("hookParallel", hookName, parameters, plugin);
277
+ const promise = this.run("hookParallel", hookName, parameters, plugin);
242
278
  parallelPromises.push(promise);
243
279
  }
244
280
  }
@@ -248,8 +284,8 @@ var PluginDriver = class {
248
284
  let promise = Promise.resolve(argument0);
249
285
  for (const plugin of this.getSortedPlugins(hookName)) {
250
286
  promise = promise.then(
251
- (argument02) => this.runHook("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
252
- (result) => reduce.call(this.api, argument02, result, plugin)
287
+ (argument02) => this.run("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
288
+ (result) => reduce.call(this.core.api, argument02, result, plugin)
253
289
  )
254
290
  );
255
291
  }
@@ -258,14 +294,14 @@ var PluginDriver = class {
258
294
  hookSeq(hookName, parameters) {
259
295
  let promise = Promise.resolve();
260
296
  for (const plugin of this.getSortedPlugins(hookName)) {
261
- promise = promise.then(() => this.runHook("hookSeq", hookName, parameters, plugin));
297
+ promise = promise.then(() => this.run("hookSeq", hookName, parameters, plugin));
262
298
  }
263
299
  return promise.then(noReturn);
264
300
  }
265
301
  getSortedPlugins(_hookName) {
266
302
  return [...this.plugins];
267
303
  }
268
- runHook(strategy, hookName, parameters, plugin) {
304
+ run(strategy, hookName, parameters, plugin) {
269
305
  const hook = plugin[hookName];
270
306
  return Promise.resolve().then(() => {
271
307
  if (typeof hook !== "function") {
@@ -275,7 +311,7 @@ var PluginDriver = class {
275
311
  this.logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
276
312
  `;
277
313
  }
278
- const hookResult = hook.apply(this.api, parameters);
314
+ const hookResult = hook.apply(this.core.api, parameters);
279
315
  if (!hookResult?.then) {
280
316
  if (this.config.logLevel === "info" && this.logger?.spinner) {
281
317
  this.logger.spinner.succeed(`[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
@@ -292,10 +328,10 @@ var PluginDriver = class {
292
328
  });
293
329
  });
294
330
  }
295
- runHookSync(hookName, parameters, plugin) {
331
+ runSync(hookName, parameters, plugin) {
296
332
  const hook = plugin[hookName];
297
333
  try {
298
- return hook.apply(this.api, parameters);
334
+ return hook.apply(this.core.api, parameters);
299
335
  } catch (error) {
300
336
  return error;
301
337
  }
@@ -304,6 +340,14 @@ var PluginDriver = class {
304
340
  function noReturn() {
305
341
  }
306
342
 
343
+ // src/config.ts
344
+ var defaultConfig = {
345
+ root: process.cwd()
346
+ };
347
+ function defineConfig(config) {
348
+ return config;
349
+ }
350
+
307
351
  // src/build.ts
308
352
  async function transformReducer(previousCode, result, _plugin) {
309
353
  if (result === null) {
@@ -312,112 +356,68 @@ async function transformReducer(previousCode, result, _plugin) {
312
356
  return result;
313
357
  }
314
358
  async function buildImplementation(options, done) {
315
- const { config } = options;
316
- const pluginCache = /* @__PURE__ */ Object.create(null);
317
- const pluginDriver = new PluginDriver(config, pluginCache, { logger: this.logger });
359
+ const { config, logger } = options;
360
+ if (config.clear) {
361
+ await fse.remove(config.output.path);
362
+ }
363
+ const pluginDriver = new PluginDriver(
364
+ {
365
+ ...defaultConfig,
366
+ ...config
367
+ },
368
+ { logger }
369
+ );
370
+ const input = fse.readFileSync(path2.resolve(config.root, config.input.path), "utf-8");
318
371
  const validations = await pluginDriver.hookParallel("validate", [pluginDriver.plugins]);
319
372
  const validationsWithMessage = validations.filter(Boolean);
320
373
  if (validationsWithMessage.some((validation) => typeof validation !== "boolean")) {
321
374
  validationsWithMessage.forEach((validation) => {
322
375
  if (validation && typeof validation !== "boolean" && validation?.message) {
323
- this.logger?.log(validation.message, "warn");
376
+ logger?.log(validation.message, "warn");
324
377
  }
325
378
  });
326
379
  return;
327
380
  }
328
381
  pluginDriver.fileEmitter.on("new", async (emittedFile) => {
329
- if (!emittedFile?.source) {
382
+ if (!emittedFile) {
330
383
  return;
331
384
  }
332
- const resolvedId = await pluginDriver.hookFirst("resolveId", [emittedFile.source, path2.resolve(emittedFile.source), emittedFile.meta]);
333
- const id = resolvedId || emittedFile.source;
334
- let { code } = emittedFile;
385
+ const { id } = emittedFile;
386
+ if (!id) {
387
+ throw new Error("No id could be transformed, please add id to emitFile or use resolveId");
388
+ }
389
+ let { source: code } = emittedFile;
335
390
  const loadedResult = await pluginDriver.hookFirst("load", [id]);
336
391
  if (loadedResult) {
337
392
  code = loadedResult;
338
393
  }
339
394
  if (code) {
340
- const result = await pluginDriver.hookReduceArg0("transform", [code, id], transformReducer);
341
- await pluginDriver.hookParallel("writeFile", [result, id]);
395
+ const transformedCode = await pluginDriver.hookReduceArg0("transform", [code, id], transformReducer);
396
+ await pluginDriver.hookParallel("writeFile", [transformedCode, id]);
342
397
  pluginDriver.fileEmitter.delete(emittedFile.id);
343
398
  }
344
399
  });
345
400
  await pluginDriver.hookParallel("buildStart");
346
401
  pluginDriver.emitFile({
347
- id: path2.resolve(pluginDriver.api.config.root, pluginDriver.api.config.input.path),
348
- source: pluginDriver.api.config.input.path,
402
+ id: config.input.path,
349
403
  name: void 0,
350
- code: pluginDriver.api.input
404
+ source: input
351
405
  });
352
406
  pluginDriver.fileEmitter.on("end", async () => {
353
407
  await pluginDriver.hookParallel("buildEnd");
354
408
  done();
355
409
  });
356
410
  }
357
- function build(userConfig, env) {
358
- const config = {
359
- ...userConfig,
360
- root: userConfig.root || process.cwd()
361
- };
362
- return new Promise((resolve) => {
363
- buildImplementation.call(
364
- this,
365
- {
366
- config,
367
- env
368
- },
369
- resolve
370
- );
411
+ function build(options) {
412
+ return new Promise((resolve, reject) => {
413
+ try {
414
+ buildImplementation(options, resolve);
415
+ } catch (e) {
416
+ reject(e);
417
+ }
371
418
  });
372
419
  }
373
420
 
374
- // src/config.ts
375
- function defineConfig(config) {
376
- return config;
377
- }
378
-
379
- // src/utils/isPromise.ts
380
- var isPromise = (result) => {
381
- return typeof result?.then === "function";
382
- };
383
- var formatOptions = {
384
- tabWidth: 2,
385
- printWidth: 160,
386
- parser: "typescript",
387
- singleQuote: true,
388
- semi: false,
389
- bracketSameLine: false,
390
- endOfLine: "auto"
391
- };
392
- var format = (text) => {
393
- return prettier.format(text, formatOptions);
394
- };
395
-
396
- // src/utils/write.ts
397
- var write = async (data, path4, options = { format: false }) => {
398
- const formattedData = options.format ? format(data) : data;
399
- try {
400
- await fse2.stat(path4);
401
- const oldContent = await fse2.readFile(path4, { encoding: "utf-8" });
402
- if (oldContent?.toString() === formattedData) {
403
- return;
404
- }
405
- } catch (_err) {
406
- return fse2.outputFile(path4, formattedData);
407
- }
408
- return fse2.outputFile(path4, formattedData);
409
- };
410
- var getRelativePath = (from, to) => {
411
- if (!from || !to) {
412
- throw new Error("From and to should be filled in when retrieving the relativePath");
413
- }
414
- const newPath = path2.relative(from, to).replace("../", "").replace(".ts", "").trimEnd();
415
- if (newPath.startsWith("./") || newPath.startsWith("../")) {
416
- return newPath.replace;
417
- }
418
- return `./${newPath}`;
419
- };
420
-
421
421
  // src/index.ts
422
422
  var src_default = build;
423
423
 
@@ -428,9 +428,9 @@ exports.build = build;
428
428
  exports.createPlugin = createPlugin;
429
429
  exports.createPluginCache = createPluginCache;
430
430
  exports.default = src_default;
431
+ exports.defaultConfig = defaultConfig;
431
432
  exports.defineConfig = defineConfig;
432
433
  exports.format = format;
433
- exports.getPluginContext = getPluginContext;
434
434
  exports.getRelativePath = getRelativePath;
435
435
  exports.hooks = hooks;
436
436
  exports.isPromise = isPromise;