forked from floydspace/serverless-esbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
489 lines (395 loc) · 15 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import assert from 'assert';
import path from 'path';
import fs from 'fs-extra';
import globby from 'globby';
import { concat, mergeDeepRight } from 'ramda';
import type Serverless from 'serverless';
import type ServerlessPlugin from 'serverless/classes/Plugin';
import chokidar from 'chokidar';
import anymatch from 'anymatch';
import {
asArray,
assertIsString,
assertIsSupportedRuntime,
buildServerlessV3LoggerFromLegacyLogger,
extractFunctionEntries,
isNodeMatcherKey,
isString,
providerRuntimeMatcher,
} from './helper';
import { packExternalModules } from './pack-externals';
import { pack } from './pack';
import { preOffline } from './pre-offline';
import { preLocal } from './pre-local';
import { bundle } from './bundle';
import { BUILD_FOLDER, ONLY_PREFIX, SERVERLESS_FOLDER, WORK_FOLDER } from './constants';
import type {
ConfigFn,
Configuration,
EsbuildFunctionDefinitionHandler,
FileBuildResult,
FunctionBuildResult,
Plugins,
ReturnPluginsFn,
} from './types';
function updateFile(op: string, src: string, dest: string) {
if (['add', 'change', 'addDir'].includes(op)) {
fs.copySync(src, dest, {
dereference: true,
errorOnExist: false,
preserveTimestamps: true,
recursive: true,
});
return;
}
if (['unlink', 'unlinkDir'].includes(op)) {
fs.removeSync(dest);
}
}
class EsbuildServerlessPlugin implements ServerlessPlugin {
serviceDirPath: string;
outputWorkFolder: string | undefined;
workDirPath: string | undefined;
outputBuildFolder: string | undefined;
buildDirPath: string | undefined;
log: ServerlessPlugin.Logging['log'];
serverless: Serverless;
options: Serverless.Options;
hooks: ServerlessPlugin.Hooks;
buildOptions: Configuration | undefined;
buildResults: FunctionBuildResult[] | undefined;
/** Used for storing previous esbuild build results so we can rebuild more efficiently */
buildCache: Record<string, FileBuildResult> = {};
// These are bound to imported functions.
packExternalModules: typeof packExternalModules;
pack: typeof pack;
preOffline: typeof preOffline;
preLocal: typeof preLocal;
bundle: typeof bundle;
constructor(serverless: Serverless, options: Serverless.Options, logging?: ServerlessPlugin.Logging) {
this.serverless = serverless;
this.options = options;
this.log = logging?.log || buildServerlessV3LoggerFromLegacyLogger(this.serverless.cli, this.options.verbose);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore old versions use servicePath, new versions serviceDir. Types will use only one of them
this.serviceDirPath = this.serverless.config.serviceDir || this.serverless.config.servicePath;
this.packExternalModules = packExternalModules.bind(this);
this.pack = pack.bind(this);
this.preOffline = preOffline.bind(this);
this.preLocal = preLocal.bind(this);
this.bundle = bundle.bind(this);
// This tells serverless that this skipEsbuild property can exist in a function definition, but isn't required.
// That way a user could skip a function if they have defined their own artifact, for example.
this.serverless.configSchemaHandler.defineFunctionProperties(this.serverless.service.provider.name, {
properties: {
skipEsbuild: { type: 'boolean' },
},
});
this.hooks = {
initialize: () => this.init(),
'before:run:run': async () => {
await this.bundle();
await this.packExternalModules();
await this.copyExtras();
},
'before:offline:start': async () => {
await this.bundle(true);
await this.packExternalModules();
await this.copyExtras();
await this.preOffline();
this.watch();
},
'before:offline:start:init': async () => {
await this.bundle(true);
await this.packExternalModules();
await this.copyExtras();
await this.preOffline();
this.watch();
},
'before:package:createDeploymentArtifacts': async () => {
await this.bundle();
await this.packExternalModules();
await this.copyExtras();
await this.pack();
},
'after:package:createDeploymentArtifacts': async () => {
await this.cleanup();
},
'before:deploy:function:packageFunction': async () => {
await this.bundle();
await this.packExternalModules();
await this.copyExtras();
await this.pack();
},
'after:deploy:function:packageFunction': async () => {
await this.cleanup();
},
'before:invoke:local:invoke': async () => {
await this.bundle();
await this.packExternalModules();
await this.copyExtras();
await this.preLocal();
},
};
}
private init() {
this.buildOptions = this.getBuildOptions();
this.outputWorkFolder = this.buildOptions.outputWorkFolder || WORK_FOLDER;
this.outputBuildFolder = this.buildOptions.outputBuildFolder || BUILD_FOLDER;
this.workDirPath = path.join(this.serviceDirPath, this.outputWorkFolder);
this.buildDirPath = path.join(this.workDirPath, this.outputBuildFolder);
}
/**
* Checks if the runtime for the given function is nodejs.
* If the runtime is not set , checks the global runtime.
* @param {Serverless.FunctionDefinitionHandler} func the function to be checked
* @returns {boolean} true if the function/global runtime is nodejs; false, otherwise
*/
private isNodeFunction(func: Serverless.FunctionDefinitionHandler): boolean {
const runtime = func.runtime || this.serverless.service.provider.runtime;
const runtimeMatcher = providerRuntimeMatcher[this.serverless.service.provider.name];
return isNodeMatcherKey(runtime) && typeof runtimeMatcher?.[runtime] === 'string';
}
/**
* Checks if the function has a handler
* @param {Serverless.FunctionDefinitionHandler | Serverless.FunctionDefinitionImage} func the function to be checked
* @returns {boolean} true if the function has a handler
*/
private isFunctionDefinitionHandler(
func: Serverless.FunctionDefinitionHandler | Serverless.FunctionDefinitionImage
): func is Serverless.FunctionDefinitionHandler {
return Boolean((func as Serverless.FunctionDefinitionHandler)?.handler);
}
get functions(): Record<string, Serverless.FunctionDefinitionHandler> {
const functions = this.options.function
? {
[this.options.function]: this.serverless.service.getFunction(this.options.function),
}
: this.serverless.service.functions;
// ignore all functions with a different runtime than nodejs:
const nodeFunctions: Record<string, Serverless.FunctionDefinitionHandler> = {};
for (const [functionAlias, fn] of Object.entries(functions)) {
if (
this.isFunctionDefinitionHandler(fn) &&
this.isNodeFunction(fn) &&
!(fn as EsbuildFunctionDefinitionHandler).skipEsbuild
) {
nodeFunctions[functionAlias] = fn;
}
}
return nodeFunctions;
}
get plugins(): Plugins {
if (!this.buildOptions?.plugins) {
return [];
}
if (Array.isArray(this.buildOptions.plugins)) {
return this.buildOptions.plugins;
}
const plugins: Plugins | ReturnPluginsFn = require(path.join(this.serviceDirPath, this.buildOptions.plugins));
if (typeof plugins === 'function') {
return plugins(this.serverless);
}
return plugins;
}
get packagePatterns() {
const { service } = this.serverless;
const patterns: string[] = [];
const ignored: string[] = [];
for (const pattern of service.package.patterns) {
if (pattern.startsWith('!')) {
ignored.push(pattern.slice(1));
} else {
patterns.push(pattern);
}
}
for (const fn of Object.values(this.functions)) {
const fnPatterns = asArray(fn.package?.patterns).filter(isString);
for (const pattern of fnPatterns) {
if (pattern.startsWith('!')) {
ignored.push(pattern.slice(1));
} else {
patterns.push(pattern);
}
}
}
return { patterns, ignored };
}
private getBuildOptions() {
const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
concurrency: Infinity,
bundle: true,
target: 'node12',
external: [],
exclude: ['aws-sdk'],
nativeZip: false,
packager: 'npm',
packagerOptions: {
noInstall: false,
},
installExtraArgs: [],
watch: {
pattern: './**/*.(js|ts)',
ignore: [WORK_FOLDER, 'dist', 'node_modules', BUILD_FOLDER],
chokidar: {
ignoreInitial: true,
},
},
keepOutputDirectory: false,
platform: 'node',
outputFileExtension: '.js',
};
const providerRuntime = this.serverless.service.provider.runtime;
assertIsSupportedRuntime(providerRuntime);
const runtimeMatcher = providerRuntimeMatcher[this.serverless.service.provider.name];
const target = isNodeMatcherKey(providerRuntime) ? runtimeMatcher?.[providerRuntime] : undefined;
const resolvedOptions = {
...(target ? { target } : {}),
};
const withDefaultOptions = mergeDeepRight(DEFAULT_BUILD_OPTIONS);
const withResolvedOptions = mergeDeepRight(withDefaultOptions(resolvedOptions));
const configPath: string | undefined = this.serverless.service.custom?.esbuild?.config;
const config: ConfigFn | undefined = configPath ? require(path.join(this.serviceDirPath, configPath)) : undefined;
return withResolvedOptions<Configuration>(
config ? config(this.serverless) : this.serverless.service.custom?.esbuild ?? {}
) as Configuration;
}
get functionEntries() {
return extractFunctionEntries(this.serviceDirPath, this.serverless.service.provider.name, this.functions);
}
watch(): void {
assert(this.buildOptions, 'buildOptions is not defined');
const defaultPatterns = asArray(this.buildOptions.watch.pattern).filter(isString);
const defaultIgnored = asArray(this.buildOptions.watch.ignore).filter(isString);
const { patterns, ignored } = this.packagePatterns;
const allPatterns: string[] = [...defaultPatterns, ...patterns];
const allIgnored: string[] = [...defaultIgnored, ...ignored];
const options = {
ignored: allIgnored,
...this.buildOptions.watch.chokidar,
};
chokidar.watch(allPatterns, options).on('all', (eventName, srcPath) =>
this.bundle(true)
.then(() => this.updateFile(eventName, srcPath))
.then(() => this.log.verbose('Watching files for changes...'))
.catch(() => this.log.error('Bundle error, waiting for a file change to reload...'))
);
}
prepare() {
assertIsString(this.buildDirPath, 'buildDirPath is not a string');
assertIsString(this.workDirPath, 'workDirPath is not a string');
fs.mkdirpSync(this.buildDirPath);
fs.mkdirpSync(path.join(this.workDirPath, SERVERLESS_FOLDER));
// exclude serverless-esbuild
this.serverless.service.package = {
...(this.serverless.service.package || {}),
patterns: [
...new Set([
...(this.serverless.service.package?.include || []),
...(this.serverless.service.package?.exclude || []).map(concat('!')),
...(this.serverless.service.package?.patterns || []),
'!node_modules/serverless-esbuild',
]),
],
};
for (const fn of Object.values(this.functions)) {
fn.package = {
...(fn.package || {}),
patterns: [
...new Set([
...(fn.package?.include || []),
...(fn.package?.exclude || []).map(concat('!')),
...(fn.package?.patterns || []),
]),
],
};
}
}
async updateFile(op: string, filename: string) {
assertIsString(this.buildDirPath, 'buildDirPath is not a string');
const { service } = this.serverless;
const patterns = asArray(service.package.patterns).filter(isString);
if (
patterns.length > 0 &&
anymatch(
patterns.filter((pattern) => !pattern.startsWith('!')),
filename
)
) {
const destFileName = path.resolve(path.join(this.buildDirPath, filename));
updateFile(op, path.resolve(filename), destFileName);
return;
}
for (const [functionAlias, fn] of Object.entries(this.functions)) {
if (fn.package?.patterns?.length === 0) {
continue;
}
if (
anymatch(
asArray(fn.package?.patterns)
.filter(isString)
.filter((pattern) => !pattern.startsWith('!')),
filename
)
) {
const destFileName = path.resolve(path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename));
updateFile(op, path.resolve(filename), destFileName);
return;
}
}
}
/** Link or copy extras such as node_modules or package.patterns definitions */
async copyExtras() {
assertIsString(this.buildDirPath, 'buildDirPath is not a string');
const { service } = this.serverless;
const packagePatterns = asArray(service.package.patterns).filter(isString);
// include any "extras" from the "patterns" section
if (packagePatterns.length) {
const files = await globby(packagePatterns);
for (const filename of files) {
const destFileName = path.resolve(path.join(this.buildDirPath, filename));
updateFile('add', path.resolve(filename), destFileName);
}
}
// include any "extras" from the individual function "patterns" section
for (const [functionAlias, fn] of Object.entries(this.functions)) {
const patterns = asArray(fn.package?.patterns).filter(isString);
if (!patterns.length) {
continue;
}
const files = await globby(patterns);
for (const filename of files) {
const destFileName = path.resolve(path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename));
updateFile('add', path.resolve(filename), destFileName);
}
}
}
/**
* Move built code to the serverless folder, taking into account individual
* packaging preferences.
*/
async moveArtifacts(): Promise<void> {
assertIsString(this.workDirPath, 'workDirPath is not a string');
const { service } = this.serverless;
await fs.copy(path.join(this.workDirPath, SERVERLESS_FOLDER), path.join(this.serviceDirPath, SERVERLESS_FOLDER));
if (service.package.individually === true || this.options.function) {
Object.values(this.functions).forEach((func) => {
if (func.package?.artifact) {
// eslint-disable-next-line no-param-reassign
func.package.artifact = path.join(SERVERLESS_FOLDER, path.basename(func.package.artifact));
}
});
return;
}
service.package.artifact = path.join(SERVERLESS_FOLDER, path.basename(service.package.artifact));
}
async cleanup(): Promise<void> {
await this.moveArtifacts();
// Remove temp build folder
if (!this.buildOptions?.keepOutputDirectory) {
assertIsString(this.workDirPath, 'workDirPath is not a string');
fs.removeSync(path.join(this.workDirPath));
}
}
}
export = EsbuildServerlessPlugin;