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

Implement subpath exports #73

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@rollup/plugin-replace": "^5.0.7",
"@rollup/pluginutils": "^5.1.0",
"esbuild": "^0.23.0",
"glob": "^11.0.0",
"magic-string": "^0.30.10",
"rollup": "^4.18.1"
},
Expand Down
51 changes: 51 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { getExportEntries } from './utils/parse-package-json/get-export-entries.js';
import { getAliases } from './utils/parse-package-json/get-aliases.js';
import { normalizePath } from './utils/normalize-path.js';
import { getSourcePath } from './utils/get-source-path.js';
import { getSourcePaths } from './utils/get-source-path.js';
import { getRollupConfigs } from './utils/get-rollup-configs.js';
import { getTsconfig } from './utils/get-tsconfig';
import { log } from './utils/log.js';
Expand Down Expand Up @@ -130,7 +130,7 @@
const validPath = entry.outputPath.startsWith(distPath);

if (!validPath) {
console.warn(`Ignoring entry outside of ${distPath} directory: package.json#${entry.from}=${stringify(entry.outputPath)}`);

Check warning on line 133 in src/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement
}

return validPath;
Expand All @@ -140,10 +140,8 @@
throw new Error('No export entries found in package.json');
}

const sourcePaths = await Promise.all(exportEntries.map(async exportEntry => ({
...(await getSourcePath(exportEntry, sourcePath, distPath)),
exportEntry,
})));
const sourcePaths = (await Promise.all(exportEntries.map(exportEntry => getSourcePaths(exportEntry, sourcePath, distPath, cwd))));

Check failure on line 143 in src/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

This line has a length of 132. Maximum allowed is 100
const flatSourcePaths = sourcePaths.flat();

const rollupConfigs = await getRollupConfigs(

Expand All @@ -155,7 +153,7 @@
*/
normalizePath(fs.realpathSync.native(sourcePath), true),
distPath,
sourcePaths,
flatSourcePaths,
argv.flags,
getAliases(packageJson, cwd),
packageJson,
Expand Down Expand Up @@ -207,6 +205,6 @@
);
}
})().catch((error) => {
console.error(error);

Check warning on line 208 in src/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement
process.exit(1);
});
2 changes: 1 addition & 1 deletion src/local-typescript-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
}
};

// eslint-disable-next-line n/global-require, import-x/no-dynamic-require
// eslint-disable-next-line import-x/no-dynamic-require
export default require(getLocalTypescriptPath());

Check failure on line 13 in src/local-typescript-loader.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected require()
48 changes: 46 additions & 2 deletions src/utils/get-source-path.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { globSync } from 'glob';
import type { ExportEntry } from '../types.js';
import { fsExists } from './fs-exists.js';

Expand All @@ -6,10 +7,11 @@
const tryExtensions = async (
pathWithoutExtension: string,
extensions: readonly string[],
checker: (sourcePath: string)=>Promise<boolean>,
) => {
for (const extension of extensions) {
const pathWithExtension = pathWithoutExtension + extension;
if (await fsExists(pathWithExtension)) {
if (await checker(pathWithExtension)) {
return {
extension,
path: pathWithExtension,
Expand All @@ -33,14 +35,16 @@
exportEntry: ExportEntry,
source: string,
dist: string,
) => {
checker: (sourcePath: string)=>Promise<boolean> = fsExists,
): Promise<Omit<SourcePath, 'exportEntry'>> => {
const sourcePathUnresolved = source + exportEntry.outputPath.slice(dist.length);

for (const distExtension of distExtensions) {
if (exportEntry.outputPath.endsWith(distExtension)) {
const sourcePath = await tryExtensions(
sourcePathUnresolved.slice(0, -distExtension.length),
extensionMap[distExtension],
checker,
);

if (sourcePath) {
Expand All @@ -55,3 +59,43 @@

throw new Error(`Could not find matching source file for export path ${stringify(exportEntry.outputPath)}`);
};

interface SourcePath {
exportEntry: ExportEntry;
input: string;
srcExtension: string;
distExtension: string;
}

export const getSourcePaths = async (
exportEntry: ExportEntry,
sourcePath: string,
distPath: string,
cwd: string,
): Promise<SourcePath[]> => {
if (exportEntry.outputPath.includes('*')) {
// use glob to resolve matches from the packageJsonRoot directory
const matchSet = new Set<string>();
const sourceMatch = await getSourcePath(exportEntry, sourcePath, distPath, async (path) => {
const matches = globSync(path, { cwd });
for (const match of matches) { matchSet.add(match); }

Check warning on line 81 in src/utils/get-source-path.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

This line has 2 statements. Maximum allowed is 1
return matches.length > 0; // always return false to prevent early exit
});

const matchedPaths = Array.from(matchSet);

const allMatches = matchedPaths.map(match => ({
exportEntry,
input: match,
srcExtension: sourceMatch.srcExtension,
distExtension: sourceMatch.distExtension,
}));

return allMatches;
}

return [{
exportEntry,
...await getSourcePath(exportEntry, sourcePath, distPath, fsExists),
}];
};
Loading
Loading