Skip to content

Commit

Permalink
feat(nano-build): microservice preset (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd authored Dec 23, 2023
2 parents 2691697 + 3f662b8 commit bbdf37f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
21 changes: 18 additions & 3 deletions packages/nano-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Add the following scripts to your `package.json`:
entryPoints: ['src/main.ts'],
outdir: 'dist',
logLevel: 'info',
platform: 'node',
target: 'es2020',
minify: true,
treeShaking: false,
Expand All @@ -55,6 +54,7 @@ Add the following scripts to your `package.json`:
```js
{
...defaultPreset,
platform: 'node',
format: 'esm',
cjs: true,
mangleProps: '_$',
Expand All @@ -67,8 +67,8 @@ Add the following scripts to your `package.json`:
```js
{
...defaultPreset,
format: 'iife',
platform: 'browser',
format: 'iife',
mangleProps: '_$',
treeShaking: true,
sourcemap: false,
Expand All @@ -90,8 +90,8 @@ Add the following scripts to your `package.json`:
...defaultPreset,
entryPoints: ['site/_ts/*.ts'],
outdir: 'dist/es',
format: 'iife',
platform: 'browser',
format: 'iife',
mangleProps: '_$',
treeShaking: true,
sourcemap: false,
Expand All @@ -106,6 +106,21 @@ Add the following scripts to your `package.json`:
}
```

### `--preset=microservice`

```js
{
...defaultPreset,
platform: 'node',
format: 'esm',
treeShaking: true,
mangleProps: '_$',
sourcemap: false,
sourcesContent: false,
target: 'node20',
}
```

## Configuration

Add 'nano-build' field to your `package.json` for overwriting configuration:
Expand Down
25 changes: 19 additions & 6 deletions packages/nano-build/nano-build.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const defaultOptions = {
entryPoints: ['src/main.ts'],
outdir: 'dist',
logLevel: 'info',
platform: 'node',
target: 'es2020',
minify: true,
treeShaking: false,
Expand All @@ -42,16 +41,17 @@ const defaultOptions = {
};

const presetRecord = {
default: defaultOptions,
default: {},
module: {
platform: 'node',
format: 'esm',
cjs: true,
mangleProps: '_$',
packages: 'external',
},
pwa: {
format: 'iife',
platform: 'browser',
format: 'iife',
mangleProps: '_$',
treeShaking: true,
sourcemap: false,
Expand All @@ -67,8 +67,8 @@ const presetRecord = {
pmpa: {
entryPoints: ['site/_ts/*.ts'],
outdir: 'dist/es',
format: 'iife',
platform: 'browser',
format: 'iife',
mangleProps: '_$',
treeShaking: true,
sourcemap: false,
Expand All @@ -81,6 +81,15 @@ const presetRecord = {
'safari11',
],
},
microservice: {
platform: 'node',
format: 'esm',
treeShaking: true,
mangleProps: '_$',
sourcemap: false,
sourcesContent: false,
target: 'node20',
},
};

function getOptions() {
Expand Down Expand Up @@ -115,14 +124,18 @@ function getOptions() {
return options;
}

/**
* Nano build process.
* @param {import('esbuild').BuildOptions} options
*/
async function nanoBuild(options) {
const alsoCjs = options.format === 'esm' && options.cjs;
delete options.cjs;

if (alsoCjs) {
if (options.format === 'esm' || options.format === 'cjs') {
options.outExtension = {
'.js': options.format === 'esm' ? '.mjs' : '.cjs',
...options.outExtension,
'.js': '.mjs',
};
}

Expand Down

0 comments on commit bbdf37f

Please sign in to comment.