1.0.27 • Published 21 days ago

@common-web/esbuild v1.0.27

Weekly downloads
-
License
MIT
Repository
github
Last release
21 days ago

Common esbuild config

Using esbuild to transpile typescript scripts.

Why

Why not use esbuild directly ?

You can use it directly but for typescript projects you’d need additional setup like handling resolution of external node_modules and other options like format, target, platform.

This might be for you if:

  • You are creating scripts or servers on node
  • You need to output cjs
  • You need to target es2015 syntax

This package comes with all the configuration on esbuild out of the box to get up and running without going through the docs and figuring out what options are needed.

Typical uses:

  • You are writing simple node server in typescript you need to transpile to cjs
  • You are writing npm package with utility scripts in typescript you need to transpile to cjs
  • etc...

Requirements

Requires node >= 12.x due to yargs.

Getting started

Install the pkg:

yarn:

yarn add @common-web/esbuild -D

npm:

npm install @common-web/esbuild --save-dev

Running the build

  1. Directly run the cli
// simple build
npx esbuild-ts

// Change path
npx esbuild-ts --entryPoint=./path-to-my-entry --outfile=./path-to-my-outfile

For more options see below Configuration Options

  1. Use the common config file

Creating build.js file:

// build.js

const config = require('@common-web/esbuild');
const esbuild = require('esbuild');
const path = require('path');

// Simple
esbuild.build(config.getBaseConfig())
  .then(() => {
      console.log('Build finished');
  });

// Advanced (optional)
esbuild.build(
    config.getBaseConfig({
      entryPoint: './my-custom-entry-file',
      outfile: './my-custom-out-file',
      plugins: [myCustomPlugin()],
      // By default it imports tsconfig from your root directory
      tsconfig: './path-to-my-ts-config',
      rootDir: path.join(process.cwd(), '../../'),
      target: 'esnext'
    })
).then(() => {
  console.log('Build finished');
});

Add scripts to package.json:

{
  ...,
  "scripts": {
    "build": "node ./build.js"
  }
}

Run the build:

yarn build

Configuration options

namedescriptiondefaultscli configurabledata type
rootDirworking or root directorycurrent working directoryystring
entryPointentry path for the build (relative to rootDir)./src/index.tsystring
outfileoutput path for the build (relative to rootDir)./dist/index.jsystring
formatformat of the build output'cjs'ystring
platformplatform for the build outputnodeystring
targettarget for the build output'es2015'ystring or string[]
pluginsadditional esbuild plugins[]narray
tsconfigpath to tsconfig./tsconfig.jsonystring
overrideadditional override options on esbuild{}nobject
bundlebundle the filesfalseybolean
externalexternal files to exclude from final bundle[]ystring[]

More options:

Caveats

Please review the esbuild documentation for more options and information.

Examples

Bundling example (full node web app)

1.0.27

21 days ago

1.0.25

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.20

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.5

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago