balm-core 6.0.0-alpha.2 → 6.0.0-alpha.3
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/CHANGELOG.md +16 -0
- package/dist/index.cjs +395 -111
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -3
- package/dist/index.d.ts +21 -3
- package/dist/index.js +381 -97
- package/dist/index.js.map +1 -1
- package/package.json +38 -1
- package/src/balm.ts +0 -107
- package/src/bundler/esbuild.ts +0 -48
- package/src/bundler/index.ts +0 -3
- package/src/bundler/rollup.ts +0 -46
- package/src/bundler/webpack.ts +0 -125
- package/src/config/constants.ts +0 -12
- package/src/config/defaults.ts +0 -212
- package/src/config/index.ts +0 -112
- package/src/hooks/index.ts +0 -224
- package/src/index.ts +0 -16
- package/src/pipeline/file.ts +0 -69
- package/src/pipeline/index.ts +0 -10
- package/src/pipeline/pipeline.ts +0 -135
- package/src/pipeline/transforms/cache.ts +0 -147
- package/src/pipeline/transforms/htmlmin.ts +0 -18
- package/src/pipeline/transforms/less.ts +0 -75
- package/src/pipeline/transforms/postcss.ts +0 -43
- package/src/pipeline/transforms/rename.ts +0 -38
- package/src/pipeline/transforms/replace.ts +0 -30
- package/src/pipeline/transforms/sass.ts +0 -106
- package/src/pipeline/transforms/terser.ts +0 -20
- package/src/runner/dag.ts +0 -77
- package/src/runner/task.ts +0 -38
- package/src/shims.d.ts +0 -5
- package/src/tasks/cache.ts +0 -84
- package/src/tasks/clean.ts +0 -21
- package/src/tasks/copy.ts +0 -36
- package/src/tasks/ftp.ts +0 -90
- package/src/tasks/html.ts +0 -290
- package/src/tasks/index.ts +0 -14
- package/src/tasks/publish.ts +0 -71
- package/src/tasks/pwa.ts +0 -86
- package/src/tasks/remove.ts +0 -24
- package/src/tasks/replace.ts +0 -34
- package/src/tasks/script.ts +0 -28
- package/src/tasks/server.ts +0 -218
- package/src/tasks/static.ts +0 -77
- package/src/tasks/style.ts +0 -57
- package/src/tasks/zip.ts +0 -77
- package/src/types/index.ts +0 -225
- package/src/utilities/file.ts +0 -140
- package/src/utilities/logger.ts +0 -63
- package/src/utilities/utils.ts +0 -46
- package/src/utilities/workspace.ts +0 -38
- package/src/watcher/index.ts +0 -100
- package/tsconfig.json +0 -8
- package/tsup.config.ts +0 -12
package/src/config/index.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { BalmConfig, BalmCustomConfig } from '../types/index.js';
|
|
3
|
-
import { createDefaultConfig } from './defaults.js';
|
|
4
|
-
import { deepMerge } from '../utilities/utils.js';
|
|
5
|
-
import { ASSETS_TYPES } from './constants.js';
|
|
6
|
-
import { setWorkspaces } from '../utilities/workspace.js';
|
|
7
|
-
|
|
8
|
-
export function resolveConfig(
|
|
9
|
-
customConfig: BalmCustomConfig = {},
|
|
10
|
-
defaultWorkspace = process.cwd()
|
|
11
|
-
): BalmConfig {
|
|
12
|
-
let localWorkspace = defaultWorkspace;
|
|
13
|
-
let globalWorkspace = path.resolve(defaultWorkspace, '..');
|
|
14
|
-
|
|
15
|
-
if (customConfig.workspace) {
|
|
16
|
-
if (typeof customConfig.workspace === 'string') {
|
|
17
|
-
localWorkspace = path.resolve(customConfig.workspace);
|
|
18
|
-
globalWorkspace = path.resolve(localWorkspace, '..');
|
|
19
|
-
} else if (typeof customConfig.workspace === 'object') {
|
|
20
|
-
localWorkspace = path.resolve(customConfig.workspace.local || defaultWorkspace);
|
|
21
|
-
globalWorkspace = path.resolve(
|
|
22
|
-
customConfig.workspace.global || path.resolve(localWorkspace, '..')
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
setWorkspaces(localWorkspace, globalWorkspace);
|
|
28
|
-
|
|
29
|
-
const defaultConfig = createDefaultConfig(localWorkspace);
|
|
30
|
-
const config = deepMerge(defaultConfig, customConfig) as BalmConfig;
|
|
31
|
-
|
|
32
|
-
// Set normalized workspace path
|
|
33
|
-
config.workspace = localWorkspace;
|
|
34
|
-
config.workspaces = {
|
|
35
|
-
local: localWorkspace,
|
|
36
|
-
global: globalWorkspace
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// Update src quick directories
|
|
40
|
-
config.src = {
|
|
41
|
-
base: path.join(config.workspace, config.roots.source),
|
|
42
|
-
html: path.join(config.workspace, config.roots.source, config.paths.source.html),
|
|
43
|
-
css: path.join(config.workspace, config.roots.source, config.paths.source.css),
|
|
44
|
-
js: path.join(config.workspace, config.roots.source, config.paths.source.js),
|
|
45
|
-
img: path.join(config.workspace, config.roots.source, config.paths.source.img),
|
|
46
|
-
font: path.join(config.workspace, config.roots.source, config.paths.source.font),
|
|
47
|
-
media: path.join(config.workspace, config.roots.source, config.paths.source.media)
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// Update dest quick directories
|
|
51
|
-
const targetBase = path.join(
|
|
52
|
-
config.workspace,
|
|
53
|
-
config.env.isProd || !config.inFrontend ? config.roots.target : config.roots.tmp
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const isProd = config.env.isProd || !config.inFrontend;
|
|
57
|
-
const pathMap = isProd ? config.paths.target : config.paths.tmp;
|
|
58
|
-
|
|
59
|
-
const destStatic = path.join(
|
|
60
|
-
targetBase,
|
|
61
|
-
config.assets.virtualDir,
|
|
62
|
-
config.assets.subDir
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
config.dest = {
|
|
66
|
-
base: targetBase,
|
|
67
|
-
html: targetBase,
|
|
68
|
-
css: path.join(targetBase, pathMap.css),
|
|
69
|
-
js: path.join(targetBase, pathMap.js),
|
|
70
|
-
img: path.join(targetBase, pathMap.img),
|
|
71
|
-
font: path.join(targetBase, pathMap.font),
|
|
72
|
-
media: path.join(targetBase, pathMap.media),
|
|
73
|
-
static: destStatic
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
// Set assets.static
|
|
77
|
-
config.assets.static = path.join(
|
|
78
|
-
config.assets.root,
|
|
79
|
-
config.assets.mainDir,
|
|
80
|
-
config.assets.subDir
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
for (const assetType of ASSETS_TYPES) {
|
|
84
|
-
(config.assets as any)[assetType] = path.join(
|
|
85
|
-
config.assets.static,
|
|
86
|
-
(config.paths.target as any)[assetType]
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Set HMR flag
|
|
91
|
-
const hmrEnv =
|
|
92
|
-
config.useDefaults &&
|
|
93
|
-
config.env.isDev &&
|
|
94
|
-
!config.env.inSSR &&
|
|
95
|
-
!config.env.isMP;
|
|
96
|
-
const hmrEnabling = config.scripts.bundler === 'webpack';
|
|
97
|
-
config.server.useHMR = hmrEnv && hmrEnabling;
|
|
98
|
-
|
|
99
|
-
// Set desktop app
|
|
100
|
-
if (/^electron-.*/.test(config.scripts.target as string)) {
|
|
101
|
-
config.env.inDesktopApp = true;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Set use cache for webpack
|
|
105
|
-
config.scripts.useCache =
|
|
106
|
-
config.env.isProd && config.assets.cache && config.scripts.injectHtml;
|
|
107
|
-
|
|
108
|
-
return config;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export * from './constants.js';
|
|
112
|
-
export * from './defaults.js';
|
package/src/hooks/index.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CopyTask,
|
|
3
|
-
RemoveTask,
|
|
4
|
-
ReplaceTask,
|
|
5
|
-
PublishTask,
|
|
6
|
-
PwaTask,
|
|
7
|
-
ZipTask,
|
|
8
|
-
FtpTask,
|
|
9
|
-
ServerTask
|
|
10
|
-
} from '../tasks/index.js';
|
|
11
|
-
import { FunctionTask } from '../runner/task.js';
|
|
12
|
-
import { Pipeline } from '../pipeline/pipeline.js';
|
|
13
|
-
import { transformSass } from '../pipeline/transforms/sass.js';
|
|
14
|
-
import { transformLess } from '../pipeline/transforms/less.js';
|
|
15
|
-
import { transformPostcss } from '../pipeline/transforms/postcss.js';
|
|
16
|
-
import { runWebpack } from '../bundler/webpack.js';
|
|
17
|
-
import { runRollup } from '../bundler/rollup.js';
|
|
18
|
-
import { runEsbuild } from '../bundler/esbuild.js';
|
|
19
|
-
import { TaskDAG } from '../runner/dag.js';
|
|
20
|
-
import {
|
|
21
|
-
RenameOptions,
|
|
22
|
-
ReplaceOptions,
|
|
23
|
-
TemplateOption,
|
|
24
|
-
BalmConfig
|
|
25
|
-
} from '../types/index.js';
|
|
26
|
-
import { logger } from '../utilities/logger.js';
|
|
27
|
-
|
|
28
|
-
export class BalmHooks {
|
|
29
|
-
private dag: TaskDAG;
|
|
30
|
-
private _config: BalmConfig;
|
|
31
|
-
private recipeIndex = 0;
|
|
32
|
-
|
|
33
|
-
constructor(dag: TaskDAG, config: BalmConfig) {
|
|
34
|
-
this.dag = dag;
|
|
35
|
-
this._config = config;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get config(): BalmConfig {
|
|
39
|
-
return this._config;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
get env() {
|
|
43
|
-
return this._config.env;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private addRecipeTask(fn: (config: BalmConfig) => Promise<void> | void): void {
|
|
47
|
-
this.recipeIndex++;
|
|
48
|
-
const taskName = `recipe:${this.recipeIndex}`;
|
|
49
|
-
this.dag.registerTask(new FunctionTask(taskName, fn));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
copy(
|
|
53
|
-
input: string | string[],
|
|
54
|
-
output: string,
|
|
55
|
-
options: { rename?: RenameOptions | string | Function } = {}
|
|
56
|
-
): this {
|
|
57
|
-
this.addRecipeTask(async (config) => {
|
|
58
|
-
const task = new CopyTask({ input, output, rename: options.rename });
|
|
59
|
-
await task.run(config);
|
|
60
|
-
});
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
remove(target: string | string[]): this {
|
|
65
|
-
this.addRecipeTask(async (config) => {
|
|
66
|
-
const task = new RemoveTask(target);
|
|
67
|
-
await task.run(config);
|
|
68
|
-
});
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
replace(
|
|
73
|
-
input: string | string[],
|
|
74
|
-
output: string,
|
|
75
|
-
options: ReplaceOptions | ReplaceOptions[]
|
|
76
|
-
): this {
|
|
77
|
-
this.addRecipeTask(async (config) => {
|
|
78
|
-
const task = new ReplaceTask({ input, output, options });
|
|
79
|
-
await task.run(config);
|
|
80
|
-
});
|
|
81
|
-
return this;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
sass(input: string | string[], output: string, options = {}): this {
|
|
85
|
-
this.addRecipeTask(async (config) => {
|
|
86
|
-
const pipeline = Pipeline.from(input, { cwd: config.workspace });
|
|
87
|
-
pipeline.pipe(transformSass(options));
|
|
88
|
-
pipeline.pipe(transformPostcss({ minify: config.env.isProd }));
|
|
89
|
-
await pipeline.dest(output);
|
|
90
|
-
});
|
|
91
|
-
return this;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
less(input: string | string[], output: string, options = {}): this {
|
|
95
|
-
this.addRecipeTask(async (config) => {
|
|
96
|
-
const pipeline = Pipeline.from(input, { cwd: config.workspace });
|
|
97
|
-
pipeline.pipe(transformLess(options));
|
|
98
|
-
pipeline.pipe(transformPostcss({ minify: config.env.isProd }));
|
|
99
|
-
await pipeline.dest(output);
|
|
100
|
-
});
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
css(input: string | string[], output: string, options = {}): this {
|
|
105
|
-
this.addRecipeTask(async (config) => {
|
|
106
|
-
const pipeline = Pipeline.from(input, { cwd: config.workspace });
|
|
107
|
-
pipeline.pipe(transformPostcss({ minify: config.env.isProd, ...options }));
|
|
108
|
-
await pipeline.dest(output);
|
|
109
|
-
});
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
webpack(entry?: any, output?: string, customOptions: any = {}): this {
|
|
114
|
-
this.addRecipeTask(async (config) => {
|
|
115
|
-
const mergedOptions = {
|
|
116
|
-
...customOptions,
|
|
117
|
-
...(entry ? { entry } : {}),
|
|
118
|
-
output: {
|
|
119
|
-
...(output ? { path: output } : {}),
|
|
120
|
-
...customOptions.output
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
await runWebpack(config, mergedOptions);
|
|
124
|
-
});
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
rollup(
|
|
129
|
-
input?: any,
|
|
130
|
-
output?: any,
|
|
131
|
-
customInputOptions: any = {},
|
|
132
|
-
customOutputOptions: any = {}
|
|
133
|
-
): this {
|
|
134
|
-
this.addRecipeTask(async (config) => {
|
|
135
|
-
let inputOptions: any = {};
|
|
136
|
-
let outputOptions: any = {};
|
|
137
|
-
|
|
138
|
-
if (typeof input === 'object' && !Array.isArray(input) && input.input) {
|
|
139
|
-
inputOptions = { ...input, ...customInputOptions };
|
|
140
|
-
outputOptions =
|
|
141
|
-
typeof output === 'object'
|
|
142
|
-
? { ...output, ...customOutputOptions }
|
|
143
|
-
: { ...(output ? { dir: output } : {}), ...customOutputOptions };
|
|
144
|
-
} else {
|
|
145
|
-
inputOptions = { ...(input ? { input } : {}), ...customInputOptions };
|
|
146
|
-
outputOptions =
|
|
147
|
-
typeof output === 'object'
|
|
148
|
-
? { ...output, ...customOutputOptions }
|
|
149
|
-
: { ...(output ? { dir: output } : {}), ...customOutputOptions };
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
await runRollup(config, inputOptions, outputOptions);
|
|
153
|
-
});
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
esbuild(entryPoints?: any, outdir?: string, customOptions = {}): this {
|
|
158
|
-
this.addRecipeTask(async (config) => {
|
|
159
|
-
await runEsbuild(config, {
|
|
160
|
-
...(entryPoints ? { entryPoints } : {}),
|
|
161
|
-
...(outdir ? { outdir } : {}),
|
|
162
|
-
...customOptions
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
return this;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
js(input?: any, output?: any, options = {}): this {
|
|
169
|
-
logger.warn('balm api', '`mix.js` is deprecated, please use `mix.webpack`/`mix.rollup`/`mix.esbuild` instead.');
|
|
170
|
-
return this.webpack(input, output, options);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
publish(
|
|
174
|
-
input?: string | TemplateOption[],
|
|
175
|
-
output?: string,
|
|
176
|
-
renameOptions?: string | Function | RenameOptions
|
|
177
|
-
): this {
|
|
178
|
-
this.addRecipeTask(async (config) => {
|
|
179
|
-
const task = new PublishTask({ input, output, renameOptions });
|
|
180
|
-
await task.run(config);
|
|
181
|
-
});
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
generateSW(options = {}): this {
|
|
186
|
-
this.addRecipeTask(async (config) => {
|
|
187
|
-
const task = new PwaTask({ mode: 'generateSW', options });
|
|
188
|
-
await task.run(config);
|
|
189
|
-
});
|
|
190
|
-
return this;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
injectManifest(options = {}): this {
|
|
194
|
-
this.addRecipeTask(async (config) => {
|
|
195
|
-
const task = new PwaTask({ mode: 'injectManifest', options });
|
|
196
|
-
await task.run(config);
|
|
197
|
-
});
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
ftp(localFiles?: string | string[], options = {}): this {
|
|
202
|
-
this.addRecipeTask(async (config) => {
|
|
203
|
-
const task = new FtpTask({ localFiles, options });
|
|
204
|
-
await task.run(config);
|
|
205
|
-
});
|
|
206
|
-
return this;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
zip(input?: string | string[], output?: string, filename?: string): this {
|
|
210
|
-
this.addRecipeTask(async (config) => {
|
|
211
|
-
const task = new ZipTask({ input, output, filename });
|
|
212
|
-
await task.run(config);
|
|
213
|
-
});
|
|
214
|
-
return this;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
serve(): this {
|
|
218
|
-
this.addRecipeTask(async (config) => {
|
|
219
|
-
const task = new ServerTask();
|
|
220
|
-
await task.run(config);
|
|
221
|
-
});
|
|
222
|
-
return this;
|
|
223
|
-
}
|
|
224
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { balm, Balm } from './balm.js';
|
|
2
|
-
|
|
3
|
-
export default balm;
|
|
4
|
-
export { Balm, balm };
|
|
5
|
-
export * from './types/index.js';
|
|
6
|
-
export * from './config/index.js';
|
|
7
|
-
export * from './runner/task.js';
|
|
8
|
-
export * from './runner/dag.js';
|
|
9
|
-
export * from './pipeline/index.js';
|
|
10
|
-
export * from './bundler/index.js';
|
|
11
|
-
export * from './tasks/index.js';
|
|
12
|
-
export * from './hooks/index.js';
|
|
13
|
-
export * from './utilities/logger.js';
|
|
14
|
-
export * from './utilities/file.js';
|
|
15
|
-
export * from './utilities/utils.js';
|
|
16
|
-
export * from './utilities/workspace.js';
|
package/src/pipeline/file.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
|
|
3
|
-
export interface VirtualFileOptions {
|
|
4
|
-
cwd: string;
|
|
5
|
-
base: string;
|
|
6
|
-
path: string;
|
|
7
|
-
contents: Buffer | string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class VirtualFile {
|
|
11
|
-
cwd: string;
|
|
12
|
-
base: string;
|
|
13
|
-
path: string;
|
|
14
|
-
contents: Buffer;
|
|
15
|
-
revPathOriginal?: string;
|
|
16
|
-
|
|
17
|
-
constructor(options: VirtualFileOptions) {
|
|
18
|
-
this.cwd = options.cwd;
|
|
19
|
-
this.base = options.base;
|
|
20
|
-
this.path = options.path;
|
|
21
|
-
this.contents = typeof options.contents === 'string'
|
|
22
|
-
? Buffer.from(options.contents)
|
|
23
|
-
: options.contents;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get relative(): string {
|
|
27
|
-
return path.relative(this.base, this.path);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get extname(): string {
|
|
31
|
-
return path.extname(this.path);
|
|
32
|
-
}
|
|
33
|
-
set extname(val: string) {
|
|
34
|
-
const dir = path.dirname(this.path);
|
|
35
|
-
const basename = path.basename(this.path, this.extname);
|
|
36
|
-
this.path = path.join(dir, `${basename}${val.startsWith('.') ? val : `.${val}`}`);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
get basename(): string {
|
|
40
|
-
return path.basename(this.path);
|
|
41
|
-
}
|
|
42
|
-
set basename(val: string) {
|
|
43
|
-
const dir = path.dirname(this.path);
|
|
44
|
-
this.path = path.join(dir, val);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
get stem(): string {
|
|
48
|
-
return path.basename(this.path, this.extname);
|
|
49
|
-
}
|
|
50
|
-
set stem(val: string) {
|
|
51
|
-
const dir = path.dirname(this.path);
|
|
52
|
-
this.path = path.join(dir, `${val}${this.extname}`);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
toString(encoding: BufferEncoding = 'utf8'): string {
|
|
56
|
-
return this.contents.toString(encoding);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
clone(): VirtualFile {
|
|
60
|
-
const cloned = new VirtualFile({
|
|
61
|
-
cwd: this.cwd,
|
|
62
|
-
base: this.base,
|
|
63
|
-
path: this.path,
|
|
64
|
-
contents: Buffer.from(this.contents)
|
|
65
|
-
});
|
|
66
|
-
cloned.revPathOriginal = this.revPathOriginal;
|
|
67
|
-
return cloned;
|
|
68
|
-
}
|
|
69
|
-
}
|
package/src/pipeline/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './file.js';
|
|
2
|
-
export * from './pipeline.js';
|
|
3
|
-
export * from './transforms/sass.js';
|
|
4
|
-
export * from './transforms/less.js';
|
|
5
|
-
export * from './transforms/postcss.js';
|
|
6
|
-
export * from './transforms/htmlmin.js';
|
|
7
|
-
export * from './transforms/terser.js';
|
|
8
|
-
export * from './transforms/replace.js';
|
|
9
|
-
export * from './transforms/rename.js';
|
|
10
|
-
export * from './transforms/cache.js';
|
package/src/pipeline/pipeline.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import fg from 'fast-glob';
|
|
3
|
-
import fs from 'node:fs/promises';
|
|
4
|
-
import { VirtualFile } from './file.js';
|
|
5
|
-
import { file as fsUtil } from '../utilities/file.js';
|
|
6
|
-
|
|
7
|
-
export type TransformFn = (
|
|
8
|
-
file: VirtualFile
|
|
9
|
-
) => Promise<VirtualFile | VirtualFile[] | null | void> | VirtualFile | VirtualFile[] | null | void;
|
|
10
|
-
|
|
11
|
-
export interface PipelineOptions {
|
|
12
|
-
cwd?: string;
|
|
13
|
-
base?: string;
|
|
14
|
-
allowEmpty?: boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class Pipeline {
|
|
18
|
-
private cwd: string;
|
|
19
|
-
private base?: string;
|
|
20
|
-
private allowEmpty: boolean;
|
|
21
|
-
private files: VirtualFile[] = [];
|
|
22
|
-
private transforms: TransformFn[] = [];
|
|
23
|
-
|
|
24
|
-
constructor(options: PipelineOptions = {}) {
|
|
25
|
-
this.cwd = options.cwd || process.cwd();
|
|
26
|
-
this.base = options.base;
|
|
27
|
-
this.allowEmpty = options.allowEmpty ?? true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static from(
|
|
31
|
-
patterns: string | string[],
|
|
32
|
-
options: PipelineOptions = {}
|
|
33
|
-
): Pipeline {
|
|
34
|
-
const pipeline = new Pipeline(options);
|
|
35
|
-
return pipeline.src(patterns);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
src(patterns: string | string[], options: PipelineOptions = {}): this {
|
|
39
|
-
if (options.cwd) this.cwd = options.cwd;
|
|
40
|
-
if (options.base) this.base = options.base;
|
|
41
|
-
if (options.allowEmpty !== undefined) this.allowEmpty = options.allowEmpty;
|
|
42
|
-
|
|
43
|
-
this.transforms.push(async () => {
|
|
44
|
-
const patternArr = Array.isArray(patterns) ? patterns : [patterns];
|
|
45
|
-
const posPatterns = patternArr.filter((p) => !p.startsWith('!'));
|
|
46
|
-
const ignorePatterns = patternArr.filter((p) => p.startsWith('!')).map((p) => p.slice(1));
|
|
47
|
-
|
|
48
|
-
const fileList: VirtualFile[] = [];
|
|
49
|
-
for (const p of posPatterns) {
|
|
50
|
-
let patternBase = this.base;
|
|
51
|
-
if (!patternBase) {
|
|
52
|
-
const globIdx = p.search(/[*?{[\\]/);
|
|
53
|
-
if (globIdx !== -1) {
|
|
54
|
-
patternBase = path.resolve(this.cwd, p.slice(0, globIdx));
|
|
55
|
-
} else {
|
|
56
|
-
patternBase = path.resolve(this.cwd, path.dirname(p));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const matched = await fg(p, {
|
|
61
|
-
cwd: this.cwd,
|
|
62
|
-
absolute: true,
|
|
63
|
-
dot: true,
|
|
64
|
-
onlyFiles: true,
|
|
65
|
-
ignore: ignorePatterns
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
for (const filePath of matched) {
|
|
69
|
-
const buffer = await fs.readFile(filePath);
|
|
70
|
-
fileList.push(
|
|
71
|
-
new VirtualFile({
|
|
72
|
-
cwd: this.cwd,
|
|
73
|
-
base: patternBase,
|
|
74
|
-
path: filePath,
|
|
75
|
-
contents: buffer
|
|
76
|
-
})
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (!fileList.length && !this.allowEmpty) {
|
|
82
|
-
throw new Error(`File not found with pattern: ${patterns}`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.files = fileList;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
pipe(transform: TransformFn): this {
|
|
92
|
-
this.transforms.push(transform);
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async process(): Promise<VirtualFile[]> {
|
|
97
|
-
let isSrcLoaded = false;
|
|
98
|
-
for (const transform of this.transforms) {
|
|
99
|
-
if (!isSrcLoaded) {
|
|
100
|
-
// Initial src loader
|
|
101
|
-
await transform(null as any);
|
|
102
|
-
isSrcLoaded = true;
|
|
103
|
-
} else {
|
|
104
|
-
if (this.files.length === 0) break;
|
|
105
|
-
const nextFiles: VirtualFile[] = [];
|
|
106
|
-
for (const file of this.files) {
|
|
107
|
-
const result = await transform(file);
|
|
108
|
-
if (result === undefined || result === file) {
|
|
109
|
-
nextFiles.push(file);
|
|
110
|
-
} else if (Array.isArray(result)) {
|
|
111
|
-
nextFiles.push(...result);
|
|
112
|
-
} else if (result !== null) {
|
|
113
|
-
nextFiles.push(result);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
this.files = nextFiles;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return this.files;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
async dest(outDir: string): Promise<string[]> {
|
|
123
|
-
const files = await this.process();
|
|
124
|
-
const writtenPaths: string[] = [];
|
|
125
|
-
const absOutDir = path.isAbsolute(outDir) ? outDir : path.resolve(this.cwd, outDir);
|
|
126
|
-
|
|
127
|
-
for (const file of files) {
|
|
128
|
-
const targetPath = path.resolve(absOutDir, file.relative);
|
|
129
|
-
await fsUtil.writeFile(targetPath, file.contents);
|
|
130
|
-
writtenPaths.push(targetPath);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return writtenPaths;
|
|
134
|
-
}
|
|
135
|
-
}
|