Ignore unnecessary build artifacts

Summary:
This allows bundles to have their `.buildignore` file. I did that first to remove some things from the `static` folder, but it turned out that those aren't just unnecessary for the bundle but completely unused in general. I still think there may be a case for allowing this on a per-package basis.

Right now it excludes `.ts` files, license files and readmes. Full list of excluded files is here: P129474747

It adds up to about 6MB in the *DEFLATE compressed* bundle.

Reviewed By: nikoant

Differential Revision: D21176143

fbshipit-source-id: a13e900152617fdf1c2dbfa6330c0a06a75e5484
This commit is contained in:
Pascal Hartig
2020-04-24 08:32:55 -07:00
committed by Facebook GitHub Bot
parent b4a5a17d49
commit 4d58563168
3 changed files with 24 additions and 4 deletions

View File

@@ -9,6 +9,16 @@
import fs from 'fs-extra';
import path from 'path';
import ignore from 'ignore';
const DEFAULT_BUILD_IGNORES = [
'/node_modules',
'README*',
'LICENSE*',
'*.ts',
'*.ls',
'Gruntfile*',
];
/**
* This function copies package into the specified target dir with all its dependencies:
@@ -39,10 +49,18 @@ async function copyPackageWithDependenciesRecursive(
if ((await fs.stat(packageDir)).isSymbolicLink()) {
packageDir = await fs.readlink(packageDir);
}
const ignores = await fs
.readFile(path.join(packageDir, '.buildignore'), 'utf-8')
.then((l) => l.split('\n'))
.catch((_e) => [])
.then((l: Array<string>) => ignore().add(DEFAULT_BUILD_IGNORES.concat(l)));
await fs.copy(packageDir, targetDir, {
dereference: true,
recursive: true,
filter: (src) => !src.startsWith(path.join(packageDir, 'node_modules')),
filter: (src) => {
const relativePath = path.relative(packageDir, src);
return relativePath === '' || !ignores.ignores(relativePath);
},
});
const pkg = await fs.readJson(path.join(packageDir, 'package.json'));
const dependencies = (pkg.dependencies ?? {}) as {[key: string]: string};