/
handleModuleJs.ts
59 lines (56 loc) · 1.43 KB
/
handleModuleJs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createRequire } from 'node:module'
import type Config from 'webpack-chain'
import type { WebpackBundlerOptions } from '../types.js'
import { resolveEsbuildJsxOptions } from './resolveEsbuildJsxOptions.js'
const require = createRequire(import.meta.url)
/**
* Set webpack module to handle js files
*/
export const handleModuleJs = ({
options,
config,
isBuild,
isServer,
}: {
options: WebpackBundlerOptions
config: Config
isBuild: boolean
isServer: boolean
}): void => {
// only enable transpilation in production client bundle
// when `evergreen` option is set to `false`
if (options.evergreen !== false || !isBuild || isServer) {
return
}
config.module
.rule('js')
.test(/\.jsx?$/)
.exclude.add((filePath) => {
// always transpile js / jsx in vue files
if (/\.vue\.jsx?$/.test(filePath)) {
return false
}
// transpile all core packages and vuepress related packages.
// i.e.
// @vuepress/*
// vuepress-*
if (
/(@vuepress[/\\][^/\\]*|vuepress-[^/\\]*)[/\\](?!node_modules).*\.js$/.test(
filePath
)
) {
return false
}
// don't transpile node_modules
return /node_modules/.test(filePath)
})
.end()
// use esbuild-loader
.use('esbuild-loader')
.loader(require.resolve('esbuild-loader'))
.options({
target: 'es2015',
...resolveEsbuildJsxOptions(),
})
.end()
}