Repository navigation
Expand file tree
/
Copy pathwebpack.apps.config.js
More file actions
120 lines (104 loc) · 2.78 KB
/
Copy pathwebpack.apps.config.js
File metadata and controls
120 lines (104 loc) · 2.78 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
/**
* External dependencies
*/
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin'; // eslint-disable-line import/no-extraneous-dependencies
/**
* WordPress dependencies
*/
import defaultConfig from '@wordpress/scripts/config/webpack.config.js'; // eslint-disable-line no-restricted-syntax,import/no-extraneous-dependencies
const isDev = process.env.NODE_ENV === 'development';
/**
* Add one entry per MCP app. The app name must match the directory name under
* src/apps/ and will become the output directory name under build-apps/.
*/
const mcpApps = {
'pattern-approval': 'src/apps/pattern-approval/index.tsx',
};
const mcpAppEntries = Object.fromEntries(
Object.entries( mcpApps ).map( ( [ name, file ] ) => [
name,
path.resolve( import.meta.dirname, file ),
] )
);
const plugins = defaultConfig.plugins.filter(
( plugin ) => plugin.constructor.name !== 'RtlCssPlugin'
);
const appsConfig = {
...defaultConfig,
mode: isDev ? 'development' : 'production',
devtool: isDev ? 'inline-source-map' : false,
entry: mcpAppEntries,
output: {
path: path.resolve( import.meta.dirname, 'build-apps' ),
filename: '[name].js',
publicPath: '%%PLUGIN_URL%%',
clean: true,
chunkFormat: false,
},
resolve: {
extensions: [ '.ts', '.tsx', '.js', '.jsx' ],
alias: {
...defaultConfig.resolve.alias,
'@apps': path.resolve( import.meta.dirname, 'src/apps' ),
},
},
optimization: {
...defaultConfig.optimization,
runtimeChunk: false,
},
module: {
...defaultConfig.module,
rules: [
...defaultConfig.module.rules,
/**
* postcss-loader is already added in the defaultConfig
* but we need it here for tailwindcss.
*/
{
test: /\.scss$/i,
include: path.resolve( import.meta.dirname, 'src' ),
use: [ 'postcss-loader' ],
},
],
},
plugins: [
...plugins,
new HtmlWebpackPlugin( {
template: path.resolve(
import.meta.dirname,
'src/apps/template.html'
),
filename: '[name].html',
scriptLoading: 'blocking',
} ),
],
};
/**
* In Hot Module Replacement (HMR) mode, we need to allow the dev server to proxy requests to the WordPress site.
*
* The SITE_URL environment variable should be set to the URL of the WordPress site during development.
*
* @see https://webpack.js.org/configuration/dev-server/#devserverproxy
*/
if ( isDev && process.env.SITE_URL ) {
const SITE_URL = process.env.SITE_URL;
const SITE_HOSTNAME = new URL( SITE_URL ).hostname;
appsConfig.devServer = {
devMiddleware: {
writeToDisk: true,
},
host: 'localhost',
port: 8889,
allowedHosts: SITE_HOSTNAME,
proxy: [
{
context: [ '/build-apps' ],
target: SITE_URL,
pathRewrite: { '^/build-apps': '' },
},
],
};
appsConfig.output.publicPath = 'http://localhost:8889/build-apps/';
}
export default appsConfig;