Skip to content

Commit 91bdf90

Browse files
committed
refactor(nano-build): Update all presets options
BREAKING CHANGE: heads up all presets options changed, please double check the readme and your build output.
1 parent a000d38 commit 91bdf90

File tree

2 files changed

+81
-64
lines changed

2 files changed

+81
-64
lines changed

packages/nano-build/README.md

+49-22
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,27 @@ Add 'nano-build' field to your `package.json` for overwriting configuration:
7474

7575
## Presets
7676

77+
Presets are predefined configurations that can be used to build your project. You can use the `--preset` flag to specify a preset.
78+
79+
```bash
80+
yarn run build --preset=module
81+
```
82+
7783
### default
7884

7985
```js
8086
{
81-
entryPoints: ['src/main.ts'],
87+
entryPoints: ['src/*.ts'],
8288
outdir: 'dist',
8389
logLevel: 'info',
8490
target: 'es2020',
8591
minify: true,
86-
treeShaking: false,
87-
sourcemap: true,
88-
sourcesContent: true,
92+
treeShaking: true,
93+
sourcemap: false,
94+
sourcesContent: false,
8995
bundle: true,
90-
splitting: false,
9196
charset: 'utf8',
92-
legalComments: 'none',
97+
legalComments: 'linked',
9398
define: {
9499
__package_name__: packageJson.name,
95100
__package_version__: packageJson.version,
@@ -103,7 +108,7 @@ Add 'nano-build' field to your `package.json` for overwriting configuration:
103108

104109
### `--preset=module`
105110

106-
Builds and bundle a single entry point.
111+
Builds and bundle for single export module.
107112

108113
```js
109114
{
@@ -112,14 +117,19 @@ Builds and bundle a single entry point.
112117
bundle: true,
113118
platform: 'node',
114119
format: 'esm',
120+
minify: false,
115121
cjs: true,
116122
packages: 'external',
123+
sourcemap: true,
124+
sourcesContent: true
117125
}
118126
```
119127

128+
Note: default production overwrite options not applied.
129+
120130
### `--preset=module2`
121131

122-
Builds and bundles multiple entry points in root of `src` directory.
132+
Builds and bundles multiple entry points in root of `src` directory for multiple exports module.
123133

124134
```js
125135
{
@@ -128,44 +138,55 @@ Builds and bundles multiple entry points in root of `src` directory.
128138
bundle: true,
129139
platform: 'node',
130140
format: 'esm',
141+
minify: false,
131142
cjs: true,
132143
packages: 'external',
144+
sourcemap: true,
145+
sourcesContent: true
133146
}
134147
```
135148

149+
Note: default production overwrite options not applied.
150+
136151
### `--preset=module3`
137152

138-
Builds multiple entry points in `src` directory without bundling.
153+
Builds multiple entry points in `src` directory for multiple exports module without bundling.
139154

140155
```js
141156
{
157+
...defaultPreset,
142158
entryPoints: ['src/**/*.ts'],
143159
bundle: false,
144160
platform: 'node',
145161
format: 'esm',
162+
minify: false,
146163
cjs: true,
147164
packages: 'external',
165+
sourcemap: true,
166+
sourcesContent: true
148167
}
149168
```
150169

170+
Note: default production overwrite options not applied.
171+
151172
### `--preset=pwa`
152173

153174
```js
154175
{
155176
...defaultPreset,
177+
entryPoints: ['site/_ts/*.ts'],
178+
outdir: 'dist/es',
156179
platform: 'browser',
157180
format: 'iife',
158181
mangleProps: '_$',
159-
treeShaking: true,
160-
sourcemap: false,
161-
sourcesContent: false,
162182
target: [
163183
'es2018',
164184
'chrome62',
165185
'edge79',
166186
'firefox78',
167187
'safari11',
168188
],
189+
...(devMode ? developmentOverwriteOptions : productionOverwriteOptions),
169190
}
170191
```
171192

@@ -179,16 +200,14 @@ Builds multiple entry points in `src` directory without bundling.
179200
platform: 'browser',
180201
format: 'iife',
181202
mangleProps: '_$',
182-
treeShaking: true,
183-
sourcemap: false,
184-
sourcesContent: false,
185203
target: [
186204
'es2018',
187205
'chrome62',
188206
'edge79',
189207
'firefox78',
190208
'safari11',
191209
],
210+
...(devMode ? developmentOverwriteOptions : productionOverwriteOptions),
192211
}
193212
```
194213

