Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Commit

Permalink
feat: implement bundleDependenciesOf resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Nov 8, 2019
1 parent 1e59cdd commit 7de22f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ By default it will include only:

- What is defined inside the `files` entry (see [here for more info](https://docs.npmjs.com/files/package.json#files))
- All of the `dependencies` defined inside the package.json (no devDependencies)
- All of the `dependencies` defined inside each `bundleDependenciesOf` entries

# License

Expand Down
9 changes: 1 addition & 8 deletions bin/packpack
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,4 @@ if (args.includes('--help')) {
process.exit(0)
}

run({ cwd: process.cwd() }, options, function onNpmBundle (error, output) {
if (error) {
throw error
}
if (output) {
process.stdout.write(output.file)
}
})
run({ cwd: process.cwd() }, options)
48 changes: 37 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,23 @@ const NEVER_IGNORE = ignoreLinesToRegex([
'!/+(changes|changelog|history)*',
])

const fetchDependencies = async (cwd: string, deps: string[]) => {
let bundleDependenciesFiles = []
for (const dependency of deps) {
const dependencyList = depsFor(dependency, cwd)
for (const dep of dependencyList) {
const filesForBundledDep = await walk(dep.baseDir, null, new Set(FOLDERS_IGNORE))
bundleDependenciesFiles = bundleDependenciesFiles.concat(filesForBundledDep)
}
}
return bundleDependenciesFiles
}

export async function packTarball(
config: Config
): Promise<NodeJS.ReadableStream> {
const pkg = require(path.resolve(config.cwd, 'package.json'))
const { dependencies, main, files: onlyFiles} = pkg;
const { dependencies, main, files: onlyFiles, bundleDependenciesOf } = pkg;

// include required files
let filters: Array<IgnoreFilter> = NEVER_IGNORE.slice()
Expand All @@ -69,12 +81,17 @@ export async function packTarball(
let bundleDependenciesFiles = [];
const bundleDependencies = Object.keys(dependencies)
if (bundleDependencies) {
for (const dependency of bundleDependencies) {
const dependencyList = depsFor(dependency, config.cwd)
for (const dep of dependencyList) {
const filesForBundledDep = await walk(dep.baseDir, null, new Set(FOLDERS_IGNORE))
bundleDependenciesFiles = bundleDependenciesFiles.concat(filesForBundledDep)
}
const deps = await fetchDependencies(config.cwd, bundleDependencies)
bundleDependenciesFiles = bundleDependenciesFiles.concat(deps)
}
// include more dependencies
if (bundleDependenciesOf) {
for (let pkgPath of bundleDependenciesOf) {
if (pkgPath.includes('package.json') === false) pkgPath += `/package.json`
const absolutePkgPath = path.resolve(config.cwd, pkgPath)
const { dependencies } = require(absolutePkgPath)
const resolvedDeps = await fetchDependencies(path.dirname(absolutePkgPath), Object.keys(dependencies))
bundleDependenciesFiles = bundleDependenciesFiles.concat(resolvedDeps)
}
}

Expand Down Expand Up @@ -126,8 +143,16 @@ export async function packTarball(
config.cwd,
keepFiles,
(header: { name: string }) => {
if (header.name.includes(`../../node_modules`)) {
header.name = header.name.replace('../../node_modules', 'node_modules')
// the file might comes from outside of our cwd
// we must rewrite it to be correctly decompressed
if (header.name.startsWith('..')) {
// compute how much we need to cut
// const absolutePath = path.resolve(config.cwd, header.name)
const bundleOf = bundleDependenciesOf.find(path => header.name.startsWith(path))
if (bundleOf) {
const hasTrailingSlash = bundleOf[bundleOf.length - 1] === '/'
header.name = header.name.replace(`${bundleOf}${hasTrailingSlash ? '' : '/'}`, '')
}
}
return header
},
Expand All @@ -142,11 +167,12 @@ function packWithIgnoreAndHeaders(
return tar.pack(cwd, {
entries: Array.from(filesTooKep),
map: header => {
header = mapHeader ? mapHeader(header) : header
const suffix = header.name === '.' ? '' : `/${header.name}`;
header.name = `package${suffix}`;
delete header.uid;
delete header.gid;
return mapHeader ? mapHeader(header) : header;
return header;
},
})
}
Expand All @@ -171,7 +197,7 @@ export async function run(
}

const normaliseScope = name => (name[0] === '@' ? name.substr(1).replace('/', '-') : name)
const filename = flags.filename || path.join(config.cwd, `${normaliseScope(pkg.name)}-v${pkg.version}.tgz`)
const filename = flags.filename || path.join(config.cwd, `${normaliseScope(pkg.name)}-${pkg.version}.tgz`)

const stream = await pack(config)

Expand Down

0 comments on commit 7de22f5

Please sign in to comment.