Skip to content

Commit

Permalink
Improve bundling of dependencies and source files
Browse files Browse the repository at this point in the history
  • Loading branch information
iansu committed Apr 25, 2022
1 parent 8c3a829 commit de811f4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Serverless Plugin Neo Changelog

## 0.2.0 - April 25, 2022

- Improve error handling when linking dependencies
- Bundle source files that aren't included by TypeScript

## 0.1.6 - April 25, 2022

- Fix race condition when linking dependencies
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "serverless-plugin-neo",
"description": "Serverless plugin that compiles TypeScript code and bundles dependencies with node-file-trace",
"version": "0.1.6",
"version": "0.2.0",
"author": "Neo Financial Engineering <[email protected]>",
"license": "MIT",
"repository": {
Expand Down
37 changes: 30 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,31 @@ export class NeoPlugin {
this.log.verbose(`trace reasons ${tracedDependencies.reasons}`);

const dependencies = await handleSpecialCases([...tracedDependencies.fileList], baseDir);
const localDependencies = [];
const rootDependencies = [];
const sourceFiles = [];
const localDependencies = new Set<string>();
const rootDependencies = new Set<string>();
const sourceFiles = new Set<string>();

for (const dependency of dependencies) {
if (dependency.startsWith('node_modules')) {
rootDependencies.push(dependency);
rootDependencies.add(dependency);
} else if (dependency.startsWith(path.join(packagePrefix, 'node_modules'))) {
localDependencies.push(dependency);
} else {
sourceFiles.push(dependency);
localDependencies.add(dependency);
} else if (
!dependency.includes('.build') &&
dependency !== 'package.json' &&
dependency !== path.join(packagePrefix, 'package.json')
) {
sourceFiles.add(dependency);
}
}

for (const dependency of localDependencies) {
const filename = dependency.replace(`${path.join(packagePrefix)}${path.sep}`, '');

if (rootDependencies.has(filename)) {
this.log.verbose(`pruning root dependency: ${filename}`);

rootDependencies.delete(filename);
}
}

Expand Down Expand Up @@ -329,6 +343,15 @@ export class NeoPlugin {
await linkOrCopy(sourcePath, destinationPath);
}

for (const file of sourceFiles) {
const sourcePath = path.resolve(baseDir, file);
const destinationPath = path.resolve(path.join(BUILD_FOLDER, file.replace(packagePrefix, '')));

this.log.verbose(`${sourcePath} -> ${destinationPath}`);

await linkOrCopy(sourcePath, destinationPath);
}

if (!fs.existsSync(this.outPackagePath)) {
await linkOrCopy(path.resolve('package.json'), this.outPackagePath, 'file');
}
Expand Down
12 changes: 10 additions & 2 deletions src/lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ const extractFilenames = (

const linkOrCopy = async (srcPath: string, dstPath: string, type?: fs.SymlinkType): Promise<void> => {
try {
return fs.ensureSymlink(srcPath, dstPath, type);
await fs.ensureSymlink(srcPath, dstPath, type);
} catch (error) {
if (error.code === 'EPERM' && error.errno === -4048) {
return fs.copy(srcPath, dstPath);
try {
await fs.copy(srcPath, dstPath);
} catch (error) {
console.error('Failed to symlink and copy file', error);
}
} else if (error.code === 'EEXIST' && error.errno === -17) {
console.warn(chalk.yellow(`WARNING: File already exists: ${srcPath} -> ${dstPath}`));

return Promise.resolve(void 0);
}

throw error;
Expand Down

0 comments on commit de811f4

Please sign in to comment.