Skip to content

Commit bca9218

Browse files
committed
✨🚧 add build scripts
SWC and esbuild could use some work. I'd like to have them read values from package.json & tsconfig.
1 parent 737a697 commit bca9218

File tree

1 file changed

+73
-28
lines changed

1 file changed

+73
-28
lines changed

packages/create-configs/src/steps/scripts.ts

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import Package from '@npmcli/package-json';
2+
import chalk from 'chalk';
23

3-
import { CodeStyleSetupOptions as SetupOptions } from '@code-style/code-style/config-types';
4+
import {
5+
CodeStyleSetupOptions as SetupOptions,
6+
Builder,
7+
} from '@code-style/code-style/config-types';
48
import {
59
Dependencies,
610
includes_js,
@@ -11,6 +15,8 @@ import {
1115

1216
const concurrently_opts = '--raw --group';
1317

18+
const test_file_glob = '**/*.@(spec|test).*';
19+
1420
export type AddNPMScriptsOptions = Pick<
1521
SetupOptions,
1622
| 'languages'
@@ -19,6 +25,7 @@ export type AddNPMScriptsOptions = Pick<
1925
| 'runtime'
2026
| 'overwrite'
2127
| 'library'
28+
| 'input_dir'
2229
| 'output_dir'
2330
>;
2431

@@ -51,13 +58,18 @@ export async function add_npm_scripts(
5158
return dependencies;
5259
}
5360

54-
type BuildScripts = { build?: string; prepublishOnly?: string };
55-
// TODO(2): add npm script for build, prepublishOnly
61+
type BuildScripts = {
62+
[key: `build:${string}`]: string;
63+
build?: string;
64+
prepublishOnly?: string;
65+
};
5666
export function _generate_build_script({
5767
languages,
5868
technologies,
5969
builder,
6070
library,
71+
input_dir,
72+
output_dir,
6173
overwrite = false,
6274
}: AddNPMScriptsOptions): {
6375
scripts: BuildScripts;
@@ -73,52 +85,85 @@ export function _generate_build_script({
7385
if (languages.includes('scss')) deps.d.depend('sass-embedded');
7486
scripts.build = `${deps.p.depend('next')} build`;
7587
} else {
76-
// const build_step: Set<Exclude<Builder, 'bun' | 'none'> | 'scss'> =
77-
// new Set();
78-
// switch (builder) {
79-
// case 'babel':
80-
// case 'esbuild':
81-
// case 'swc':
82-
// case 'tsc':
83-
// build_step.add(builder);
84-
// break;
85-
// default:
86-
// }
87-
// if (languages.includes('scss')) {
88-
// deps.d.depend('sass-embedded');
89-
// build_step.add('scss');
90-
// }
91-
// if (languages.includes('ts')) {
92-
// deps.d.depend('typescript');
93-
// }
88+
const build_step: Set<Exclude<Builder, 'bun' | 'none'> | 'scss'> =
89+
new Set();
90+
switch (builder) {
91+
case 'babel':
92+
case 'esbuild':
93+
case 'swc':
94+
case 'tsc':
95+
build_step.add(builder);
96+
break;
97+
default:
98+
}
99+
if (languages.includes('scss')) {
100+
deps.d.depend('sass-embedded');
101+
build_step.add('scss');
102+
}
103+
if (languages.includes('ts')) {
104+
deps.d.depend('typescript');
105+
}
94106
// if (build_step.has('esbuild')) {
95107
// if (languages.includes('scss')) {
96108
// //
97109
// } else {
98110
// //
99111
// }
100112
// }
101-
// const script = '';
102-
// await write_scripts({ build: script });
113+
114+
scripts.build = `${deps.d.depend('concurrently')} ${concurrently_opts} "npm:build:*"`;
115+
116+
if (library) {
117+
// build types
118+
switch (builder) {
119+
case 'esbuild':
120+
case 'swc':
121+
scripts['build:types'] =
122+
`${deps.d.depend('typescript', { cmd: 'tsc' })} --project tsconfig.build.json --emitDeclarationOnly`;
123+
break;
124+
default:
125+
}
126+
}
127+
128+
// build JS
129+
switch (builder) {
130+
case 'esbuild':
131+
scripts['build:js'] =
132+
`${deps.d.depend('esbuild')} --tsconfig=tsconfig.build.json $(${deps.d.depend('glob')} '${input_dir}/**/*.?(c|m)[jt]s' --ignore '${test_file_glob}') --outdir=${output_dir} --sourcemap=inline --platform=node --target=node18 --format=${technologies.includes('esm') ? 'esm' : 'cjs'}`;
133+
break;
134+
case 'swc':
135+
deps.d.depend('@swc/core');
136+
scripts['build:js'] =
137+
`${deps.d.depend('@swc/cli', { cmd: 'swc' })} ./${input_dir} --ignore '${test_file_glob}' --out-dir ${output_dir}`;
138+
break;
139+
case 'tsc':
140+
scripts['build:js'] =
141+
`${deps.d.depend('typescript', { cmd: 'tsc' })} --project tsconfig.build.json`;
142+
break;
143+
default:
144+
console.warn(
145+
chalk.yellow`Build scripts using "${builder}" are not at this time`,
146+
);
147+
}
103148
}
104149

105150
if (library) scripts.prepublishOnly = `npm run build`;
106151

107152
return { scripts, dependencies: deps };
108153
}
109154

110-
type LintScripts = Record<`lint:${string}`, string>;
155+
type LintScripts = {
156+
[key: `lint:${string}`]: string;
157+
lint: string;
158+
};
111159

112160
/** @private */
113161
export function _generate_lint_script({
114162
languages,
115163
technologies,
116164
builder,
117165
}: Omit<AddNPMScriptsOptions, 'overwrite' | 'runtime' | 'library'>): {
118-
scripts: {
119-
[key: `lint:${string}`]: string;
120-
lint: string;
121-
};
166+
scripts: LintScripts;
122167
dependencies: Dependencies;
123168
} {
124169
const deps = new Dependencies();

0 commit comments

Comments
 (0)