@@ -197,13 +216,12 @@ Builds multiple entry points in `src` directory without bundling.
197216
```js
198217
{
199218
...defaultPreset,
219+
entryPoints: ['src/ts/main.ts'],
200220
platform: 'node',
201221
format: 'esm',
202-
treeShaking: true,
203222
mangleProps: '_$',
204-
sourcemap: false,
205-
sourcesContent: false,
206223
target: 'node20',
224+
...(devMode ? developmentOverwriteOptions : productionOverwriteOptions),
207225
}
208226
```
209227

@@ -217,16 +235,14 @@ Builds multiple entry points in `src` directory without bundling.
217235
platform: 'browser',
218236
format: 'iife',
219237
mangleProps: '_$',
220-
treeShaking: true,
221-
sourcemap: false,
222-
sourcesContent: false,
223238
target: [
224239
'es2018',
225240
'chrome62',
226241
'edge79',
227242
'firefox78',
228243
'safari11',
229244
],
245+
...(devMode ? developmentOverwriteOptions : productionOverwriteOptions),
230246
}
231247
```
232248

@@ -238,12 +254,23 @@ This preset is used when `NODE_ENV` is not set to `production`. It overwrites al
238254
{
239255
sourcemap: true,
240256
sourcesContent: true,
241-
dropLabels: ['__dev_mode__'],
242257
}
243258
```
244259

245260
you can also add `nano-build-development` field to your `package.json` for overwriting configuration.
246261

262+
### Production overwrite
263+
264+
This preset is used when `NODE_ENV` is set to `production`. It overwrites all other presets.
265+
266+
```js
267+
{
268+
dropLabels: ['__dev_mode__'];
269+
}
270+
```
271+
272+
you can also add `nano-build-production` field to your `package.json` for overwriting configuration.
273+
247274
## Sponsors
248275

249276
The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.

packages/nano-build/nano-build.cjs

+32-42
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,29 @@ if (existsSync(packageJsonPath) === false) {
99
}
1010
const packageJson = require(packageJsonPath);
1111

12-
console.log('🚀 nano-build');
13-
console.log('📦 %s\n', packageJson.name);
12+
console.log('\n🚀 nano-build 📦 %s\n', packageJson.nam);
1413

1514
const devMode = process.env.NODE_ENV !== 'production';
1615

17-
if (devMode) {
18-
console.log('🔧 Development mode');
19-
} else {
20-
console.log('🔧 Production mode');
21-
}
16+
console.log(`🔧 ${devMode ? 'Development' : 'Production'} mode`);
2217

2318
const watchMode = process.argv.includes('--watch');
2419

