diff --git a/package.json b/package.json index 2d6c10d..d58c5c7 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b4395a..18939a6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -33,7 +33,6 @@ specifiers: pretty-bytes: ^6.0.0 rimraf: ^3.0.2 rollup: ^3.1.0 - rollup-plugin-esbuild: ^4.10.1 scule: ^0.3.2 typescript: ^4.8.4 untyped: ^0.5.0 @@ -64,7 +63,6 @@ dependencies: pretty-bytes: 6.0.0 rimraf: 3.0.2 rollup: 3.1.0 - rollup-plugin-esbuild: 4.10.1_fclsl4c6gsr6cofi4hyljdq5fe scule: 0.3.2 typescript: 4.8.4 untyped: 0.5.0 @@ -1221,10 +1219,6 @@ packages: unbox-primitive: 1.0.2 dev: true - /es-module-lexer/0.9.3: - resolution: {integrity: sha512-1HQ2M2sPtxwnvOvT1ZClHyQDiggdNjURWpY2we6aMKCQiUVxTmVs2UYPLIrD84sS+kMdUwfBSylbJPwNnBrnHQ==} - dev: false - /es-shim-unscopables/1.0.0: resolution: {integrity: sha512-Jm6GPcCdC30eMLbZ2x8z2WuRwAws3zTBBKuusffYVUrNj/GVSUAZ+xKMaUpfNDR5IbyNA5LJbaecoUVbmUcB1w==} dependencies: @@ -2525,11 +2519,6 @@ packages: resolution: {integrity: sha512-L3BJStEf5NAqNuzrpfbN71dp43mYIcBUlCRea/vdyv5dW/AYa1d4bpelko4SHdY3I6eN9Wzyasxirj1/vv5kmg==} hasBin: true - /joycon/3.1.1: - resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==} - engines: {node: '>=10'} - dev: false - /js-sdsl/4.1.4: resolution: {integrity: sha512-Y2/yD55y5jteOAmY50JbUZYwk3CP3wnLPEZnlR1w9oKhITrBEtAxwuWKebFf8hMrPMgbYwFoWK/lH2sBkErELw==} dev: true @@ -3073,24 +3062,6 @@ packages: dependencies: glob: 7.2.3 - /rollup-plugin-esbuild/4.10.1_fclsl4c6gsr6cofi4hyljdq5fe: - resolution: {integrity: sha512-/ymcRB283zjFp1JTBXO8ekxv0c9vRc2L6OTljghsLthQ4vqeDSDWa9BVz1tHiVrx6SbUnUpDPLC0K/MXK7j5TA==} - engines: {node: '>=12'} - peerDependencies: - esbuild: '>=0.10.1' - rollup: ^1.20.0 || ^2.0.0 - dependencies: - '@rollup/pluginutils': 4.2.1 - debug: 4.3.4 - es-module-lexer: 0.9.3 - esbuild: 0.15.10 - joycon: 3.1.1 - jsonc-parser: 3.2.0 - rollup: 3.1.0 - transitivePeerDependencies: - - supports-color - dev: false - /rollup/2.78.1: resolution: {integrity: sha512-VeeCgtGi4P+o9hIg+xz4qQpRl6R401LWEXBmxYKOV4zlF82lyhgh2hTZnheFUbANE8l2A41F458iwj2vEYaXJg==} engines: {node: '>=10.0.0'} diff --git a/src/builder/plugins/esbuild.ts b/src/builder/plugins/esbuild.ts new file mode 100644 index 0000000..4557295 --- /dev/null +++ b/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) + } + } +} diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 67e7b5d..5bbc95b 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -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) { diff --git a/src/types.ts b/src/types.ts index 5c93190..5baf35f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,6 @@ 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' @@ -11,6 +10,7 @@ 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[0] & {} diff --git a/tsconfig.json b/tsconfig.json index 8ebaddb..3d4f70a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "target": "ESNext", "module": "ESNext", "moduleResolution": "Node", + "resolveJsonModule": true, "esModuleInterop": true, "outDir": "dist", "strict": true,