Skip to content

Commit 8f9a3e7

Browse files
committed
feat(build-core): Add additional options to the node-app build
1 parent 78a86a1 commit 8f9a3e7

File tree

8 files changed

+158
-20
lines changed

8 files changed

+158
-20
lines changed

build/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@storm-software/unbuild": "latest"
1717
},
1818
"devDependencies": {
19+
"@storm-software/build-tools": "latest",
1920
"@types/node": "^22.10.2",
2021
"esbuild": "^0.24.0",
2122
"tsup": "^8.3.5",

build/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
*/
2626

2727
export * from "./node-app";
28+
export * from "./types";

build/core/src/node-app/build.ts

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,49 @@
1515
1616
-------------------------------------------------------------------*/
1717

18+
import type { AssetGlob } from "@storm-software/build-tools";
1819
import { build as esbuild } from "@storm-software/esbuild";
1920
import stormStack from "@storm-stack/build-plugin/esbuild";
21+
import { EntryPointsOption } from "../types";
22+
23+
/**
24+
* The options for building a NodeJs application
25+
*/
26+
export interface NodeAppBuildOptions {
27+
/**
28+
* The entry point(s) of the project
29+
*
30+
* @defaultValue ["src/index.ts"]
31+
*/
32+
entryPoints?: EntryPointsOption;
33+
34+
/**
35+
* Should the build be run in development mode
36+
*
37+
* @remarks
38+
* When set to `true`, the build will not be minified and will include sourcemaps
39+
*
40+
* @defaultValue false
41+
*/
42+
debug?: boolean;
43+
44+
/**
45+
* A list of files to include in the build package
46+
*
47+
* @defaultValue []
48+
*/
49+
assets?: (AssetGlob | string)[];
50+
51+
/**
52+
* The output path of the build
53+
*
54+
* @remarks
55+
* The path is relative to the distribution/build directory. In most cases, you should not need to change this.
56+
*
57+
* @defaultValue "dist"
58+
*/
59+
outputPath?: string;
60+
}
2061

2162
/**
2263
* Build a NodeJs application
@@ -26,27 +67,24 @@ import stormStack from "@storm-stack/build-plugin/esbuild";
2667
*/
2768
export const build = async (
2869
projectRoot: string,
29-
entry:
30-
| string
31-
| string[]
32-
| Record<string, string>
33-
| {
34-
in: string;
35-
out: string;
36-
}[] = ["src/index.ts"]
70+
options: NodeAppBuildOptions = {}
3771
) => {
38-
const entryPoints = typeof entry === "string" ? [entry] : entry;
72+
const entryPoints = !options.entryPoints
73+
? ["src/index.ts"]
74+
: typeof options.entryPoints === "string"
75+
? [options.entryPoints]
76+
: options.entryPoints;
3977

4078
await esbuild([
4179
{
4280
entryPoints,
4381
projectRoot,
44-
outdir: "dist",
82+
outdir: options.outputPath || "dist",
4583
platform: "node",
4684
format: "cjs",
4785
bundle: true,
48-
minify: true,
49-
sourcemap: true,
86+
minify: !options.debug,
87+
sourcemap: options.debug,
5088
plugins: [
5189
stormStack({
5290
cache: true
@@ -56,12 +94,12 @@ export const build = async (
5694
{
5795
entryPoints,
5896
projectRoot,
59-
outdir: "dist",
97+
outdir: options.outputPath || "dist",
6098
platform: "node",
6199
format: "esm",
62100
bundle: true,
63-
minify: true,
64-
sourcemap: true,
101+
minify: !options.debug,
102+
sourcemap: options.debug,
65103
plugins: [
66104
stormStack({
67105
cache: true

build/core/src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* The format for providing the application entry point(s) to the build command
3+
*/
4+
export type EntryPointsOption =
5+
| string
6+
| string[]
7+
| Record<string, string>
8+
| {
9+
in: string;
10+
out: string;
11+
}[];

build/nx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@nx/js": "^20.3.0",
5757
"@nx/workspace": "^20.3.0",
5858
"@samchon/openapi": "^2.3.1",
59+
"@storm-software/build-tools": "latest",
5960
"@swc/core": "1.7.26",
6061
"@swc/wasm": "^1.10.1",
6162
"@types/node": "^22.10.2",

build/nx/src/executors/node-app/executor.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import type { ExecutorContext, PromiseExecutor } from "@nx/devkit";
1919
import { withRunExecutor } from "@storm-software/workspace-tools";
20-
import { build } from "@storm-stack/build-core/node/app";
20+
import { build } from "@storm-stack/build-core/node-app";
2121
import type { NodeApplicationExecutorSchema } from "./schema";
2222

2323
export async function executorFn(
@@ -38,7 +38,13 @@ export async function executorFn(
3838
}
3939

4040
await build(
41-
context.projectsConfigurations.projects[context.projectName]?.root!
41+
context.projectsConfigurations.projects[context.projectName]?.root!,
42+
{
43+
entryPoints: options.entryPoints,
44+
outputPath: options.outputPath,
45+
assets: options.assets,
46+
debug: options.debug
47+
}
4248
);
4349

4450
return {
@@ -53,6 +59,11 @@ export default withRunExecutor<NodeApplicationExecutorSchema>(
5359
skipReadingConfig: false,
5460
hooks: {
5561
applyDefaultOptions: (options: NodeApplicationExecutorSchema) => {
62+
options.entryPoints ??= ["{sourceRoot}/index.ts"];
63+
options.outputPath ??= "dist";
64+
options.assets ??= [];
65+
options.debug ??= false;
66+
5667
return options as NodeApplicationExecutorSchema;
5768
}
5869
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
export interface NodeApplicationExecutorSchema {} // eslint-disable-line
1+
import type { EntryPointsOption } from "@storm-stack/build-core";
2+
import type { AssetGlob } from "@storm-stack/build-tools";
3+
4+
export type Platform = "browser" | "neutral" | "node" | "worker";
5+
6+
export interface NodeApplicationExecutorSchema {
7+
entryPoints: EntryPointsOption;
8+
outputPath: string;
9+
assets: (AssetGlob | string)[];
10+
debug?: boolean;
11+
}

build/nx/src/executors/node-app/schema.json

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,71 @@
44
"title": "NodeBuild executor",
55
"description": "",
66
"type": "object",
7-
"properties": {},
8-
"required": []
7+
"properties": {
8+
"entryPoints": {
9+
"type": "array",
10+
"description": "List of entry points.",
11+
"x-priority": "important",
12+
"items": {
13+
"type": "string"
14+
},
15+
"default": ["{sourceRoot}/index.ts"]
16+
},
17+
"outputPath": {
18+
"type": "string",
19+
"description": "The output path of the generated JS files (in the dist directory).",
20+
"x-completion-type": "directory",
21+
"x-priority": "important",
22+
"default": "dist"
23+
},
24+
"assets": {
25+
"type": "array",
26+
"description": "List of static assets.",
27+
"default": [],
28+
"items": {
29+
"$ref": "#/definitions/assetPattern"
30+
}
31+
},
32+
"debug": {
33+
"type": "boolean",
34+
"description": "Should output be unminified with source mappings.",
35+
"default": false
36+
}
37+
},
38+
"required": ["entryPoints", "outputPath", "assets"],
39+
"definitions": {
40+
"assetPattern": {
41+
"oneOf": [
42+
{
43+
"type": "object",
44+
"properties": {
45+
"glob": {
46+
"type": "string",
47+
"description": "The pattern to match."
48+
},
49+
"input": {
50+
"type": "string",
51+
"description": "The input directory path in which to apply `glob`. Defaults to the project root."
52+
},
53+
"output": {
54+
"type": "string",
55+
"description": "Relative path within the output folder."
56+
},
57+
"ignore": {
58+
"description": "An array of globs to ignore.",
59+
"type": "array",
60+
"items": {
61+
"type": "string"
62+
}
63+
}
64+
},
65+
"additionalProperties": false,
66+
"required": ["glob", "input", "output"]
67+
},
68+
{
69+
"type": "string"
70+
}
71+
]
72+
}
73+
}
974
}

0 commit comments

Comments
 (0)