Skip to content

Commit e78ca7d

Browse files
committed
v2.0 - removes cpy and rimraf
1 parent 09e0374 commit e78ca7d

File tree

5 files changed

+56
-440
lines changed

5 files changed

+56
-440
lines changed

README.md

+27-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ You could try these too. They solve the same problem but using require hooks ins
1818

1919
## How does it work?
2020

21-
esbuild-node-tsc reads the tsconfig.json and builds the typescript project using esbuild. esbuild is the fastest typescript builder around. It also copies the non ts files such as json, graphql files, images,etc to the dist folder. If the assets are not copied correctly check the configuration guide below.
21+
esbuild-node-tsc reads the tsconfig.json and builds the typescript project using esbuild. esbuild is the fastest typescript builder around.
22+
The purpose of this library is to read tsconfig and translate the options to esbuild.
23+
24+
You can also perform custom postbuild and prebuild operations like copying non ts files such as json, graphql files, images,etc to the dist folder or cleaning up build folder.
2225

2326
## Installation
2427

@@ -63,7 +66,7 @@ Since esbuild can build large typescript projects in subsecond speeds you can us
6366
To achieve live reloading:
6467

6568
```
66-
npm install --save-dev nodemon esbuild-node-tsc
69+
npm install --save-dev nodemon esbuild-node-tsc esbuild
6770
```
6871

6972
Then add the following script to package.json
@@ -100,50 +103,43 @@ npm run dev
100103

101104
<img src="https://user-images.githubusercontent.com/4029423/94347242-c6497600-0032-11eb-8a66-4311adf04554.gif" width="638" height="750">
102105

106+
## v2.0 Migration and Breaking changes 🌱
107+
108+
In v2.0, esbuild-node-tsc no longer has cpy and rimraf preinstalled and doesnt perform any non source file copying automatically.
109+
Instead it exposes prebuild and postbuild hooks which can be used to perform custom operations. See the example below for more details.
110+
103111
## Optional configuration
104112

105113
By default esbuild-node-tsc should work out of the box for your project since it reads the necessary configuration from your tsconfig.json
106114

107115
But if things are not working as expected you can configure esbuild-node-tsc by adding `etsc.config.js` along side tsconfig.json.
108116

109-
> You might need to install `esbuild-plugin-tsc` package too to use the esbuild tsc plugin
110-
111117
Example `etsc.config.js`
112118

