@kubb/core 0.11.1 → 0.13.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 +65 -72
- package/dist/index.js +110 -134
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +110 -134
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/schemas/config.json +1 -13
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
|
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
|
|
93
|
+
private runSync;
|
|
94
|
+
}
|
|
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;
|
|
92
101
|
}
|
|
102
|
+
declare function createPluginCache(cache: any): Cache;
|
|
93
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,48 +111,51 @@ 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:
|
|
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: string;
|
|
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
|
|
133
|
+
root: string;
|
|
130
134
|
mode?: 'single';
|
|
131
|
-
/**
|
|
132
|
-
* plugins means it will run context, buildStart, transform and buildEnd based on order of the plugins array
|
|
133
|
-
* Example of an order(plugin x does not have a buildEnd): [buildStart of plugin x, buildStart of plugin y, transform of plugin x, transform of plugin y, buildEnd of plugin y ]
|
|
134
|
-
* Default: plugins
|
|
135
|
-
*/
|
|
136
|
-
lifeCycle?: 'plugins';
|
|
137
|
-
/**
|
|
138
|
-
* Default: fifo
|
|
139
|
-
*/
|
|
140
|
-
strategy?: 'fifo' | 'lifo';
|
|
141
135
|
input: {
|
|
142
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Path or link to the input file
|
|
138
|
+
*/
|
|
139
|
+
path: string;
|
|
143
140
|
};
|
|
144
141
|
output: {
|
|
142
|
+
/**
|
|
143
|
+
* Path to export folder
|
|
144
|
+
*/
|
|
145
145
|
path: string;
|
|
146
146
|
};
|
|
147
147
|
/**
|
|
148
|
-
* Array of
|
|
148
|
+
* Array of Kubb plugins to use.
|
|
149
149
|
*/
|
|
150
150
|
plugins?: Plugin[];
|
|
151
|
+
/**
|
|
152
|
+
* Log level to report when using the CLI
|
|
153
|
+
*/
|
|
151
154
|
logLevel?: LogLevel;
|
|
152
155
|
}
|
|
153
|
-
type UserConfigFn = (
|
|
156
|
+
type UserConfigFn = (options: BuildOptions) => WithPromise<UserConfig>;
|
|
154
157
|
type UserConfigExport = WithPromise<UserConfig> | UserConfigFn;
|
|
158
|
+
declare const defaultConfig: Partial<UserConfig>;
|
|
155
159
|
/**
|
|
156
160
|
* Type helper to make it easier to use kubb.config.ts
|
|
157
161
|
* accepts a direct {@link UserConfig} object, or a function that returns it.
|
|
@@ -159,11 +163,10 @@ type UserConfigExport = WithPromise<UserConfig> | UserConfigFn;
|
|
|
159
163
|
*/
|
|
160
164
|
declare function defineConfig(config: UserConfigExport): UserConfigExport;
|
|
161
165
|
|
|
162
|
-
type PluginName = string;
|
|
163
166
|
type PluginLifecycle = {
|
|
164
167
|
validate: (this: PluginContext, plugins: Plugin[]) => WithPromise<ValidationResult>;
|
|
165
168
|
buildStart: (this: PluginContext) => WithPromise<void>;
|
|
166
|
-
resolveId: (this: PluginContext,
|
|
169
|
+
resolveId: (this: PluginContext, importee: string, importer?: string, options?: Record<string, any>) => string | null | undefined;
|
|
167
170
|
load: (this: PluginContext, id: string) => WithPromise<TransformResult | null>;
|
|
168
171
|
transform: (this: PluginContext, code: string, id: string) => WithPromise<TransformResult>;
|
|
169
172
|
writeFile: (this: PluginContext, code: string | undefined, id: string) => WithPromise<void>;
|
|
@@ -171,25 +174,17 @@ type PluginLifecycle = {
|
|
|
171
174
|
};
|
|
172
175
|
type PluginLifecycleHooks = keyof PluginLifecycle;
|
|
173
176
|
type Api = {
|
|
174
|
-
config: UserConfig
|
|
175
|
-
|
|
176
|
-
};
|
|
177
|
-
schema: Schema;
|
|
177
|
+
config: UserConfig;
|
|
178
|
+
cache: Cache;
|
|
178
179
|
emitFile: FileEmitter['emitFile'];
|
|
179
|
-
resolveId: (
|
|
180
|
+
resolveId: (importee: string, importer?: string, options?: Record<string, any>) => WithPromise<string | null | undefined>;
|
|
180
181
|
load: (id: string) => WithPromise<TransformResult | void>;
|
|
181
|
-
getEmittedFile: FileEmitter['getEmittedFile'];
|
|
182
|
-
on: FileEmitter['on'];
|
|
183
|
-
};
|
|
184
|
-
type PluginContext = Api & {
|
|
185
|
-
cache: Cache;
|
|
186
182
|
};
|
|
183
|
+
type PluginContext = Api;
|
|
187
184
|
type ValidationResult = true | {
|
|
188
185
|
message: string;
|
|
189
186
|
};
|
|
190
187
|
type TransformResult = string | null;
|
|
191
|
-
type Id = string;
|
|
192
|
-
type Schema = string;
|
|
193
188
|
type LogType = 'error' | 'warn' | 'info';
|
|
194
189
|
type LogLevel = LogType | 'silent';
|
|
195
190
|
|
|
@@ -204,16 +199,14 @@ type Spinner = {
|
|
|
204
199
|
text: string;
|
|
205
200
|
info: (text: string) => Spinner;
|
|
206
201
|
};
|
|
207
|
-
type
|
|
202
|
+
type BuildOptions = {
|
|
203
|
+
config: Api['config'];
|
|
204
|
+
mode: 'development' | 'production';
|
|
208
205
|
logger?: {
|
|
209
206
|
log: (message: string, logLevel: LogLevel) => void;
|
|
210
207
|
spinner?: Spinner;
|
|
211
208
|
};
|
|
212
209
|
};
|
|
213
|
-
declare function build(
|
|
214
|
-
|
|
215
|
-
declare function getPluginContext(this: PluginDriver, plugin: Plugin, pluginCache: Record<string, SerializablePluginCache> | void): {
|
|
216
|
-
cache: Cache<any>;
|
|
217
|
-
};
|
|
210
|
+
declare function build(options: BuildOptions): Promise<BuildOutput>;
|
|
218
211
|
|
|
219
|
-
export { Api, Argument0,
|
|
212
|
+
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
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
5
|
+
var path = require('path');
|
|
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 = path.relative(from, to).replace("../", "").replace(".ts", "").trimEnd();
|
|
158
|
+
return `./${newPath}`;
|
|
159
|
+
};
|
|
121
160
|
|
|
122
|
-
// src/
|
|
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,31 @@ 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.schema), "utf-8");
|
|
148
179
|
const api = {
|
|
149
180
|
get config() {
|
|
150
181
|
return options.config;
|
|
151
182
|
},
|
|
152
|
-
|
|
153
|
-
|
|
183
|
+
async emitFile(emittedFile) {
|
|
184
|
+
const resolvedId = await resolveId(emittedFile.id, emittedFile.importer, emittedFile.options);
|
|
185
|
+
const id = resolvedId || emittedFile.importer || emittedFile.id;
|
|
186
|
+
return fileEmitter.emitFile({
|
|
187
|
+
...emittedFile,
|
|
188
|
+
id
|
|
189
|
+
});
|
|
154
190
|
},
|
|
155
|
-
on: fileEmitter.on.bind(fileEmitter),
|
|
156
|
-
getEmittedFile: fileEmitter.getEmittedFile.bind(fileEmitter),
|
|
157
|
-
emitFile: fileEmitter.emitFile.bind(fileEmitter),
|
|
158
191
|
resolveId,
|
|
159
|
-
load
|
|
192
|
+
load,
|
|
193
|
+
cache: createPluginCache(/* @__PURE__ */ Object.create(null))
|
|
160
194
|
};
|
|
161
195
|
return {
|
|
162
196
|
name,
|
|
163
|
-
api
|
|
197
|
+
api,
|
|
198
|
+
resolveId(importee, importer) {
|
|
199
|
+
if (!importer) {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
return path.resolve(importer, importee);
|
|
203
|
+
}
|
|
164
204
|
};
|
|
165
205
|
});
|
|
166
206
|
|
|
@@ -176,36 +216,17 @@ var hookNames = {
|
|
|
176
216
|
};
|
|
177
217
|
var hooks = Object.keys(hookNames);
|
|
178
218
|
var PluginDriver = class {
|
|
179
|
-
pluginContexts;
|
|
180
219
|
plugins;
|
|
181
|
-
fileEmitter;
|
|
220
|
+
fileEmitter = new FileEmitter();
|
|
182
221
|
logger;
|
|
183
222
|
config;
|
|
184
|
-
|
|
185
|
-
constructor(config,
|
|
223
|
+
core;
|
|
224
|
+
constructor(config, options) {
|
|
186
225
|
this.logger = options.logger;
|
|
187
226
|
this.config = config;
|
|
188
|
-
this.fileEmitter = new FileEmitter();
|
|
189
227
|
this.emitFile = this.fileEmitter.emitFile.bind(this.fileEmitter);
|
|
190
|
-
|
|
191
|
-
this.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;
|
|
228
|
+
this.core = definePlugin({ config, fileEmitter: this.fileEmitter, load: this.load, resolveId: this.resolveId });
|
|
229
|
+
this.plugins = [this.core, ...config.plugins || []];
|
|
209
230
|
}
|
|
210
231
|
emitFile(...params) {
|
|
211
232
|
return this.fileEmitter.emitFile(...params);
|
|
@@ -225,7 +246,7 @@ var PluginDriver = class {
|
|
|
225
246
|
promise = promise.then((result) => {
|
|
226
247
|
if (result != null)
|
|
227
248
|
return result;
|
|
228
|
-
return this.
|
|
249
|
+
return this.run("hookFirst", hookName, parameters, plugin);
|
|
229
250
|
});
|
|
230
251
|
}
|
|
231
252
|
return promise;
|
|
@@ -236,9 +257,9 @@ var PluginDriver = class {
|
|
|
236
257
|
if (plugin[hookName]?.sequential) {
|
|
237
258
|
await Promise.all(parallelPromises);
|
|
238
259
|
parallelPromises.length = 0;
|
|
239
|
-
await this.
|
|
260
|
+
await this.run("hookParallel", hookName, parameters, plugin);
|
|
240
261
|
} else {
|
|
241
|
-
const promise = this.
|
|
262
|
+
const promise = this.run("hookParallel", hookName, parameters, plugin);
|
|
242
263
|
parallelPromises.push(promise);
|
|
243
264
|
}
|
|
244
265
|
}
|
|
@@ -248,8 +269,8 @@ var PluginDriver = class {
|
|
|
248
269
|
let promise = Promise.resolve(argument0);
|
|
249
270
|
for (const plugin of this.getSortedPlugins(hookName)) {
|
|
250
271
|
promise = promise.then(
|
|
251
|
-
(argument02) => this.
|
|
252
|
-
(result) => reduce.call(this.api, argument02, result, plugin)
|
|
272
|
+
(argument02) => this.run("hookReduceArg0", hookName, [argument02, ...rest], plugin).then(
|
|
273
|
+
(result) => reduce.call(this.core.api, argument02, result, plugin)
|
|
253
274
|
)
|
|
254
275
|
);
|
|
255
276
|
}
|
|
@@ -258,14 +279,14 @@ var PluginDriver = class {
|
|
|
258
279
|
hookSeq(hookName, parameters) {
|
|
259
280
|
let promise = Promise.resolve();
|
|
260
281
|
for (const plugin of this.getSortedPlugins(hookName)) {
|
|
261
|
-
promise = promise.then(() => this.
|
|
282
|
+
promise = promise.then(() => this.run("hookSeq", hookName, parameters, plugin));
|
|
262
283
|
}
|
|
263
284
|
return promise.then(noReturn);
|
|
264
285
|
}
|
|
265
286
|
getSortedPlugins(_hookName) {
|
|
266
287
|
return [...this.plugins];
|
|
267
288
|
}
|
|
268
|
-
|
|
289
|
+
run(strategy, hookName, parameters, plugin) {
|
|
269
290
|
const hook = plugin[hookName];
|
|
270
291
|
return Promise.resolve().then(() => {
|
|
271
292
|
if (typeof hook !== "function") {
|
|
@@ -275,7 +296,7 @@ var PluginDriver = class {
|
|
|
275
296
|
this.logger.spinner.text = `[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
|
|
276
297
|
`;
|
|
277
298
|
}
|
|
278
|
-
const hookResult = hook.apply(this.api, parameters);
|
|
299
|
+
const hookResult = hook.apply(this.core.api, parameters);
|
|
279
300
|
if (!hookResult?.then) {
|
|
280
301
|
if (this.config.logLevel === "info" && this.logger?.spinner) {
|
|
281
302
|
this.logger.spinner.succeed(`[${strategy}] ${hookName}: Excecuting task for plugin ${plugin.name}
|
|
@@ -292,10 +313,10 @@ var PluginDriver = class {
|
|
|
292
313
|
});
|
|
293
314
|
});
|
|
294
315
|
}
|
|
295
|
-
|
|
316
|
+
runSync(hookName, parameters, plugin) {
|
|
296
317
|
const hook = plugin[hookName];
|
|
297
318
|
try {
|
|
298
|
-
return hook.apply(this.api, parameters);
|
|
319
|
+
return hook.apply(this.core.api, parameters);
|
|
299
320
|
} catch (error) {
|
|
300
321
|
return error;
|
|
301
322
|
}
|
|
@@ -304,6 +325,14 @@ var PluginDriver = class {
|
|
|
304
325
|
function noReturn() {
|
|
305
326
|
}
|
|
306
327
|
|
|
328
|
+
// src/config.ts
|
|
329
|
+
var defaultConfig = {
|
|
330
|
+
root: process.cwd()
|
|
331
|
+
};
|
|
332
|
+
function defineConfig(config) {
|
|
333
|
+
return config;
|
|
334
|
+
}
|
|
335
|
+
|
|
307
336
|
// src/build.ts
|
|
308
337
|
async function transformReducer(previousCode, result, _plugin) {
|
|
309
338
|
if (result === null) {
|
|
@@ -312,118 +341,65 @@ async function transformReducer(previousCode, result, _plugin) {
|
|
|
312
341
|
return result;
|
|
313
342
|
}
|
|
314
343
|
async function buildImplementation(options, done) {
|
|
315
|
-
const { config } = options;
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const
|
|
344
|
+
const { config, logger } = options;
|
|
345
|
+
const pluginDriver = new PluginDriver(
|
|
346
|
+
{
|
|
347
|
+
...defaultConfig,
|
|
348
|
+
...config
|
|
349
|
+
},
|
|
350
|
+
{ logger }
|
|
351
|
+
);
|
|
352
|
+
const input = fse.readFileSync(path.resolve(config.root, config.input.path), "utf-8");
|
|
324
353
|
const validations = await pluginDriver.hookParallel("validate", [pluginDriver.plugins]);
|
|
325
354
|
const validationsWithMessage = validations.filter(Boolean);
|
|
326
355
|
if (validationsWithMessage.some((validation) => typeof validation !== "boolean")) {
|
|
327
356
|
validationsWithMessage.forEach((validation) => {
|
|
328
357
|
if (validation && typeof validation !== "boolean" && validation?.message) {
|
|
329
|
-
|
|
358
|
+
logger?.log(validation.message, "warn");
|
|
330
359
|
}
|
|
331
360
|
});
|
|
332
361
|
return;
|
|
333
362
|
}
|
|
334
363
|
pluginDriver.fileEmitter.on("new", async (emittedFile) => {
|
|
335
|
-
if (!emittedFile
|
|
364
|
+
if (!emittedFile) {
|
|
336
365
|
return;
|
|
337
366
|
}
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
367
|
+
const { id } = emittedFile;
|
|
368
|
+
if (!id) {
|
|
369
|
+
throw new Error("No id could be transformed, please add id to emitFile or use resolveId");
|
|
370
|
+
}
|
|
371
|
+
let { source: code } = emittedFile;
|
|
341
372
|
const loadedResult = await pluginDriver.hookFirst("load", [id]);
|
|
342
373
|
if (loadedResult) {
|
|
343
374
|
code = loadedResult;
|
|
344
375
|
}
|
|
345
376
|
if (code) {
|
|
346
|
-
const
|
|
347
|
-
await pluginDriver.hookParallel("writeFile", [
|
|
377
|
+
const transformedCode = await pluginDriver.hookReduceArg0("transform", [code, id], transformReducer);
|
|
378
|
+
await pluginDriver.hookParallel("writeFile", [transformedCode, id]);
|
|
348
379
|
pluginDriver.fileEmitter.delete(emittedFile.id);
|
|
349
380
|
}
|
|
350
381
|
});
|
|
351
382
|
await pluginDriver.hookParallel("buildStart");
|
|
352
383
|
pluginDriver.emitFile({
|
|
353
|
-
id:
|
|
354
|
-
source: pluginDriver.api.config.input.schema,
|
|
384
|
+
id: config.input.path,
|
|
355
385
|
name: void 0,
|
|
356
|
-
|
|
386
|
+
source: input
|
|
357
387
|
});
|
|
358
388
|
pluginDriver.fileEmitter.on("end", async () => {
|
|
359
389
|
await pluginDriver.hookParallel("buildEnd");
|
|
360
390
|
done();
|
|
361
391
|
});
|
|
362
392
|
}
|
|
363
|
-
function build(
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
this,
|
|
371
|
-
{
|
|
372
|
-
config,
|
|
373
|
-
env
|
|
374
|
-
},
|
|
375
|
-
resolve
|
|
376
|
-
);
|
|
393
|
+
function build(options) {
|
|
394
|
+
return new Promise((resolve, reject) => {
|
|
395
|
+
try {
|
|
396
|
+
buildImplementation(options, resolve);
|
|
397
|
+
} catch (e) {
|
|
398
|
+
reject(e);
|
|
399
|
+
}
|
|
377
400
|
});
|
|
378
401
|
}
|
|
379
402
|
|
|
380
|
-
// src/config.ts
|
|
381
|
-
function defineConfig(config) {
|
|
382
|
-
return config;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// src/utils/isPromise.ts
|
|
386
|
-
var isPromise = (result) => {
|
|
387
|
-
return typeof result?.then === "function";
|
|
388
|
-
};
|
|
389
|
-
var formatOptions = {
|
|
390
|
-
tabWidth: 2,
|
|
391
|
-
printWidth: 160,
|
|
392
|
-
parser: "typescript",
|
|
393
|
-
singleQuote: true,
|
|
394
|
-
semi: false,
|
|
395
|
-
bracketSameLine: false,
|
|
396
|
-
endOfLine: "auto"
|
|
397
|
-
};
|
|
398
|
-
var format = (text) => {
|
|
399
|
-
return prettier.format(text, formatOptions);
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
// src/utils/write.ts
|
|
403
|
-
var write = async (data, path4, options = { format: false }) => {
|
|
404
|
-
const formattedData = options.format ? format(data) : data;
|
|
405
|
-
try {
|
|
406
|
-
await fse2.stat(path4);
|
|
407
|
-
const oldContent = await fse2.readFile(path4, { encoding: "utf-8" });
|
|
408
|
-
if (oldContent?.toString() === formattedData) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
} catch (_err) {
|
|
412
|
-
return fse2.outputFile(path4, formattedData);
|
|
413
|
-
}
|
|
414
|
-
return fse2.outputFile(path4, formattedData);
|
|
415
|
-
};
|
|
416
|
-
var getRelativePath = (from, to) => {
|
|
417
|
-
if (!from || !to) {
|
|
418
|
-
throw new Error("From and to should be filled in when retrieving the relativePath");
|
|
419
|
-
}
|
|
420
|
-
const newPath = path2.relative(from, to).replace("../", "").replace(".ts", "").trimEnd();
|
|
421
|
-
if (newPath.startsWith("./") || newPath.startsWith("../")) {
|
|
422
|
-
return newPath.replace;
|
|
423
|
-
}
|
|
424
|
-
return `./${newPath}`;
|
|
425
|
-
};
|
|
426
|
-
|
|
427
403
|
// src/index.ts
|
|
428
404
|
var src_default = build;
|
|
429
405
|
|
|
@@ -434,9 +410,9 @@ exports.build = build;
|
|
|
434
410
|
exports.createPlugin = createPlugin;
|
|
435
411
|
exports.createPluginCache = createPluginCache;
|
|
436
412
|
exports.default = src_default;
|
|
413
|
+
exports.defaultConfig = defaultConfig;
|
|
437
414
|
exports.defineConfig = defineConfig;
|
|
438
415
|
exports.format = format;
|
|
439
|
-
exports.getPluginContext = getPluginContext;
|
|
440
416
|
exports.getRelativePath = getRelativePath;
|
|
441
417
|
exports.hooks = hooks;
|
|
442
418
|
exports.isPromise = isPromise;
|