Skip to content

Commit

Permalink
feat(deploy): export handleDependencies (#5854)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe committed Jun 26, 2024
1 parent 33e3a95 commit 3755228
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 74 deletions.
6 changes: 6 additions & 0 deletions .changeset/cold-squids-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/app-tools': patch
---

feat: export handleDependencies
feat: 暴露 handleDependencies 方法
2 changes: 1 addition & 1 deletion packages/server/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/esm-node/index.d.ts",
"import": "./dist/esm-node/index.js",
"default": "./dist/cjs/index.js"
},
"./node": {
Expand Down
10 changes: 9 additions & 1 deletion packages/solutions/app-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"modern",
"modern.js"
],
"version": "2.54.3",
"version": "2.54.3-alpha.6",
"jsnext:source": "./src/index.ts",
"types": "./src/index.ts",
"main": "./dist/cjs/index.js",
Expand Down Expand Up @@ -43,6 +43,11 @@
"types": "./dist/types/exports/server.d.ts",
"jsnext:source": "./src/exports/server.ts",
"default": "./dist/cjs/exports/server.js"
},
"./deploy": {
"types": "./dist/types/plugins/deploy/exports.d.ts",
"jsnext:source": "./src/plugins/deploy/exports.ts",
"default": "./dist/cjs/plugins/deploy/exports.js"
}
},
"engines": {
Expand All @@ -58,6 +63,9 @@
],
"server": [
"./dist/types/exports/server.d.ts"
],
"deploy": [
"./dist/types/plugins/deploy/exports.d.ts"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path, { isAbsolute } from 'node:path';
import path from 'node:path';
import { fs as fse, pkgUp, semver } from '@modern-js/utils';
import type { PackageJson } from 'pkg-types';
import { readPackageJSON } from 'pkg-types';
Expand All @@ -10,31 +10,32 @@ import {
TracedPackage,
TracedFile,
findEntryFiles,
traceFiles,
traceFiles as defaultTraceFiles,
findPackageParents,
resolveTracedPath,
readDirRecursive,
} from './utils';

export const handleDependencies = async (
appDir: string,
serverRootDir: string,
include: string[],
entryFilter?: (filePath: string) => boolean,
) => {
export const handleDependencies = async ({
appDir,
serverRootDir,
includeEntries,
traceFiles = defaultTraceFiles,
entryFilter,
modifyPackageJson,
copyWholePackage,
}: {
appDir: string;
serverRootDir: string;
includeEntries: string[];
traceFiles?: typeof defaultTraceFiles;
entryFilter?: (filePath: string) => boolean;
modifyPackageJson?: (pkgJson: PackageJson) => PackageJson;
copyWholePackage?: (pkgName: string) => boolean;
}) => {
const base = '/';
const entryFiles = await findEntryFiles(serverRootDir, entryFilter);

const includeEntries = include.map(item => {
if (isAbsolute(item)) {
return item;
}
try {
// FIXME: should appoint paths
return require.resolve(item);
} catch (error) {}
return item;
});

const fileTrace = await traceFiles(
entryFiles.concat(includeEntries),
serverRootDir,
Expand Down Expand Up @@ -161,11 +162,22 @@ export const handleDependencies = async (
tracedPackage.versions[pkgJSON.version!] = tracedPackageVersion;
}

tracedFile.path.startsWith(tracedFile.pkgPath) &&
tracedPackageVersion.path === tracedFile.pkgPath &&
tracedPackageVersion.files.push(tracedFile.path);
tracedFile.pkgName = pkgName;
tracedFile.pkgVersion = pkgJSON.version;

const shouldCopyWholePackage = copyWholePackage?.(pkgName);
if (
tracedFile.path.startsWith(tracedFile.pkgPath) &&
// Merged package files are based on the version, not on paths, to handle some boundary cases
tracedPackageVersion.pkgJSON.version === tracedFile.pkgVersion
) {
if (shouldCopyWholePackage) {
const allFiles = await readDirRecursive(tracedFile.pkgPath);
tracedPackageVersion.files.push(...allFiles);
} else {
tracedPackageVersion.files.push(tracedFile.path);
}
}
}

const multiVersionPkgs: Record<string, { [version: string]: string[] }> = {};
Expand All @@ -191,7 +203,11 @@ export const handleDependencies = async (
singleVersionPackages.map(pkgName => {
const pkg = tracedPackages[pkgName];
const version = Object.keys(pkg.versions)[0];
return writePackage(pkg, version, serverRootDir);
return writePackage({
pkg,
version,
projectDir: serverRootDir,
});
}),
);

Expand Down Expand Up @@ -228,7 +244,12 @@ export const handleDependencies = async (
const pkg = tracedPackages[pkgName];

const pkgDestPath = `.modernjs/${pkgName}@${version}/node_modules/${pkgName}`;
await writePackage(pkg, version, serverRootDir, pkgDestPath);
await writePackage({
pkg,
version,
projectDir: serverRootDir,
_pkgPath: pkgDestPath,
});
await linkPackage(pkgDestPath, `${pkgName}`, serverRootDir);

for (const parentPkg of parentPkgs) {
Expand All @@ -249,7 +270,8 @@ export const handleDependencies = async (
}

const outputPkgPath = path.join(serverRootDir, 'package.json');
await fse.writeJSON(outputPkgPath, {

const newPkgJson = {
name: `${projectPkgJson.name || 'modernjs-project'}-prod`,
version: projectPkgJson.version || '0.0.0',
private: true,
Expand All @@ -261,5 +283,9 @@ export const handleDependencies = async (
]),
].sort(([a], [b]) => a.localeCompare(b)),
),
});
};

const finalPkgJson = modifyPackageJson?.(newPkgJson) || newPkgJson;

await fse.writeJSON(outputPkgPath, finalPkgJson);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import os from 'node:os';
import { fs as fse } from '@modern-js/utils';
import type { PackageJson } from 'pkg-types';
import { parseNodeModulePath } from 'mlly';
import { nodeFileTrace, resolve } from '@vercel/nft';
import { nodeFileTrace } from '@vercel/nft';

export type TracedPackage = {
name: string;
Expand Down Expand Up @@ -35,12 +35,15 @@ function applyPublicCondition(pkg: PackageJson) {
}
}

export const writePackage = async (
pkg: TracedPackage,
version: string,
projectDir: string,
_pkgPath?: string,
) => {
interface WritePackageOptions {
pkg: TracedPackage;
version: string;
projectDir: string;
_pkgPath?: string;
}

export const writePackage = async (options: WritePackageOptions) => {
const { pkg, version, projectDir, _pkgPath } = options;
const pkgPath = _pkgPath || pkg.name;
for (const src of pkg.versions[version].files) {
if (src.includes('node_modules')) {
Expand Down Expand Up @@ -156,7 +159,8 @@ export const findPackageParents = (
const parentPkgs = [
...new Set(
versionFiles.flatMap(file =>
file.parents
// Because it supports copyWholePackage configuration, not all files exist.
file?.parents
.map(parentPath => {
const parentFile = tracedFiles[parentPath];

Expand All @@ -170,7 +174,7 @@ export const findPackageParents = (
),
),
];
return parentPkgs as string[];
return parentPkgs.filter(parentPkg => parentPkg) as string[];
};

export const traceFiles = async (
Expand All @@ -181,15 +185,6 @@ export const traceFiles = async (
return await nodeFileTrace(entryFiles, {
base,
processCwd: serverRootDir,
resolve: async (id, parent, job, isCjs) => {
if (id.startsWith('@modern-js/prod-server')) {
return require.resolve(id, {
paths: [require.resolve('@modern-js/app-tools')],
});
} else {
return resolve(id, parent, job, isCjs);
}
},
});
};

Expand Down
1 change: 1 addition & 0 deletions packages/solutions/app-tools/src/plugins/deploy/exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { handleDependencies } from './dependencies';
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ export const createNetlifyPreset: CreatePreset = (
if (!needModernServer) {
return;
}
await handleDependencies(appDirectory, funcsDirectory, [
'@modern-js/prod-server',
]);
await handleDependencies({
appDir: appDirectory,
serverRootDir: funcsDirectory,
includeEntries: [require.resolve('@modern-js/prod-server')],
});
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ export const createNodePreset: CreatePreset = (appContext, config) => {
return !filePath.startsWith(staticDirectory);
};
// Because @modern-js/prod-server is an implicit dependency of the entry, so we add it to the include here.
await handleDependencies(
appDirectory,
outputDirectory,
['@modern-js/prod-server'],
filter,
);
await handleDependencies({
appDir: appDirectory,
serverRootDir: outputDirectory,
includeEntries: [require.resolve('@modern-js/prod-server')],
entryFilter: filter,
});
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ export const createVercelPreset: CreatePreset = (
if (!needModernServer) {
return;
}
await handleDependencies(appDirectory, funcsDirectory, [
'@modern-js/prod-server',
]);
await handleDependencies({
appDir: appDirectory,
serverRootDir: funcsDirectory,
includeEntries: [require.resolve('@modern-js/prod-server')],
});
},
};
};
26 changes: 10 additions & 16 deletions pnpm-lock.yaml

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

0 comments on commit 3755228

Please sign in to comment.