rollup-plugin-esbuild
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "rollup-plugin-esbuild", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "src/index.js", | ||
@@ -5,0 +5,0 @@ "author": "EGOIST", |
# rollup-plugin-esbuild | ||
[esbuild](https://github.com/evanw/esbuild) is by far one of the fastest TS/ESNext to ES6 compilers, so it makes sense to use it over Babel/TSC with Rollup to take advantage of both worlds (Speed and the Rollup plugin ecosytem). | ||
## Install | ||
@@ -11,2 +13,4 @@ | ||
In `rollup.config.js`: | ||
```js | ||
@@ -18,10 +22,15 @@ import esbuild from 'rollup-plugin-esbuild' | ||
esbuild({ | ||
watch: process.env.NODE_ENV === 'development' | ||
}) | ||
] | ||
// All options are optional | ||
watch: process.argv.includes('--watch'), | ||
minify: process.env.NODE_ENV === 'production', | ||
target: 'es2015' // default, or 'es20XX', 'esnext' | ||
}), | ||
], | ||
} | ||
``` | ||
This plugin handles `.js` `.jsx` `.ts` `.tsx` for you. | ||
## License | ||
MIT © [EGOIST (Kevin Titor)](https://github.com/sponsors/egoist) | ||
MIT © [EGOIST (Kevin Titor)](https://github.com/sponsors/egoist) |
@@ -0,1 +1,2 @@ | ||
// @ts-check | ||
const fs = require('fs') | ||
@@ -5,6 +6,9 @@ const { extname, resolve, dirname } = require('path') | ||
/** @typedef {import('esbuild').TransformOptions} TransformOptions */ | ||
/** @type {TransformOptions['loader'][]} */ | ||
const loaders = ['js', 'jsx', 'ts', 'tsx'] | ||
/** @typedef {import('rollup').Plugin} Plugin*/ | ||
/** @typedef {{ watch?: boolean }} Options */ | ||
/** @typedef {import('rollup').Plugin} Plugin */ | ||
/** @typedef {{ watch?: boolean, target?: TransformOptions['target'], minify?: boolean }} Options */ | ||
@@ -23,8 +27,2 @@ /** @type {(options?: Options) => Plugin} */ | ||
buildEnd() { | ||
if (!options.watch) { | ||
service.stop() | ||
} | ||
}, | ||
resolveId(importee, importer) { | ||
@@ -37,11 +35,13 @@ if (importer && importee[0] === '.') { | ||
if (!fs.existsSync(resolved) && fs.existsSync(`${resolved}.jsx`)) { | ||
const exists = fs.existsSync(resolved) | ||
if (!exists && fs.existsSync(`${resolved}.jsx`)) { | ||
return `${resolved}.jsx` | ||
} | ||
if (!fs.existsSync(resolved) && fs.existsSync(`${resolved}.ts`)) { | ||
if (!exists && fs.existsSync(`${resolved}.ts`)) { | ||
return `${resolved}.ts` | ||
} | ||
if (!fs.existsSync(resolved) && fs.existsSync(`${resolved}.tsx`)) { | ||
if (!exists && fs.existsSync(`${resolved}.tsx`)) { | ||
return `${resolved}.tsx` | ||
@@ -53,2 +53,3 @@ } | ||
async transform(code, id) { | ||
/** @type {any} */ | ||
const loader = extname(id).slice(1) | ||
@@ -60,9 +61,8 @@ | ||
const result = await service.transform(code, { loader }) | ||
const result = await service.transform(code, { | ||
loader, | ||
target: options.target || 'es2015', | ||
}) | ||
if (result.warnings) { | ||
for (const warning of result.warnings) { | ||
this.warn(warning.text) | ||
} | ||
} | ||
printWarnings(result, this) | ||
@@ -74,3 +74,39 @@ return { | ||
}, | ||
async renderChunk(code, chunk) { | ||
if (options.minify) { | ||
const result = await service.transform(code, { | ||
loader: 'js', | ||
target: 'esnext', | ||
minify: true, | ||
}) | ||
printWarnings(result, this) | ||
return { | ||
code: result.js, | ||
map: result.jsSourceMap, | ||
} | ||
} | ||
return null | ||
}, | ||
generateBundle() { | ||
if (!options.watch && service) { | ||
service.stop() | ||
service = undefined | ||
} | ||
}, | ||
} | ||
} | ||
/** | ||
* Print esbuild transform warnings | ||
* @param {import('esbuild').TransformResult} result | ||
* @param {import('rollup').PluginContext} plugin | ||
*/ | ||
function printWarnings(result, plugin) { | ||
if (result.warnings) { | ||
for (const warning of result.warnings) { | ||
plugin.warn(warning.text) | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4697
87
35