Skip to content

Commit

Permalink
replace last 'src' for core build
Browse files Browse the repository at this point in the history
  • Loading branch information
boylec committed Jan 28, 2025
1 parent c6b8ca7 commit 0684244
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
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 @@ -187,7 +188,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 @@ -231,7 +232,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 @@ -257,7 +258,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 @@ -275,7 +276,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 @@ -293,7 +294,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
6 changes: 6 additions & 0 deletions scripts/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export async function findFirstPath(paths: string[], { cwd }: { cwd: string }) {
}
return null;
}

export const replaceSrcWithDist = (path: string): string => {
const regex = new RegExp(/(.*)src/);
const result = path.replace(regex, `$1dist`);
return result;
};

0 comments on commit 0684244

Please sign in to comment.