2021-12-20 12:00:27 +00:00
|
|
|
import esbuildLoader from 'esbuild-loader'
|
2020-09-02 12:27:27 +00:00
|
|
|
import { WebpackConfigContext } from '../utils/config'
|
|
|
|
|
|
|
|
export function esbuild (ctx: WebpackConfigContext) {
|
|
|
|
const { config } = ctx
|
|
|
|
|
2020-10-30 11:55:17 +00:00
|
|
|
// https://esbuild.github.io/getting-started/#bundling-for-the-browser
|
|
|
|
// https://gs.statcounter.com/browser-version-market-share
|
|
|
|
// https://nodejs.org/en/
|
2021-09-05 20:33:24 +00:00
|
|
|
const target = ctx.isServer ? 'es2019' : 'chrome85'
|
2020-10-30 11:55:17 +00:00
|
|
|
|
2021-12-20 12:00:27 +00:00
|
|
|
// https://github.com/nuxt/framework/issues/2372
|
|
|
|
config.optimization.minimizer.push(new (esbuildLoader as unknown as typeof import('esbuild-loader')).ESBuildMinifyPlugin())
|
2020-09-02 12:27:27 +00:00
|
|
|
|
|
|
|
config.module.rules.push(
|
|
|
|
{
|
2021-10-14 13:21:55 +00:00
|
|
|
test: /\.m?[jt]s$/i,
|
2020-09-02 12:27:27 +00:00
|
|
|
loader: 'esbuild-loader',
|
|
|
|
exclude: (file) => {
|
|
|
|
file = file.split('node_modules', 2)[1]
|
|
|
|
|
|
|
|
// Not exclude files outside node_modules
|
|
|
|
if (!file) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Item in transpile can be string or regex object
|
|
|
|
return !ctx.transpile.some(module => module.test(file))
|
|
|
|
},
|
2021-04-21 15:10:53 +00:00
|
|
|
resolve: {
|
|
|
|
fullySpecified: false
|
|
|
|
},
|
2020-09-02 12:27:27 +00:00
|
|
|
options: {
|
|
|
|
loader: 'ts',
|
2020-10-30 11:55:17 +00:00
|
|
|
target
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2021-10-14 13:21:55 +00:00
|
|
|
test: /\.m?[jt]sx$/,
|
2020-09-02 12:27:27 +00:00
|
|
|
loader: 'esbuild-loader',
|
|
|
|
options: {
|
|
|
|
loader: 'tsx',
|
2020-10-30 11:55:17 +00:00
|
|
|
target
|
2020-09-02 12:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|