Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lambda-nodejs): move all directories references to commands #9547

Merged
merged 5 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ export class Bundling {

// Configure target in package.json for Parcel
packageJsonManager.update({
'cdk-lambda': `${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js`,
'targets': {
targets: {
'cdk-lambda': {
context: 'node',
includeNodeModules: includeNodeModules ?? true,
Expand All @@ -153,16 +152,24 @@ export class Bundling {

// 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 = [
'$(node -p "require.resolve(\'parcel\')")', // Parcel is not globally installed, find its "bin"
'build', containerEntryPath.replace(/\\/g, '/'), // Always use POSIX paths in the container
'--target', 'cdk-lambda',
'--no-autoinstall',
'--no-scope-hoist',
...options.cacheDir
? ['--cache-dir', '/parcel-cache']
: [],
].join(' ');
const distFile = path.basename(options.entry).replace(/\.ts$/, '.js');
const parcelCommand = chain([
[
'$(node -p "require.resolve(\'parcel\')")', // Parcel is not globally installed, find its "bin"
'build', containerEntryPath.replace(/\\/g, '/'), // Always use POSIX paths in the container
'--target', 'cdk-lambda',
'--dist-dir', cdk.AssetStaging.BUNDLING_OUTPUT_DIR, // Output bundle in /asset-output (will have the same name as the entry)
'--no-autoinstall',
'--no-scope-hoist',
...options.cacheDir
? ['--cache-dir', '/parcel-cache']
: [],
].join(' '),
// Always rename dist file to index.js because Lambda doesn't support filenames
// with multiple dots and we can end up with multiple dots when using automatic
// entry lookup
`mv ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/${distFile} ${cdk.AssetStaging.BUNDLING_OUTPUT_DIR}/index.js`,
]);

let installer = Installer.NPM;
let lockfile: string | undefined;
Expand Down
7 changes: 3 additions & 4 deletions packages/@aws-cdk/aws-lambda-nodejs/test/bundling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('Parcel bundling', () => {
workingDirectory: '/asset-input/folder',
command: [
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --no-autoinstall --no-scope-hoist --cache-dir /parcel-cache',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist --cache-dir /parcel-cache && mv /asset-output/entry.js /asset-output/index.js',
],
}),
});
Expand All @@ -55,8 +55,7 @@ test('Parcel bundling', () => {
const call = writeFileSyncMock.mock.calls[0];
expect(call[0]).toMatch('package.json');
expect(JSON.parse(call[1])).toEqual(expect.objectContaining({
'cdk-lambda': '/asset-output/index.js',
'targets': {
targets: {
'cdk-lambda': {
context: 'node',
includeNodeModules: {
Expand Down Expand Up @@ -107,7 +106,7 @@ test('Parcel bundling with externals and dependencies', () => {
bundling: expect.objectContaining({
command: [
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --no-autoinstall --no-scope-hoist && mv /asset-input/.package.json /asset-output/package.json && cd /asset-output && npm install',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist && mv /asset-output/entry.js /asset-output/index.js && mv /asset-input/.package.json /asset-output/package.json && cd /asset-output && npm install',
],
}),
});
Expand Down