-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
180 lines (172 loc) · 5.05 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
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
/**
* External dependencies
*/
const { resolve, dirname, basename } = require( 'node:path' );
const { DefinePlugin } = require( 'webpack' );
const MiniCSSExtractPlugin = require( 'mini-css-extract-plugin' );
const { WebWorkerPlugin } = require( '@shopify/web-worker/webpack' );
/**
* WordPress dependencies
*/
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const { hasBabelConfig, hasArgInCLI } = require( '@wordpress/scripts/utils' );
const isProduction = process.env.NODE_ENV === 'production';
const hasReactFastRefresh = hasArgInCLI( '--hot' ) && ! isProduction;
const {
version: mediaPipeVersion,
// eslint-disable-next-line import/no-extraneous-dependencies
} = require( '@mediapipe/selfie_segmentation/package.json' );
// eslint-disable-next-line import/no-extraneous-dependencies
const { version: pdfJsVersion } = require( 'pdfjs-dist/package.json' );
// eslint-disable-next-line import/no-extraneous-dependencies
const { version: ffmpegVersion } = require( '@ffmpeg/core/package.json' );
const mediapipeCdnUrl = `https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation@${ mediaPipeVersion }`;
const pdfJsCdnUrl = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${ pdfJsVersion }/build/pdf.worker.mjs`;
const ffmpegCdnUrl = `https://cdn.jsdelivr.net/npm/@ffmpeg/core@${ ffmpegVersion }/dist/ffmpeg-core.js`;
module.exports = {
...defaultConfig,
entry: {
'media-experiments': resolve(
__dirname,
'packages/editor/src/index.ts'
),
'view-upload-request': resolve(
__dirname,
'packages/view-upload-request/src/index.tsx'
),
},
output: {
filename: '[name].js',
// TODO: Maybe make [chunkhash] a part of the filename?
chunkFilename: '[name].js?v=[chunkhash]',
path: resolve( __dirname, 'build' ),
globalObject: 'self', // This is the default, but required for @shopify/web-worker.
},
resolve: {
// Ensure "require" has a higher priority when matching export conditions.
// https://webpack.js.org/configuration/resolve/#resolveconditionnames
// https://github.com/kleisauke/wasm-vips/issues/50#issuecomment-1664118137
conditionNames: [ 'require', 'import' ],
extensions: [ '.jsx', '.ts', '.tsx', '...' ],
fallback: {
crypto: false,
path: false,
util: false,
zlib: false,
assert: false,
stream: false,
fs: false,
},
},
module: {
rules: [
{
test: /\.([jt])sx?$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve( 'babel-loader' ),
options: {
// Babel uses a directory within local node_modules
// by default. Use the environment variable option
// to enable more persistent caching.
cacheDirectory:
process.env.BABEL_CACHE_DIRECTORY || true,
// Provide a fallback configuration if there's not
// one explicitly available in the project.
...( ! hasBabelConfig() && {
babelrc: false,
configFile: false,
presets: [
require.resolve(
'@wordpress/babel-preset-default'
),
],
plugins: [
require.resolve(
'@shopify/web-worker/babel'
),
hasReactFastRefresh &&
require.resolve(
'react-refresh/babel'
),
require.resolve( '@mexp/log/babel-plugin' ),
].filter( Boolean ),
} ),
},
},
],
},
{
test: /\.wasm$/,
type: 'asset/resource',
generator: {
// Use [contenthash:8] to help with cache busting.
filename: '[name].[contenthash:8].wasm',
publicPath: '',
},
},
...defaultConfig.module.rules.slice( 1 ),
],
},
plugins: [
...defaultConfig.plugins.filter(
( plugin ) => ! ( plugin instanceof MiniCSSExtractPlugin )
),
new WebWorkerPlugin(),
new MiniCSSExtractPlugin( {
filename: '[name].css',
} ),
new DefinePlugin( {
FFMPEG_CDN_URL: JSON.stringify( ffmpegCdnUrl ),
MEDIAPIPE_CDN_URL: JSON.stringify( mediapipeCdnUrl ),
PDFJS_CDN_URL: JSON.stringify( pdfJsCdnUrl ),
} ),
],
optimization: {
...defaultConfig.optimization,
splitChunks: {
...defaultConfig.optimization.splitChunks,
cacheGroups: {
editor: {
type: 'css/mini-extract',
test: /[\\/]editor\.css$/,
chunks: ( chunk ) => {
return chunk.name === 'media-experiments';
},
name( _, chunks, cacheGroupKey ) {
const chunkName = chunks[ 0 ].name;
return `${ dirname( chunkName ) }/${ basename(
chunkName
) }-${ cacheGroupKey }`;
},
},
blocks: {
type: 'css/mini-extract',
test: /[\\/]blocks\.css$/,
chunks: 'all',
enforce: true,
name( _, chunks, cacheGroupKey ) {
const chunkName = chunks[ 0 ].name;
return `${ dirname( chunkName ) }/${ basename(
chunkName
) }-${ cacheGroupKey }`;
},
},
view: {
type: 'css/mini-extract',
test: /[\\/]view\.css$/,
chunks: 'all',
enforce: true,
name( _, chunks, cacheGroupKey ) {
const chunkName = chunks[ 0 ].name;
return `${ dirname( chunkName ) }/${ basename(
chunkName
) }-${ cacheGroupKey }`;
},
},
default: false,
},
},
},
};