Skip to content

Commit

Permalink
feat: use rollup-plugin-esbuild by default (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jun 4, 2021
1 parent e952854 commit 448ca4b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .changeset/proud-islands-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pkgr/rollup": major
"@pkgr/utils": patch
---

feat: use rollup-plugin-esbuild by default
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"scripts": {
"build": "run-s build:ts build:r build:w",
"build:r": "ts-node --files packages/rollup/src/cli -d false --exclude webpack-* -p",
"build:r": "ts-node --files packages/rollup/src/cli -d false --exclude webpack-*",
"build:ts": "tsc -b",
"build:w": "rimraf dist && run-p build:w:*",
"build:w:angular": "yarn w:build -e src/angular -o dist/angular -t angular -p",
Expand Down
5 changes: 4 additions & 1 deletion packages/rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@
"@rollup/plugin-json": "^4.0.3",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-replace": "^2.4.2",
"@rollup/plugin-typescript": "^3.1.1",
"@rollup/plugin-url": "^6.0.0",
"@rxts/rollup-plugin-alias": "^0.1.1",
"builtin-modules": "^3.1.0",
"commander": "^7.2.0",
"core-js": "^3.13.1",
"debug": "^4.1.1",
"esbuild": "^0.12.5",
"is-glob": "^4.0.1",
"jsox": "^1.1.121",
"lodash": "^4.17.21",
"micromatch": "^4.0.4",
"rollup": "^2.50.6",
"rollup-plugin-copy": "^3.3.0",
"rollup-plugin-esbuild": "^4.5.0",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-vue": "^6.0.0",
"rollup-plugin-vue-jsx-compat": "^0.0.2",
"tslib": "^2.2.0"
},
"publishConfig": {
Expand Down
12 changes: 9 additions & 3 deletions packages/rollup/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,15 @@ program
JSOX.parse,
)
.option(
'-t, --typescript <JSOX>',
'Overrides the TypeScript compiler options for `@rollup/plugin-typescript`',
'--esbuild <JSOX>',
'Overrides the esbuild options for `rollup-plugin-esbuild`',
JSOX.parse,
)
.option(
'-t, --transformer [babel | esbuild]',
'Specify which transformer to use',
'esbuild',
)
.option('--postcss <JSOX>', 'options for `rollup-plugin-postcss`', JSOX.parse)
.option('--vue <JSOX>', 'options for `rollup-plugin-vue`', JSOX.parse)
.option<boolean | object>(
Expand Down Expand Up @@ -105,7 +110,8 @@ const options = pick(
'aliasEntries',
'sourceMap',
'babel',
'typescript',
'esbuild',
'transformer',
'postcss',
'vue',
'define',
Expand Down
107 changes: 57 additions & 50 deletions packages/rollup/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
__PROD__,
arrayify,
identify,
isTsAvailable,
monorepoPkgs,
tryExtensions,
tryFile,
Expand All @@ -29,7 +28,6 @@ import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import nodeResolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import typescript, { RollupTypescriptOptions } from '@rollup/plugin-typescript'
import url from '@rollup/plugin-url'
import alias, { AliasOptions } from '@rxts/rollup-plugin-alias'
import builtinModules from 'builtin-modules'
Expand All @@ -46,8 +44,10 @@ import {
WarningHandler,
} from 'rollup'
import copy, { CopyOptions } from 'rollup-plugin-copy'
import esbuild, { Options as EsBuildOptions } from 'rollup-plugin-esbuild'
import postcss, { PostCSSPluginConf } from 'rollup-plugin-postcss'
import { Options as TerserOptions, terser } from 'rollup-plugin-terser'
import vueJsx, { Options as VueJsxOptions } from 'rollup-plugin-vue-jsx-compat'

type VuePluginOptions = import('rollup-plugin-vue').Options

Expand Down Expand Up @@ -96,14 +96,7 @@ const resolve = ({ deps, node }: { deps: string[]; node?: boolean }) =>
preferBuiltins: node,
})

const cjs = (sourceMap: boolean) =>
commonjs({
// TODO: add package @pkgr/cjs-ignore ?
// see also: https://github.com/rollup/rollup-plugin-commonjs/issues/244#issuecomment-536168280
// hard-coded temporarily
ignore: ['invariant', 'react-draggable'],
sourceMap,
})
const cjs = (sourceMap: boolean) => commonjs({ sourceMap })

const DEFAULT_FORMATS = ['cjs', 'es2015', 'esm']

Expand Down Expand Up @@ -159,7 +152,8 @@ export interface ConfigOptions {
copies?: CopyOptions | CopyOptions['targets'] | StringMap
sourceMap?: boolean
babel?: RollupBabelInputPluginOptions
typescript?: RollupTypescriptOptions
esbuild?: EsBuildOptions
transformer?: 'babel' | 'esbuild'
postcss?: Readonly<PostCSSPluginConf>
vue?: VuePluginOptions
define?: Record<string, string> | boolean
Expand Down Expand Up @@ -198,7 +192,8 @@ export const config = ({
copies = [],
sourceMap = false,
babel: babelOptions,
typescript: typescriptOptions,
esbuild: esbuildOptions = {},
transformer = 'esbuild',
postcss: postcssOptions = {},
vue: vueOptions,
define,
Expand Down Expand Up @@ -303,7 +298,6 @@ ConfigOptions = {}): RollupOptions[] => {
...(node ? [...deps, ...builtinModules] : []),
]

const isTsInput = /\.tsx?/.test(pkgInput)
const pkgFormats =
formats && formats.length > 0
? formats
Expand All @@ -319,15 +313,30 @@ ConfigOptions = {}): RollupOptions[] => {
let defineValues: Record<string, string> | undefined

if (define) {
defineValues = Object.entries(define).reduce(
defineValues = Object.entries(define).reduce<Record<string, string>>(
(acc, [key, value]: [string, string]) =>
Object.assign(acc, {
[key]: JSON.stringify(value),
}),
{},
// __DEV__ and __PROD__ will always be replaced while `process.env.NODE_ENV` will be preserved except on production
prod
? {
__DEV__: JSON.stringify(false),
__PROD__: JSON.stringify(true),
'process.env.NODE_ENV': JSON.stringify(PROD),
}
: {
__DEV__: JSON.stringify(__DEV__),
__PROD__: JSON.stringify(__PROD__),
},
)
}

const useEsBuild = transformer === 'esbuild'
const { jsxFactory } = esbuildOptions
const esbuildVueJsx =
useEsBuild && vue && (!jsxFactory || jsxFactory === 'vueJsxCompat')

return pkgFormats.map(format => {
const isEsVersion = /^es(\d+|next)$/.test(format) && format !== 'es5'
return {
Expand Down Expand Up @@ -359,17 +368,25 @@ ConfigOptions = {}): RollupOptions[] => {
onwarn,
plugins: [
alias(aliasOptions),
isTsAvailable && isTsInput
? typescript({
jsx: 'react',
module: 'esnext',
esbuildVueJsx && (vueJsx as (options?: VueJsxOptions) => Plugin)(),
useEsBuild
? esbuild({
jsxFactory: esbuildVueJsx ? 'vueJsxCompat' : undefined,
tsconfig:
// FIXME: should prefer next one
tryFile('tsconfig.base.json') ||
tryFile(path.resolve(pkg, 'tsconfig.json')) ||
tryFile('tsconfig.base.json') ||
tryPkg('@1stg/tsconfig'),
...typescriptOptions,
target: isEsVersion ? format : 'es5',
define: defineValues,
minify: prod,
loaders: {
'.js': 'jsx',
},
...esbuildOptions,
/**
* es5 is not supported temporarily
* @see https://github.com/evanw/esbuild/issues/297
*/
target: isEsVersion ? format : 'es6',
sourceMap,
})
: babel({
Expand All @@ -378,16 +395,21 @@ ConfigOptions = {}): RollupOptions[] => {
presets: [
[
'@babel/env',
isEsVersion
? {
targets: {
esmodules: true,
},
}
: undefined,
{
bugfixes: true,
corejs: '3.13',
shippedProposals: true,
useBuiltIns: 'usage',
targets: isEsVersion ? { esmodules: true } : undefined,
},
],
],
plugins: [
[
'@babel/transform-runtime',
{ corejs: { proposals: true, version: 3 } },
],
],
plugins: ['@babel/transform-runtime'],
...babelOptions,
}),
resolve({
Expand All @@ -401,25 +423,10 @@ ConfigOptions = {}): RollupOptions[] => {
postcss(postcssOptions),
...[
vue?.(vueOptions),
// __DEV__ and __PROD__ will always be replaced while `process.env.NODE_ENV` will be preserved except on production
define &&
replace(
prod
? {
...defineValues,
__DEV__: JSON.stringify(false),
__PROD__: JSON.stringify(true),
'process.env.NODE_ENV': JSON.stringify(PROD),
}
: {
...defineValues,
__DEV__: JSON.stringify(__DEV__),
__PROD__: JSON.stringify(__PROD__),
},
),
prod && terser(terserOptions),
].filter(identify),
],
!useEsBuild && replace(defineValues),
prod && !useEsBuild && terser(terserOptions),
],
].filter(identify),
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const DEV = 'development' as const
export const PROD = 'production' as const

export const { NODE_ENV = DEV } = process.env
export const NODE_ENV = process.env.NODE_ENV ?? DEV

export const __DEV__ = NODE_ENV === DEV
export const __PROD__ = NODE_ENV === PROD
Expand Down
51 changes: 41 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2339,14 +2339,6 @@
"@rollup/pluginutils" "^3.1.0"
magic-string "^0.25.7"

"@rollup/plugin-typescript@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-3.1.1.tgz#a39175a552ed82a3e424862e6bb403bf9da451ee"
integrity sha512-VPY1MbzIJT+obpav9Kns4MlipVJ1FuefwzO4s1uCVXAzVWya+bhhNauOmmqR/hy1zj7tePfh3t9iBN+HbIzyRA==
dependencies:
"@rollup/pluginutils" "^3.0.1"
resolve "^1.14.1"

"@rollup/plugin-url@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-url/-/plugin-url-6.0.0.tgz#20fbc8cda9bc5f0c31d7225f633002ce73e920b0"
Expand All @@ -2356,7 +2348,7 @@
make-dir "^3.1.0"
mime "^2.4.6"

"@rollup/pluginutils@^3.0.1", "@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0":
"@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==
Expand All @@ -2365,6 +2357,14 @@
estree-walker "^1.0.1"
picomatch "^2.2.2"

"@rollup/pluginutils@^4.0.0", "@rollup/pluginutils@^4.1.0":
version "4.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-4.1.0.tgz#0dcc61c780e39257554feb7f77207dceca13c838"
integrity sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==
dependencies:
estree-walker "^2.0.1"
picomatch "^2.2.2"

"@rxts/rollup-plugin-alias@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@rxts/rollup-plugin-alias/-/rollup-plugin-alias-0.1.1.tgz#1ec45b5bb80a84d189606cc59c89528259b866b7"
Expand Down Expand Up @@ -6206,6 +6206,11 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

esbuild@^0.12.5:
version "0.12.5"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.5.tgz#36076a6bc1966ba2741981d30512e95e8aaff495"
integrity sha512-vcuP53pA5XiwUU4FnlXM+2PnVjTfHGthM7uP1gtp+9yfheGvFFbq/KyuESThmtoHPUrfZH5JpxGVJIFDVD1Egw==

escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
Expand Down Expand Up @@ -9319,6 +9324,11 @@ jest@^27.0.1:
import-local "^3.0.2"
jest-cli "^27.0.3"

joycon@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/joycon/-/joycon-3.0.1.tgz#9074c9b08ccf37a6726ff74a18485f85efcaddaf"
integrity sha512-SJcJNBg32dGgxhPtM0wQqxqV0ax9k/9TaUskGDSJkSFSQOEWWvQ3zzWdGQRIUry2j1zA5+ReH13t0Mf3StuVZA==

jpegtran-bin@^5.0.0:
version "5.0.2"
resolved "https://registry.yarnpkg.com/jpegtran-bin/-/jpegtran-bin-5.0.2.tgz#5870fd7e68317bd203a1c94572bd06ae7732cac3"
Expand Down Expand Up @@ -9448,6 +9458,11 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"

jsonc-parser@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-3.0.0.tgz#abdd785701c7e7eaca8a9ec8cf070ca51a745a22"
integrity sha512-fQzRfAbIBnR0IQvftw9FJveWiHp72Fg20giDrHz6TdfB12UH/uue0D3hm57UB5KgAVuniLMCaS8P1IMj9NR7cA==

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down Expand Up @@ -13771,7 +13786,7 @@ resolve-url@^0.2.1:
resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a"
integrity sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo=

resolve@^1.1.7, resolve@^1.10.0, resolve@^1.10.1, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.14.1, resolve@^1.14.2, resolve@^1.15.1, resolve@^1.17.0, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2:
resolve@^1.1.7, resolve@^1.10.0, resolve@^1.10.1, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.14.2, resolve@^1.15.1, resolve@^1.17.0, resolve@^1.19.0, resolve@^1.20.0, resolve@^1.3.2:
version "1.20.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A==
Expand Down Expand Up @@ -13852,6 +13867,15 @@ rollup-plugin-copy@^3.3.0:
globby "10.0.1"
is-plain-object "^3.0.0"

rollup-plugin-esbuild@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-esbuild/-/rollup-plugin-esbuild-4.5.0.tgz#0fbcb6d2d651d87dc540c4fc3b2db669f48da1f0"
integrity sha512-ieUd3AoYWsN6Tfp0LBNnC+QpdhKjDEaH4NK3ghuEXOH56/7TAtD+hMbD9vSWZgsGSbaqCkrn4j6PaUj1vOSt1g==
dependencies:
"@rollup/pluginutils" "^4.1.0"
joycon "^3.0.1"
jsonc-parser "^3.0.0"

rollup-plugin-postcss@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-postcss/-/rollup-plugin-postcss-4.0.0.tgz#2131fb6db0d5dce01a37235e4f6ad4523c681cea"
Expand Down Expand Up @@ -13881,6 +13905,13 @@ rollup-plugin-terser@^7.0.0:
serialize-javascript "^4.0.0"
terser "^5.0.0"

rollup-plugin-vue-jsx-compat@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/rollup-plugin-vue-jsx-compat/-/rollup-plugin-vue-jsx-compat-0.0.2.tgz#7ba97ae45aa204eb9ed7127b1b11b76ee9851234"
integrity sha512-Ina71+whxM3eoKJZcN8c8Yfk5HfVQCNOrN6ZbqPJTKNc1HL2AeEeUC9yybaRV5e6Nk/8Bmtan/AnvNIA/J9v8g==
dependencies:
"@rollup/pluginutils" "^4.0.0"

rollup-plugin-vue@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-vue/-/rollup-plugin-vue-6.0.0.tgz#e379e93e5ae9a8648522f698be2e452e8672aaf2"
Expand Down

0 comments on commit 448ca4b

Please sign in to comment.