113119
```js
114-
const esbuildPluginTsc = require("esbuild-plugin-tsc");
115-
116120
module.exports = {
117-
outDir: "./dist",
121+
// Supports all esbuild.build options
118122
esbuild: {
119123
minify: false,
120124
target: "es2015",
121-
plugins: [esbuildPluginTsc()],
122-
},
123-
assets: {
124-
baseDir: "src",
125-
outDir: "./dist",
126-
filePatterns: ["**/*.json"],
127125
},
128-
};
129-
```
130-
131-
or if you use ESM in your project then use
132-
133-
```js
134-
import esbuildPluginTsc from "esbuild-plugin-tsc";
135-
136-
export default {
137-
outDir: "./dist",
138-
esbuild: {
139-
minify: false,
140-
target: "es2015",
141-
plugins: [esbuildPluginTsc()],
126+
// Prebuild hook
127+
prebuild: async () => {
128+
console.log("prebuild");
129+
const rimraf = (await import("rimraf")).default;
130+
rimraf.sync("./dist"); // clean up dist folder
142131
},
143-
assets: {
144-
baseDir: "src",
145-
outDir: "./dist",
146-
filePatterns: ["**/*.json"],
132+
// Postbuild hook
133+
postbuild: async () => {
134+
console.log("postbuild");
135+
const cpy = (await import("cpy")).default;
136+
await cpy(
137+
[
138+
"src/**/*.graphql", // Copy all .graphql files
139+
"!src/**/*.{tsx,ts,js,jsx}", // Ignore already built files
140+
],
141+
"dist"
142+
);
147143
},
148144
};
149145
```

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "esbuild-node-tsc",
33
"description": "Build your Typescript Node.js projects using blazing fast esbuild",
4-
"version": "1.8.9",
4+
"version": "2.0.0",
55
"main": "./dist/index.js",
66
"bin": {
77
"esbuild-node-tsc": "dist/index.js",
@@ -24,14 +24,11 @@
2424
],
2525
"devDependencies": {
2626
"@types/node": "^18.7.3",
27-
"@types/rimraf": "^3.0.2",
2827
"@types/yargs": "^17.0.11",
2928
"esbuild": "^0.15.2",
3029
"typescript": "^4.7.4"
3130
},
3231
"dependencies": {
33-
"cpy": "^9.0.1",
34-
"rimraf": "^3.0.2",
3532
"yargs": "^17.5.1"
3633
},
3734
"peerDependencies": {

src/config.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import fs from "fs";
2-
import type { BuildOptions as EsBuildOptions, Plugin } from "esbuild";
2+
import type { BuildOptions as EsBuildOptions } from "esbuild";
33

44
export type Config = Partial<{
5-
outDir: string;
6-
clean?: boolean;
75
tsConfigFile?: string;
86
esbuild: EsBuildOptions;
9-
assets: {
10-
baseDir?: string;
11-
outDir?: string;
12-
filePatterns?: string[];
13-
};
7+
prebuild: () => Promise<void>;
8+
postbuild: () => Promise<void>;
149
}>;
1510

1611
export async function readUserConfig(configPath: string): Promise<Config> {
1712
if (fs.existsSync(configPath)) {
1813
try {
1914
const { default: config } = await import(configPath);
15+
if (config.outDir) {
16+
console.warn(
17+
"Your etsc config file is using the old v1.0 format. Please update to the new v2.0 format. Check https://github.com/a7ul/esbuild-node-tsc for details."
18+
);
19+
}
2020
return config;
2121
} catch (e) {
2222
console.log("Config file has some errors:");

src/index.ts

+20-52
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#!/usr/bin/env node
22

33
import ts from "typescript";
4-
import { build, BuildOptions as EsBuildOptions } from "esbuild";
5-
import cpy from "cpy";
4+
import { build } from "esbuild";
65
import path from "path";
7-
import rimraf from "rimraf";
86
import yargs from "yargs/yargs";
97
import { hideBin } from "yargs/helpers";
108
import { Config, readUserConfig } from "./config.js";
@@ -56,82 +54,52 @@ function esBuildSourceMapOptions(tsConfig: TSConfig) {
5654
return sourceMap;
5755
}
5856

59-
function getBuildMetadata(userConfig: Config) {
57+
function getEsbuildMetadata(userConfig: Config) {
6058
const { tsConfig, tsConfigFile } = getTSConfig(userConfig.tsConfigFile);
6159
const esbuildConfig = userConfig.esbuild || {};
6260

63-
const outDir =
64-
userConfig.outDir ||
65-
tsConfig.options.outDir ||
66-
esbuildConfig.outdir ||
67-
"dist";
68-
61+
const outdir = tsConfig.options.outDir || esbuildConfig.outdir || "dist";
6962
const srcFiles = [
7063
...tsConfig.fileNames,
7164
...((esbuildConfig.entryPoints as string[]) ?? []),
7265
];
7366
const sourcemap =
7467
esBuildSourceMapOptions(tsConfig) || userConfig.esbuild?.sourcemap;
75-
const target =
68+
const target: string =
7669
tsConfig?.raw?.compilerOptions?.target || esbuildConfig?.target || "es2015";
77-
const format = esbuildConfig?.format || "cjs";
7870

79-
const esbuildOptions: EsBuildOptions = {
71+
const esbuildOptions = {
8072
...userConfig.esbuild,
81-
outdir: outDir,
73+
outdir,
8274
entryPoints: srcFiles,
8375
sourcemap,
84-
target,
76+
target: target.toLowerCase(),
8577
tsconfig: tsConfigFile,
86-
format,
8778
};
8879

89-
const assetPatterns = userConfig.assets?.filePatterns || ["**"];
80+
return { esbuildOptions };
81+
}
9082

91-
const assetsOptions = {
92-
baseDir: userConfig.assets?.baseDir || "src",
93-
outDir: userConfig.assets?.outDir || outDir,
94-
patterns: [...assetPatterns, "!**/*.{ts,js,tsx,jsx}"],
95-
};
83+
async function main() {
84+
const configFilename = <string>(await argv)?.config || "etsc.config.js";
85+
const config = await readUserConfig(path.resolve(cwd, configFilename));
9686

97-
return { outDir, esbuildOptions, assetsOptions };
98-
}
87+
const { esbuildOptions } = getEsbuildMetadata(config);
9988

100-
async function buildSourceFiles(esbuildOptions: EsBuildOptions) {
101-
return await build({
89+
if (config.prebuild) {
90+
await config.prebuild();
91+
}
92+
93+
await build({
10294
bundle: false,
10395
format: "cjs",
10496
platform: "node",
10597
...esbuildOptions,
10698
});
107-
}
10899

109-
type AssetsOptions = { baseDir: string; outDir: string; patterns: string[] };
110-
111-
async function copyNonSourceFiles({
112-
baseDir,
113-
outDir,
114-
patterns,
115-
}: AssetsOptions) {
116-
const relativeOutDir = path.relative(baseDir, outDir);
117-
return await cpy(patterns, relativeOutDir, { cwd: baseDir });
118-
}
119-
120-
async function main() {
121-
const configFilename = <string>(await argv)?.config || "etsc.config.js";
122-
const clean = (await argv)?.clean;
123-
const config = await readUserConfig(path.resolve(cwd, configFilename));
124-
125-
const { outDir, esbuildOptions, assetsOptions } = getBuildMetadata(config);
126-
127-
if (clean) {
128-
rimraf.sync(outDir);
100+
if (config.postbuild) {
101+
await config.postbuild();
129102
}
130-
131-
await Promise.all([
132-
buildSourceFiles(esbuildOptions),
133-
copyNonSourceFiles(assetsOptions),
134-
]);
135103
}
136104

137105
console.time("Built in");

0 commit comments

Comments
 (0)