Skip to content

Commit

Permalink
feat(esbuild-meta): specify entry and dist
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder committed Apr 18, 2024
1 parent c0becf4 commit a6a7698
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
26 changes: 13 additions & 13 deletions packages/esbuild-meta/src/lib/filter-meta.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandModule, CommandBuilder, Options, InferredOptionTypes, Argv } from 'yargs';
import {
extractEntryFromManifest,
extractEntryPoints,
filterMetaFromEntryPoints,
removeDynamicImports,
getJson,
makeJson,
removeDynamicImports,
} from './utils.js';

const distPath = {
Expand All @@ -14,13 +14,6 @@ const distPath = {
description: 'The path to the dist folder'
} as const satisfies Options;

const appDist = {
alias: 'ad',
type: 'string',
default: '',
description: 'The path to the dist folder of the build excluding base dist',
} as const satisfies Options;

const outPath = {
alias: 'o',
type: 'string',
Expand All @@ -35,7 +28,15 @@ const excludeDynamicImports = {
default: false,
} as const satisfies Options;

const filterMetaOptions = { distPath, appDist, outPath, excludeDynamicImports };
const entryPoints = {
alias: 'e',
type: 'array',
default: ['main-', 'polyfills-'],
coerce: (i) => i.map((value: never) => String(value)),
description: 'Entry points that should be considered for the bundle',
} as const satisfies Options;

const filterMetaOptions = { distPath, outPath, excludeDynamicImports, entryPoints };

type FilterMetaOptions = InferredOptionTypes<typeof filterMetaOptions>;
type FilterMetaCommandModule = CommandModule<unknown, FilterMetaOptions>;
Expand All @@ -45,9 +46,8 @@ const filterMetaBuilder: CommandBuilder<unknown, FilterMetaOptions> = (argv: Arg
}

const filterMetaHandler: FilterMetaCommandModule['handler'] = (argv: FilterMetaOptions) => {
const manifest = getJson([argv.distPath, argv.appDist, 'manifest.json']);
const entryPoints = extractEntryFromManifest(manifest);
const meta = getJson([argv.distPath, argv.appDist, 'stats.json']);
const meta = getJson([argv.distPath]);
const entryPoints = extractEntryPoints(meta, argv.entryPoints);
filterMetaFromEntryPoints(meta, entryPoints);
if (argv.excludeDynamicImports) {
removeDynamicImports(meta);
Expand Down
12 changes: 6 additions & 6 deletions packages/esbuild-meta/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export function makeJson(path: string, file: any) {
writeFileSync(join(...[path]), JSON.stringify(file, null, 4), {encoding: 'utf-8'});
}

export function extractEntryFromManifest(manifest: any) {
const polyfills = manifest["polyfills.js"].replace('/ClientDist/', '');
const main = manifest["main.js"].replace('/ClientDist/', '');
return [main, polyfills];
}

export function removeDynamicImports(meta: Metafile): void {
const chunksPaths = Object.keys(meta.outputs);
for (const chunkPath of chunksPaths) {
Expand All @@ -28,6 +22,12 @@ export function removeDynamicImports(meta: Metafile): void {
}
}

const matchesPattern = (fileName: string, patterns: string[]) => patterns.some(substring => fileName.includes(substring));

export function extractEntryPoints(meta: Metafile, patterns: string[]): string[] {
return Object.keys(meta.outputs).filter(fileName => matchesPattern(fileName, patterns));
}

export function filterMetaFromEntryPoints(meta: Metafile, entryPoints: string[]) {
const extractedChunks = new Set<string>();
const alreadyExtractedChildren = (chunk: string) => extractedChunks.has(chunk);
Expand Down

0 comments on commit a6a7698

Please sign in to comment.