-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
225 lines (196 loc) · 6.66 KB
/
webpack.config.js
File metadata and controls
225 lines (196 loc) · 6.66 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Webpack Configuration for ClickAI Browser Extension
*
* This configuration builds the browser extension from React source code into
* a properly structured extension that can be loaded into Chrome and other
* Chromium-based browsers. It handles multiple entry points, environment
* variable injection, and asset copying.
*
* Build targets:
* - popup: React-based popup interface
* - content: Content script injected into web pages
* - background: Service worker for extension background tasks
*
* @author Saketh Sripada
* @version 1.0.0
*/
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
// Load environment variables from .env file
require('dotenv').config();
module.exports = {
// Multiple entry points for different parts of the extension
entry: {
// Main popup interface - React application
popup: './src/index.js',
// Content script - injected into web pages
content: './public/content.js',
// Background script - service worker for extension functionality
background: './public/background.js',
},
// Output configuration for built files
output: {
// Build output directory
path: path.resolve(__dirname, 'dist'),
// File naming pattern - [name] corresponds to entry point names
filename: '[name].bundle.js',
// Clean output directory before each build
clean: true,
},
// Module rules for processing different file types
module: {
rules: [
{
// JavaScript and JSX file processing with Babel
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
// Babel presets for React and modern JavaScript
presets: [
'@babel/preset-env',
['@babel/preset-react', { runtime: 'automatic' }]
],
},
},
},
{
// CSS file processing
test: /\.css$/i,
use: [
'style-loader', // Injects CSS into DOM
'css-loader', // Processes CSS imports and url() references
],
},
{
// Asset file processing (images, fonts, etc.)
test: /\.(png|svg|jpg|jpeg|gif|ico)$/i,
type: 'asset/resource',
generator: {
filename: 'assets/[name][ext]',
},
},
],
},
// File extension resolution
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
// Fallbacks for Node.js modules that aren't available in browser
fallback: {
"crypto": false,
"stream": false,
"util": false,
"buffer": false,
},
},
// Webpack plugins for additional functionality
plugins: [
// Generate HTML file for popup interface
new HtmlWebpackPlugin({
template: './public/index.html',
filename: 'index.html',
chunks: ['popup'], // Only include popup bundle in this HTML
inject: 'body',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
},
}),
// Copy static assets that don't need processing
new CopyWebpackPlugin({
patterns: [
{
from: 'public/manifest.json',
to: 'manifest.json',
// Transform manifest to inject environment variables if needed
transform(content) {
const manifest = JSON.parse(content.toString());
// Could add dynamic version or other build-time values here
return JSON.stringify(manifest, null, 2);
},
},
// Extension icons
{ from: 'public/logo192.png', to: 'logo192.png' },
{ from: 'public/logo512.png', to: 'logo512.png' },
{ from: 'public/favicon.ico', to: 'favicon.ico' },
// Math rendering assets
{ from: 'public/sandbox.html', to: 'sandbox.html' },
{ from: 'public/mathjax-handler.js', to: 'mathjax-handler.js' },
{ from: 'public/tex-chtml.js', to: 'tex-chtml.js' },
// Additional assets if they exist
{ from: 'public/imgs', to: 'imgs', noErrorOnMissing: true },
{ from: 'public/vendor', to: 'vendor', noErrorOnMissing: true },
],
}),
// Provide global variables for older libraries
new webpack.ProvidePlugin({
React: 'react',
// Make createRoot globally available for dynamic rendering
createRoot: ['react-dom/client', 'createRoot'],
}),
// Inject environment variables into the bundle
new webpack.DefinePlugin({
// Make environment variables available to the extension code
'process.env.EXTENSION_SECRET': JSON.stringify(process.env.EXTENSION_SECRET),
'process.env.BASE_URL': JSON.stringify(process.env.BASE_URL),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
// Define global for extension environment
'process.env.IS_EXTENSION': JSON.stringify(true),
}),
// Bundle analyzer plugin (uncomment for bundle size analysis)
// new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)({
// analyzerMode: 'static',
// openAnalyzer: false,
// }),
],
// Build mode configuration
mode: process.env.NODE_ENV === 'development' ? 'development' : 'production',
// Development tools configuration
devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-source-map' : false,
// Optimization settings
optimization: {
// Don't split chunks for extension - keep each entry point as single file
splitChunks: {
chunks: 'async', // Only split async chunks, keep entry points intact
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'async',
priority: 10,
reuseExistingChunk: true,
},
},
},
// Minimize in production
minimize: process.env.NODE_ENV !== 'development',
// Use deterministic module IDs for better caching
moduleIds: 'deterministic',
},
// Performance hints configuration
performance: {
// Set higher limits for extension bundles
maxAssetSize: 1000000, // 1MB
maxEntrypointSize: 1000000, // 1MB
// Only warn in development
hints: process.env.NODE_ENV === 'development' ? 'warning' : false,
},
// External dependencies that shouldn't be bundled
externals: {
// Chrome extension APIs are provided by the browser
'chrome': 'chrome',
},
// Stats configuration for build output
stats: {
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false,
},
};