-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.ts
157 lines (153 loc) · 4.25 KB
/
webpack.config.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// call like: yarn webpack build --mode production --entry ./lib/hashids.ts --env moduleTarget=esm --env engineTarget=web --env outDir=dist/umd
import path from 'path'
import { Compilation } from 'webpack'
import type { Configuration, Compiler } from 'webpack'
import { ReplaceSource, Source } from 'webpack-sources'
type ExternalFn = Extract<
NonNullable<Configuration['externals']>,
(data: any) => Promise<any>
>
class PostProcessChunkWebpackPlugin {
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(
'PostProcessChunkWebpackPlugin',
(compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'PostProcessChunkWebpackPlugin',
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
},
(assetRecord) => {
const jsAssets = Object.entries(
assetRecord as Record<string, Source>,
).filter(([name]) => name.endsWith('.js'))
const mappedAssets = jsAssets.map(([name, source]) => {
const newSource = new ReplaceSource(source)
const contents = source.source().toString()
const regexp = /__webpack_require__/g
const matches = contents.matchAll(regexp)
for (const match of matches) {
const index = match.index!
const length = '__webpack_require__'.length
if (length) {
newSource.replace(
index,
index + length - 1,
'__interna_require__',
)
}
}
return [name, newSource]
})
Object.assign(assetRecord, Object.fromEntries(mappedAssets))
},
)
},
)
}
}
// eslint-disable-next-line import/no-default-export
export default ({
moduleTarget = 'esm',
codeTarget = 'es2020',
engineTarget = 'web',
filename = 'main.js',
outDir = moduleTarget,
}: {
moduleTarget: string
codeTarget?: string
engineTarget?: string
filename?: string
outDir?: string
}): Configuration => {
return {
target: [codeTarget, engineTarget],
experiments: {
outputModule: true,
// futureDefaults: true,
// topLevelAwait: true,
// css: false,
},
resolve: {
extensions: ['.ts', '.tsx', '...'],
alias: {
'#image_overlay': path.join(__dirname, 'src/dummy.png'),
react: path.join(__dirname, 'src/reactInterop.js'),
'react-dom': path.join(__dirname, 'src/reactDomInterop.js'),
'external-react': 'react',
'external-react-dom': 'react-dom',
},
},
mode: 'production',
optimization: {
concatenateModules: true,
},
// entry: {
// main: {
// import: './src/main',
// // this should work, but doesn't for some reason:
// baseUri: 'data:',
// },
// },
output: {
module: true,
library: {
type: 'module',
},
chunkFormat: 'module',
filename,
path: path.join(process.cwd(), outDir),
publicPath: '',
importMetaName: `({url: 'https://_'})`,
},
devtool: 'source-map',
plugins: [new PostProcessChunkWebpackPlugin()],
module: {
rules: [
{
test: /\.tsx?$/i,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
compilerOptions: {
module: 'esnext',
target: 'es2015',
esModuleInterop: false,
sourceMap: true,
},
},
},
],
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.svg$/i,
type: 'asset/inline',
},
],
},
ignoreWarnings: [
{
module: /node_modules\/lodash\/_freeGlobal\.js$/,
},
],
externals: [
(async (data) => {
const { request } = data
if (request === 'external-react') {
return 'react'
}
if (request === 'external-react-dom') {
return 'react-dom'
}
return false
}) as ExternalFn,
],
externalsType: 'module',
}
}