/
bundling.ts
214 lines (187 loc) · 6.17 KB
/
bundling.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
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import * as fs from 'fs';
import * as path from 'path';
import { PackageJsonManager } from './package-json-manager';
import { findUp } from './util';
/**
* Base options for Parcel bundling
*/
export interface ParcelBaseOptions {
/**
* Whether to minify files when bundling.
*
* @default false
*/
readonly minify?: boolean;
/**
* Whether to include source maps when bundling.
*
* @default false
*/
readonly sourceMaps?: boolean;
/**
* The cache directory
*
* Parcel uses a filesystem cache for fast rebuilds.
*
* @default - `.cache` in the root directory
*/
readonly cacheDir?: string;
/**
* The root of the project. This will be used as the source for the volume
* mounted in the Docker container. If you specify this prop, ensure that
* this path includes `entry` and any module/dependencies used by your
* function otherwise bundling will not be possible.
*
* @default - the closest path containing a .git folder
*/
readonly projectRoot?: string;
/**
* Environment variables defined when Parcel runs.
*
* @default - no environment variables are defined.
*/
readonly parcelEnvironment?: { [key: string]: string; };
/**
* A list of modules that should be considered as externals (already available
* in the runtime).
*
* @default ['aws-sdk']
*/
readonly externalModules?: string[];
/**
* A list of modules that should be installed instead of bundled. Modules are
* installed in a Lambda compatible environnment.
*
* @default - all modules are bundled
*/
readonly nodeModules?: string[];
/**
* The version of Parcel to use.
*
* @default - 2.0.0-beta.1
*/
readonly parcelVersion?: string;
}
/**
* Options for Parcel bundling
*/
export interface ParcelOptions extends ParcelBaseOptions {
/**
* Entry file
*/
readonly entry: string;
/**
* The runtime of the lambda function
*/
readonly runtime: lambda.Runtime;
}
/**
* Bundling
*/
export class Bundling {
/**
* Parcel bundled Lambda asset code
*/
public static parcel(options: ParcelOptions): lambda.AssetCode {
// Find project root
const projectRoot = options.projectRoot ?? findUp(`.git${path.sep}`);
if (!projectRoot) {
throw new Error('Cannot find project root. Please specify it with `projectRoot`.');
}
// Bundling image derived from runtime bundling image (lambci)
const image = cdk.BundlingDockerImage.fromAsset(path.join(__dirname, '../parcel'), {
buildArgs: {
IMAGE: options.runtime.bundlingDockerImage.image,
PARCEL_VERSION: options.parcelVersion ?? '2.0.0-beta.1',
},
});
const packageJsonManager = new PackageJsonManager(path.dirname(options.entry));
// Collect external and install modules
let includeNodeModules: { [key: string]: boolean } | undefined;
let dependencies: { [key: string]: string } | undefined;
const externalModules = options.externalModules ?? ['aws-sdk'];
if (externalModules || options.nodeModules) {
const modules = [...externalModules, ...options.nodeModules ?? []];
includeNodeModules = {};
for (const mod of modules) {
includeNodeModules[mod] = false;
}
if (options.nodeModules) {
dependencies = packageJsonManager.getVersions(options.nodeModules);
}
}
// Configure target in package.json for Parcel
packageJsonManager.update({
'cdk-lambda': `${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js`,
'targets': {
'cdk-lambda': {
context: 'node',
includeNodeModules: includeNodeModules ?? true,
sourceMap: options.sourceMaps ?? false,
minify: options.minify ?? false,
engines: {
node: `>= ${runtimeVersion(options.runtime)}`,
},
},
},
});
// Entry file path relative to container path
const containerEntryPath = path.join(cdk.AssetStaging.BUNDLING_INPUT_DIR, path.relative(projectRoot, path.resolve(options.entry)));
const parcelCommand = `parcel build ${containerEntryPath.replace(/\\/g, '/')} --target cdk-lambda${options.cacheDir ? ' --cache-dir /parcel-cache' : ''}`;
let installer = Installer.NPM;
let lockfile: string | undefined;
let depsCommand = '';
if (dependencies) {
// Create a dummy package.json for dependencies that we need to install
fs.writeFileSync(
path.join(projectRoot, '.package.json'),
JSON.stringify({ dependencies }),
);
// Use npm unless we have a yarn.lock.
if (fs.existsSync(path.join(projectRoot, LockFile.YARN))) {
installer = Installer.YARN;
lockfile = LockFile.YARN;
} else if (fs.existsSync(path.join(projectRoot, LockFile.NPM))) {
lockfile = LockFile.NPM;
}
// Move dummy package.json and lock file then install
depsCommand = chain([
`mv ${cdk.AssetStaging.BUNDLING_INPUT_DIR}/.package.json ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/package.json`,
lockfile ? `cp ${cdk.AssetStaging.BUNDLING_INPUT_DIR}/${lockfile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${lockfile}` : '',
`cd ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR} && ${installer} install`,
]);
}
return lambda.Code.fromAsset(projectRoot, {
assetHashType: cdk.AssetHashType.BUNDLE,
bundling: {
image,
command: ['bash', '-c', chain([parcelCommand, depsCommand])],
environment: options.parcelEnvironment,
volumes: options.cacheDir
? [{ containerPath: '/parcel-cache', hostPath: options.cacheDir }]
: [],
workingDirectory: path.dirname(containerEntryPath).replace(/\\/g, '/'), // Always use POSIX paths in the container
},
});
}
}
enum Installer {
NPM = 'npm',
YARN = 'yarn',
}
enum LockFile {
NPM = 'package-lock.json',
YARN = 'yarn.lock'
}
function runtimeVersion(runtime: lambda.Runtime): string {
const match = runtime.name.match(/nodejs(\d+)/);
if (!match) {
throw new Error('Cannot extract version from runtime.');
}
return match[1];
}
function chain(commands: string[]): string {
return commands.filter(c => !!c).join(' && ');
}