-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack.config.js
59 lines (54 loc) · 1.29 KB
/
webpack.config.js
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
const Dotenv = require('dotenv-webpack')
const path = require('path')
const fs = require('fs')
module.exports = {
mode: 'production',
entry: getEntryPoints('./workers'),
output: {
path: path.resolve('./public/workers'),
filename: '[name].js',
libraryTarget: 'global',
},
module: {
rules: [
{
test: /\.worker\.js?$/,
exclude: /(node_modules)/,
use: 'babel-loader',
},
],
},
resolve: {
extensions: ['.worker.js', '.js', '.d.ts'],
fallback: {
path: require.resolve('path-browserify'),
},
},
plugins: [
new Dotenv({
path: './.env.local',
}),
],
}
function getEntryPoints(directory) {
const entryPoints = {}
const files = fs.readdirSync(directory)
files.forEach((file) => {
const name = path.parse(file).name
const entryName = name.replace(/\.worker$/, '') // Remove the '.worker' extension
const libraryOptions = generateLibraryOptions(entryName)
entryPoints[entryName] = {
import: path.resolve(directory, file),
library: libraryOptions,
}
})
return entryPoints
}
function generateLibraryOptions(entryName) {
const sanitized = entryName.replace(/[^a-zA-Z0-9_]/g, '_')
const libraryName = `lib_${sanitized}`
return {
type: 'var',
name: libraryName,
}
}