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

Build: Replace last 'src', not 1st 'src' in entry file paths #30404

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions code/core/scripts/helpers/generatePackageJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { join, relative } from 'node:path';
import slash from 'slash';

import { sortPackageJson } from '../../../../scripts/prepare/tools';
import { replaceSrcWithDist } from '../../../../scripts/utils/paths';
import type { getEntries } from '../entries';

const cwd = process.cwd();
Expand All @@ -18,7 +19,7 @@ export async function generatePackageJsonFile(entries: ReturnType<typeof getEntr
* correct path.
*/
pkgJson.exports = entries.reduce<Record<string, Record<string, string>>>((acc, entry) => {
let main = './' + slash(relative(cwd, entry.file).replace('src', 'dist'));
let main = './' + slash(replaceSrcWithDist(relative(cwd, entry.file)));

const content: Record<string, string> = {};
if (entry.dts) {
Expand Down Expand Up @@ -62,7 +63,7 @@ export async function generatePackageJsonFile(entries: ReturnType<typeof getEntr
return acc;
}

let main = slash(relative(cwd, entry.file).replace('src', 'dist'));
let main = slash(replaceSrcWithDist(relative(cwd, entry.file)));
if (main === './dist/index.ts' || main === './dist/index.tsx') {
main = '.';
}
Expand Down
3 changes: 2 additions & 1 deletion code/core/scripts/helpers/generateTypesMapperFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mkdir, writeFile } from 'node:fs/promises';
import { dirname, join, relative, sep } from 'node:path';

import { dedent } from '../../../../scripts/prepare/tools';
import { replaceSrcWithDist } from '../../../../scripts/utils/paths';
import type { getEntries } from '../entries';

const cwd = process.cwd();
Expand Down Expand Up @@ -32,7 +33,7 @@ export async function generateTypesMapperFiles(entries: ReturnType<typeof getEnt

await Promise.all(
all.map(async (filePath) => {
const location = filePath.replace('src', 'dist').replace(/\.tsx?/, '.d.ts');
const location = replaceSrcWithDist(filePath).replace(/\.tsx?/, '.d.ts');
if (!existsSync(location)) {
const directory = dirname(location);
await mkdir(directory, { recursive: true });
Expand Down
11 changes: 6 additions & 5 deletions code/core/scripts/prep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
prettyTime,
process,
} from '../../../scripts/prepare/tools';
import { replaceSrcWithDist } from '../../../scripts/utils/paths';
import pkg from '../package.json';
import { globalsModuleInfoMap } from '../src/manager/globals-module-info';
import { getBundles, getEntries, getFinals } from './entries';
Expand Down Expand Up @@ -191,7 +192,7 @@ async function run() {
results.push(
esbuild.context(
merge<EsbuildContextOptions>(browserEsbuildOptions, {
outdir: dirname(entry.file).replace('src', 'dist'),
outdir: replaceSrcWithDist(dirname(entry.file)),
entryPoints: [entry.file],
outExtension: { '.js': '.js' },
alias: {
Expand Down Expand Up @@ -235,7 +236,7 @@ async function run() {
},
entryPoints: [entry.file],
external: [],
outdir: dirname(entry.file).replace('src', 'dist'),
outdir: replaceSrcWithDist(dirname(entry.file)),
outExtension: {
'.js': '.js',
},
Expand All @@ -261,7 +262,7 @@ async function run() {
...entry.externals,
].filter((e) => !entry.internals.includes(e)),
format: 'cjs',
outdir: dirname(entry.file).replace('src', 'dist'),
outdir: replaceSrcWithDist(dirname(entry.file)),
outExtension: {
'.js': '.cjs',
},
Expand All @@ -279,7 +280,7 @@ async function run() {
...esbuildDefaultOptions.external,
...entry.externals,
].filter((e) => !entry.internals.includes(e)),
outdir: dirname(entry.file).replace('src', 'dist'),
outdir: replaceSrcWithDist(dirname(entry.file)),
outExtension: {
'.js': '.js',
},
Expand All @@ -297,7 +298,7 @@ async function run() {
...entry.externals,
].filter((e) => !entry.internals.includes(e)),
format: 'esm',
outdir: dirname(entry.file).replace('src', 'dist'),
outdir: replaceSrcWithDist(dirname(entry.file)),
outExtension: {
'.js': '.js',
},
Expand Down
9 changes: 5 additions & 4 deletions scripts/prepare/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import typescript from 'typescript';
import ts from 'typescript';

import { CODE_DIRECTORY } from '../utils/constants';
import { replaceSrcWithDist } from '../utils/paths';

export { globalExternals };

export const dts = async (entry: string, externals: string[], tsconfig: string) => {
const dir = dirname(entry).replace('src', 'dist');
const dir = replaceSrcWithDist(dirname(entry));
const out = await rollup.rollup({
input: entry,
external: [...externals, 'ast-types'].map((dep) => new RegExp(`^${dep}($|\\/|\\\\)`)),
output: { file: entry.replace('src', 'dist').replace('.ts', '.d.ts'), format: 'es' },
output: { file: replaceSrcWithDist(entry).replace('.ts', '.d.ts'), format: 'es' },
plugins: [
rpd.dts({
respectExternal: true,
Expand All @@ -55,8 +56,8 @@ export const dts = async (entry: string, externals: string[], tsconfig: string)
});
const { output } = await out.generate({
format: 'es',
// dir: dirname(entry).replace('src', 'dist'),
file: entry.replace('src', 'dist').replace('.ts', '.d.ts'),
// dir: replaceSrcWithDist(dirname(entry)),
file: replaceSrcWithDist(entry).replace('.ts', '.d.ts'),
});

await Promise.all(
Expand Down
10 changes: 10 additions & 0 deletions scripts/utils/paths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line depend/ban-dependencies
import { pathExists } from 'fs-extra';
import { join } from 'path';
import { sep } from 'path';

export async function findFirstPath(paths: string[], { cwd }: { cwd: string }) {
for (const filePath of paths) {
Expand All @@ -10,3 +11,12 @@ export async function findFirstPath(paths: string[], { cwd }: { cwd: string }) {
}
return null;
}

export const replaceSrcWithDist = (path: string): string => {
const parts = path.split(sep);
const index = parts.lastIndexOf('src');
if (index !== -1) {
parts[index] = 'dist';
}
return parts.join(sep);
};