Skip to content

Commit bbec0a1

Browse files
committed
build: allow synthetic default imports
1 parent b4a2f90 commit bbec0a1

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/dependencies.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { dirname, relative, isAbsolute } from 'path'
2+
import { readFileSync } from 'fs'
3+
import { sync as findUp } from 'find-up'
4+
5+
/**
6+
* Determines if the `child` path is under the `parent` path.
7+
*/
8+
function isInDirectory(parent: string, child: string): boolean {
9+
const relativePath = relative(parent, child)
10+
return !relativePath.startsWith('..') && !isAbsolute(relativePath)
11+
}
12+
13+
/**
14+
* Iterates over package.json file paths recursively found in parent directories, starting from the
15+
* current working directory. If the current working directory is in a git repository, then package.json
16+
* files outside of the git repository will not be yielded.
17+
*/
18+
export function* findPackagePaths(): Generator<string> {
19+
// Find git root if in git repository
20+
const gitDirectoryPath: string | undefined = findUp('.git', { type: 'directory' })
21+
const gitRootPath: string | undefined = gitDirectoryPath === undefined
22+
? undefined
23+
: dirname(gitDirectoryPath)
24+
25+
function isInGitDirectory(path: string): boolean {
26+
return gitRootPath === undefined || isInDirectory(gitRootPath, path)
27+
}
28+
29+
let cwd = process.cwd()
30+
let packagePath
31+
32+
while (
33+
(packagePath = findUp('package.json', { type: 'file', cwd })) &&
34+
isInGitDirectory(packagePath)
35+
) {
36+
yield packagePath
37+
cwd = dirname(packagePath)
38+
}
39+
}
40+
41+
export function findDependencies(
42+
{ packagePaths, keys, warnings }: {
43+
packagePaths: Iterable<string>,
44+
keys: string[],
45+
warnings: string[]
46+
}
47+
): string[] {
48+
const dependencies: Set<string> = new Set()
49+
50+
for (const packagePath of packagePaths) {
51+
try {
52+
const pkg: { [key in PropertyKey]: any } = JSON.parse((readFileSync(packagePath)).toString()) ?? {}
53+
54+
for (const key of keys) {
55+
const dependenciesToVersions: { [key in PropertyKey]: any } = pkg[key] ?? {}
56+
57+
for (const dependency of Object.keys(dependenciesToVersions)) {
58+
dependencies.add(dependency)
59+
}
60+
}
61+
} catch {
62+
warnings.push(`Couldn't process '${packagePath}'. Make sure it is a valid JSON or use the 'packagePath' option`)
63+
}
64+
}
65+
66+
return Array.from(dependencies)
67+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { resolve } from 'path'
99
import { Plugin } from 'rollup'
10-
import * as builtinModules from 'builtin-modules'
10+
import builtinModules from 'builtin-modules'
1111

1212
export interface ExternalsOptions {
1313
/** Path/to/your/package.json file. Defaults to the one in `process.cwd()`. */

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"checkJs": false,
1010
"allowJs": false,
1111
"moduleResolution": "node",
12-
"allowSyntheticDefaultImports": false,
12+
"allowSyntheticDefaultImports": true,
1313
"esModuleInterop": false,
1414
"lib": [ "es2018" ],
1515

0 commit comments

Comments
 (0)