2520
/**
2621
* @type {import('esbuild').BuildOptions}
2722
*/
2823
const defaultOptions = {
29-
entryPoints: ['src/main.ts'],
24+
entryPoints: ['src/*.ts'],
3025
outdir: 'dist',
3126
logLevel: 'info',
3227
target: 'es2020',
33-
minify: true,
34-
treeShaking: false,
35-
sourcemap: true,
36-
sourcesContent: true,
3728
bundle: true,
38-
splitting: false,
29+
minify: true,
30+
treeShaking: true,
31+
sourcemap: false,
32+
sourcesContent: false,
3933
charset: 'utf8',
40-
legalComments: 'none',
34+
legalComments: 'linked',
4135
banner: {
4236
js: '/* ' + packageJson.name + ' v' + packageJson.version + ' */',
4337
},
@@ -54,11 +48,17 @@ const defaultOptions = {
5448
const developmentOptions = {
5549
sourcemap: true,
5650
sourcesContent: true,
51+
};
52+
53+
/**
54+
* @type {import('esbuild').BuildOptions}
55+
*/
56+
const productionOptions = {
5757
dropLabels: ['__dev_mode__'],
58-
}
58+
};
5959

6060
/**
61-
* @type {DictionaryOpt<import('esbuild').BuildOptions & {cjs?: true}>}
61+
* @type {DictionaryOpt<import('esbuild').BuildOptions & {cjs?: boolean}>}
6262
*/
6363
const presetRecord = {
6464
default: {},
@@ -67,64 +67,67 @@ const presetRecord = {
6767
bundle: true,
6868
platform: 'node',
6969
format: 'esm',
70+
minify: false,
7071
cjs: true,
7172
packages: 'external',
73+
sourcemap: true,
74+
sourcesContent: true,
7275
},
7376
module2: {
7477
entryPoints: ['src/*.ts'],
7578
bundle: true,
7679
platform: 'node',
7780
format: 'esm',
81+
minify: false,
7882
cjs: true,
7983
packages: 'external',
84+
sourcemap: true,
85+
sourcesContent: true,
8086
},
8187
module3: {
8288
entryPoints: ['src/**/*.ts'],
8389
bundle: false,
8490
platform: 'node',
8591
format: 'esm',
92+
minify: false,
8693
cjs: true,
8794
packages: 'external',
95+
sourcemap: true,
96+
sourcesContent: true,
8897
},
8998
pwa: {
99+
entryPoints: ['src/*.ts'],
90100
platform: 'browser',
91101
format: 'iife',
92102
mangleProps: '_$',
93-
treeShaking: true,
94-
sourcemap: false,
95-
sourcesContent: false,
96103
target: ['es2018', 'chrome62', 'edge79', 'firefox78', 'safari11'],
104+
...(devMode ? developmentOptions : productionOptions),
97105
},
98106
pmpa: {
99107
entryPoints: ['site/_ts/*.ts'],
100108
outdir: 'dist/es',
101109
platform: 'browser',
102110
format: 'iife',
103111
mangleProps: '_$',
104-
treeShaking: true,
105-
sourcemap: false,
106-
sourcesContent: false,
107112
target: ['es2018', 'chrome62', 'edge79', 'firefox78', 'safari11'],
113+
...(devMode ? developmentOptions : productionOptions),
108114
},
109115
weaver: {
110116
entryPoints: ['src/ts/*.ts'],
111117
outdir: 'dist/es',
112118
platform: 'browser',
113119
format: 'iife',
114120
mangleProps: '_$',
115-
treeShaking: true,
116-
sourcemap: false,
117-
sourcesContent: false,
118121
target: ['es2018', 'chrome62', 'edge79', 'firefox78', 'safari11'],
122+
...(devMode ? developmentOptions : productionOptions),
119123
},
120124
microservice: {
125+
entryPoints: ['src/ts/main.ts'],
121126
platform: 'node',
122127
format: 'esm',
123-
treeShaking: true,
124128
mangleProps: '_$',
125-
sourcemap: false,
126-
sourcesContent: false,
127129
target: 'node20',
130+
...(devMode ? developmentOptions : productionOptions),
128131
},
129132
};
130133

@@ -142,22 +145,9 @@ function getOptions() {
142145
...defaultOptions,
143146
...presetOptions,
144147
...packageJson['nano-build'],
145-
148+
...(devMode ? packageJson['nano-build-development'] : packageJson['nano-build-production']),
146149
};
147150

148-
if (devMode) {
149-
options = {
150-
...options,
151-
...developmentOptions,
152-
...packageJson['nano-build-development'],
153-
}
154-
} else {
155-
options = {
156-
...options,
157-
...packageJson['nano-build-production'],
158-
}
159-
}
160-
161151
// Remove null fields from esbuildOptions
162152
Object.keys(options).forEach((key) => {
163153
if (options[key] === null) {

0 commit comments

Comments
 (0)