Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
136 lines (124 loc) · 3.54 KB
/
Copy pathwebpack.config.js
File metadata and controls
136 lines (124 loc) · 3.54 KB
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
const { resolve } = require('path');
var glob = require('glob');
var path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { ESBuildMinifyPlugin } = require('esbuild-loader');
const { ProvidePlugin, BannerPlugin } = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const isProd = process.env.NODE_ENV === 'production';
const isDevelopment = !isProd;
const fastRefresh = isDevelopment ? new ReactRefreshWebpackPlugin({ overlay: false }) : null;
const SANDBOX_SUFFIX = '-sandbox';
const config = {
mode: isProd ? 'production' : 'development',
entry: (() => {
const entries = glob.sync('./src/widgets/**/*.tsx').reduce((obj, el) => {
const rel = path
.relative('src/widgets', el)
.replace(/\.[tj]sx?$/, '')
.replace(/\\/g, '/');
const entryPath = el.startsWith('./') ? el : './' + el.replace(/\\/g, '/');
obj[rel] = entryPath;
obj[`${rel}${SANDBOX_SUFFIX}`] = entryPath;
return obj;
}, {});
entries['snippet'] = './src/index.css';
return entries;
})(),
output: {
path: resolve(__dirname, 'dist'),
filename: `[name].js`,
publicPath: '',
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.(ts|tsx|jsx|js)?$/,
loader: 'esbuild-loader',
options: {
loader: 'tsx',
target: 'es2020',
minify: false,
},
},
{
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { url: false } },
'postcss-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new HtmlWebpackPlugin({
templateContent: `
<body></body>
<script type="text/javascript">
const urlSearchParams = new URLSearchParams(window.location.search);
const queryParams = Object.fromEntries(urlSearchParams.entries());
const widgetName = queryParams["widgetName"];
if (widgetName == undefined) {document.body.innerHTML+="Widget ID not specified."}
const s = document.createElement('script');
s.type = "module";
s.src = widgetName+"${SANDBOX_SUFFIX}.js";
document.body.appendChild(s);
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = widgetName+"${SANDBOX_SUFFIX}.css";
document.head.appendChild(link);
</script>
`,
filename: 'index.html',
inject: false,
}),
new ProvidePlugin({
React: 'react',
reactDOM: 'react-dom',
}),
new BannerPlugin({
banner: (file) => {
return !file.chunk.name.includes(SANDBOX_SUFFIX) ? 'const IMPORT_META=import.meta;' : '';
},
raw: true,
}),
new CopyPlugin({
patterns: [
{ from: 'public', to: '' },
{ from: 'README.md', to: '' },
],
}),
fastRefresh,
].filter(Boolean),
};
if (isProd) {
config.optimization = {
minimize: isProd,
minimizer: [new ESBuildMinifyPlugin()],
};
} else {
config.devServer = {
port: 8080,
open: false,
hot: true,
compress: true,
watchFiles: ['src/**/*'],
devMiddleware: {
writeToDisk: true,
},
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'baggage, sentry-trace',
},
};
}
module.exports = config;