Repository navigation
Expand file tree
/
Copy pathwebpack.config.mjs
More file actions
267 lines (252 loc) · 8.33 KB
/
Copy pathwebpack.config.mjs
File metadata and controls
267 lines (252 loc) · 8.33 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import path from 'path'
import webpack from 'webpack'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import CopyPlugin from 'copy-webpack-plugin'
import NodePolyfillPlugin from 'node-polyfill-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
const PACKAGE_RESOLUTION_MODE = 'package'
const WORKSPACE_RESOLUTION_MODE = 'workspace'
const packageAliases = {
'rdflib': path.resolve('./node_modules/rdflib'),
'solid-logic': path.resolve('./node_modules/solid-logic'),
'solid-ui$': path.resolve('./node_modules/solid-ui/dist/index.esm.js'),
'solid-ui/components$': path.resolve('./node_modules/solid-ui/dist/components/index.esm.js'),
'solid-panes$': path.resolve('./node_modules/solid-panes/dist/index.esm.js'),
// NOTE: intentionally NO prefix alias for 'solid-ui/components'. solid-ui
// ships two parallel outputs for each component:
// dist/components/<name>.js (shim importing a shared chunk that
// INLINES @awesome.me/webawesome)
// dist/components/<name>/index.esm.js (proper leaf entry that keeps
// webawesome external)
// A prefix alias pointing at dist/components would make webpack pick the
// shim, causing duplicate wa-popup / wa-tooltip custom-element registrations
// (one from the inlined shared chunk, one from the external webawesome).
// Let webpack resolve solid-ui/components/<name> via solid-ui's package.json
// `exports` field, which correctly maps to dist/components/<name>/index.esm.js.
'UI$': path.resolve('./node_modules/solid-ui/dist/index.esm.js'),
'pane-registry': path.resolve('./node_modules/pane-registry'),
'$rdf': path.resolve('./node_modules/rdflib'),
'SolidLogic': path.resolve('./node_modules/solid-logic'),
// Force a single copy of @awesome.me/webawesome to avoid duplicate
// custom-element registrations (wa-popup, etc.) from the hoisted install.
'@awesome.me/webawesome': path.resolve('./node_modules/@awesome.me/webawesome'),
}
const workspaceAliases = {
'solid-ui$': path.resolve('../solid-ui/dist/index.cjs.js'),
'solid-ui/components$': path.resolve('../solid-ui/dist/components/index.cjs.js'),
'solid-panes$': path.resolve('../solid-panes/dist/index.esm.js'),
// See packageAliases: no prefix alias for 'solid-ui/components' — the
// shim files would shadow the correctly-externalized leaf entries.
'UI$': path.resolve('../solid-ui/dist/index.cjs.js'),
'@awesome.me/webawesome': path.resolve('../solid-ui/node_modules/@awesome.me/webawesome'),
}
function getResolutionMode (env = {}) {
const resolutionMode = env.resolutionMode || process.env.MASHLIB_RESOLUTION_MODE || PACKAGE_RESOLUTION_MODE
if (resolutionMode !== PACKAGE_RESOLUTION_MODE && resolutionMode !== WORKSPACE_RESOLUTION_MODE) {
throw new Error(`Invalid mashlib webpack resolution mode: ${resolutionMode}. Use "${PACKAGE_RESOLUTION_MODE}" or "${WORKSPACE_RESOLUTION_MODE}".`)
}
return resolutionMode
}
function getResolveConfig (resolutionMode) {
return {
extensions: ['.js', '.ts', '.cjs.js', '.esm.js'],
alias: {
...packageAliases,
...(resolutionMode === WORKSPACE_RESOLUTION_MODE ? workspaceAliases : {})
}
}
}
const externalsBase = {
'fs': 'null',
'node-fetch': 'fetch',
'isomorphic-fetch': 'fetch',
'@xmldom/xmldom': 'window',
'text-encoding': 'TextEncoder',
'whatwg-url': 'window',
'@trust/webcrypto': 'crypto'
}
function createCommonConfig (resolutionMode) {
return {
entry: [
'./src/index.ts'
],
target: 'web',
output: {
path: path.resolve(process.cwd(), 'dist'),
chunkFilename: '[name].js',
// Use /mashlib/dist/ for GitHub Pages, / for localhost
publicPath: process.env.PUBLIC_PATH || '/',
library: {
name: 'Mashlib',
type: 'umd'
},
},
resolve: getResolveConfig(resolutionMode),
module: {
rules: [
{
test: /\.ttl$/, // Target text files
type: 'asset/source', // Load the file's content as a string
},
{
test: /\.(mjs|js|ts)$/,
exclude: (modulePath) => {
if (/node_modules[/\\]solid-panes[/\\]src/.test(modulePath)) return false
return /node_modules|bower_components/.test(modulePath)
},
use: {
loader: 'babel-loader',
}
},
{
test: /^.*\/solid-app-set\/.*\.js$/,
loader: 'babel-loader'
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
'css-loader'
],
},
{
test: /\.(eot|ttf|woff2?)$/i,
loader: 'file-loader'
},
{
test: /\.(png|jpg|gif|svg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
},
},
],
},
]
},
plugins: [
new webpack.DefinePlugin({ 'global.IS_BROWSER': true }),
new HtmlWebpackPlugin({
template: './src/databrowser.html',
filename: 'databrowser.html'
}),
new MiniCssExtractPlugin({
filename: 'mash.css'
}),
new NodePolyfillPlugin(),
new CopyPlugin({
patterns: [
{ from: 'static', to: '.' }
]
})
],
devServer: {
port: 8080,
open: '/browse-test.html',
hot: true,
historyApiFallback: true,
allowedHosts: 'all',
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization',
'Access-Control-Allow-Credentials': 'true',
'Content-Security-Policy': "default-src 'self' 'unsafe-inline' 'unsafe-eval' blob: data: https: http:; img-src 'self' data: blob: https: http:; connect-src 'self' https: http: ws: wss:;"
},
static: [
{
directory: path.resolve(process.cwd(), 'static'),
publicPath: '/'
},
{
directory: path.resolve(process.cwd(), 'dist'),
publicPath: '/'
}
],
devMiddleware: {
publicPath: '/',
writeToDisk: false
}
},
devtool: 'source-map',
performance: { hints: false }
}
}
export default (env, args) => {
const resolutionMode = getResolutionMode(env)
const common = createCommonConfig(resolutionMode)
// Shared optimization configuration
const sharedOptimization = {
providedExports: true,
usedExports: true,
sideEffects: false,
// Ensure no externals for core libraries - bundle everything
removeEmptyChunks: true,
mergeDuplicateChunks: true
}
// Check if running in watch mode
const isWatchMode = process.argv.includes('--watch')
// For dev server or watch mode, return only unminified config (preserves console.log)
if (process.env.WEBPACK_SERVE || args.mode === 'development' || isWatchMode) {
const developmentBundle = {
...common,
mode: 'development',
output: {
...common.output,
filename: 'mashlib.js'
},
externals: externalsBase,
optimization: {
...sharedOptimization,
minimize: false
}
}
// Use a single devServer config for watch/dev runs to avoid duplicate port
// conflicts in webpack-dev-server.
return developmentBundle
}
// UMD Minified, everything bundled
const minified = {
...common,
mode: args.mode || 'production',
output: {
...common.output,
filename: 'mashlib.min.js'
},
externals: externalsBase,
optimization: {
...sharedOptimization,
minimize: true,
minimizer: [new TerserPlugin({
extractComments: false,
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.debug']
}
}
})]
}
}
// UMD Unminified, everything bundled
const unminified = {
...common,
mode: args.mode || 'production',
output: {
...common.output,
filename: 'mashlib.js'
},
externals: externalsBase,
optimization: {
...sharedOptimization,
minimize: false
}
}
return [minified, unminified]
}