Skip to content

Commit

Permalink
fix(rollup): use inline implementation of esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 15, 2022
1 parent 955a449 commit af41b01
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 35 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -52,7 +52,6 @@
"rimraf": "^3.0.2",
"rollup": "^3.1.0",
"@unjsio/rollup-plugin-dts": "^5.0.0-rc.1",
"rollup-plugin-esbuild": "^4.10.1",
"scule": "^0.3.2",
"typescript": "^4.8.4",
"untyped": "^0.5.0"
Expand Down
29 changes: 0 additions & 29 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions src/builder/plugins/esbuild.ts
@@ -0,0 +1,134 @@
// Based on https://github.com/egoist/rollup-plugin-esbuild and nitropack fork (MIT)

import { extname, relative } from 'pathe'
import type { Plugin, PluginContext } from 'rollup'
import { Loader, TransformResult, transform } from 'esbuild'
import { createFilter } from '@rollup/pluginutils'
import type { FilterPattern } from '@rollup/pluginutils'

const defaultLoaders: { [ext: string]: Loader } = {
'.ts': 'ts',
'.js': 'js'
}

export interface Options {
include?: FilterPattern
exclude?: FilterPattern
sourceMap?: boolean
minify?: boolean
target: string | string[]
jsxFactory?: string
jsxFragment?: string
define?: {
[k: string]: string
}
/**
* Use this tsconfig file instead
* Disable it by setting to `false`
*/
tsconfig?: string | false
/**
* Map extension to esbuild loader
* Note that each entry (the extension) needs to start with a dot
*/
loaders?: {
[ext: string]: Loader | false
}
}

export function esbuild (options: Options): Plugin {
const loaders = {
...defaultLoaders
}

if (options.loaders) {
for (const key of Object.keys(options.loaders)) {
const value = options.loaders[key]
if (typeof value === 'string') {
loaders[key] = value
} else if (value === false) {
delete loaders[key]
}
}
}

const extensions: string[] = Object.keys(loaders)
const INCLUDE_REGEXP = new RegExp(
`\\.(${extensions.map(ext => ext.slice(1)).join('|')})$`
)
const EXCLUDE_REGEXP = /node_modules/

const filter = createFilter(
options.include || INCLUDE_REGEXP,
options.exclude || EXCLUDE_REGEXP
)

return {
name: 'esbuild',

async transform (code, id) {
if (!filter(id)) {
return null
}

const ext = extname(id)
const loader = loaders[ext]

if (!loader) {
return null
}

const result = await transform(code, {
loader,
target: options.target,
define: options.define,
sourcemap: options.sourceMap,
sourcefile: id
})

printWarnings(id, result, this)

return (
result.code && {
code: result.code,
map: result.map || null
}
)
},

async renderChunk (code) {
if (options.minify) {
const result = await transform(code, {
loader: 'js',
minify: true,
target: options.target
})
if (result.code) {
return {
code: result.code,
map: result.map || null
}
}
}
return null
}
}
}

function printWarnings (
id: string,
result: TransformResult,
plugin: PluginContext
) {
if (result.warnings) {
for (const warning of result.warnings) {
let message = '[esbuild]'
if (warning.location) {
message += ` (${relative(process.cwd(), id)}:${warning.location.line}:${warning.location.column
})`
}
message += ` ${warning.text}`
plugin.warn(message)
}
}
}
5 changes: 1 addition & 4 deletions src/builder/rollup.ts
Expand Up @@ -6,21 +6,18 @@ import { rollup } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import alias from '@rollup/plugin-alias'
import _esbuild from 'rollup-plugin-esbuild'
import dts from '@unjsio/rollup-plugin-dts'
import replace from '@rollup/plugin-replace'
import { resolve, dirname, normalize, extname } from 'pathe'
import { resolvePath, resolveModuleExportNames } from 'mlly'
import { getpkg, tryResolve, warn } from '../utils'
import type { BuildContext } from '../types'
import { esbuild } from './plugins/esbuild'
import { JSONPlugin } from './plugins/json'
import { rawPlugin } from './plugins/raw'
import { cjsPlugin } from './plugins/cjs'
import { shebangPlugin, makeExecutable, getShebang } from './plugins/shebang'

// @ts-ignore https://github.com/unjs/unbuild/issues/23
const esbuild = _esbuild.default || _esbuild

const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.mjs', '.cjs', '.js', '.jsx', '.json']

export async function rollupBuild (ctx: BuildContext) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -3,14 +3,14 @@ import type { PackageJson } from 'pkg-types'
import type { Hookable } from 'hookable'
import type { RollupOptions, RollupBuild } from 'rollup'
import type { MkdistOptions } from 'mkdist'
import type { Options as EsbuildOptions } from 'rollup-plugin-esbuild'
import type { Schema } from 'untyped'
import type { RollupReplaceOptions } from '@rollup/plugin-replace'
import type { RollupAliasOptions } from '@rollup/plugin-alias'
import type { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve'
import type { RollupJsonOptions } from '@rollup/plugin-json'
import type { Options as RollupDtsOptions } from '@unjsio/rollup-plugin-dts'
import type commonjs from '@rollup/plugin-commonjs'
import type { Options as EsbuildOptions } from './builder/plugins/esbuild'

export type RollupCommonJSOptions = Parameters<typeof commonjs>[0] & {}

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -3,6 +3,7 @@
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"esModuleInterop": true,
"outDir": "dist",
"strict": true,
Expand Down

0 comments on commit af41b01

Please sign in to comment.