forked from Automattic/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.main.config.ts
104 lines (96 loc) · 3.36 KB
/
webpack.main.config.ts
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
import path from 'path';
// eslint-disable-next-line import/default
import CopyWebpackPlugin from 'copy-webpack-plugin';
import * as fs from 'fs-extra';
import { type Configuration, DefinePlugin } from 'webpack';
import { plugins } from './webpack.plugins';
import { rules } from './webpack.rules';
function getWasmDirs( dir: string ) {
return fs
.readdirSync( dir, { withFileTypes: true } )
.filter( ( dirent: fs.Dirent ) => dirent.isDirectory() )
.map( ( dirent: fs.Dirent ) => dirent.name )
.filter( ( name: string ) => name !== 'node_modules' );
}
const phpWasmDir = path.resolve( __dirname, 'node_modules/@php-wasm/node' );
const wasmDirs = getWasmDirs( phpWasmDir );
// Extra entries are bundled separately from the main bundle. They are primarily used
// for worker threads and forked processes, which need to be loaded independently.
const extraEntries = [
{
name: 'siteServerProcess',
path: './src/lib/site-server-process-child.ts',
exportName: 'SITE_SERVER_PROCESS_MODULE_PATH',
},
{
name: 'wpCliProcess',
path: './src/lib/wp-cli-process-child.ts',
exportName: 'WP_CLI_PROCESS_MODULE_PATH',
},
];
export default function mainConfig( _env: unknown, args: Record< string, unknown > ) {
const isProduction = args.mode === 'production';
// Generates the necessary plugins to expose the module path of extra entries.
const definePlugins = extraEntries.map( ( entry ) => {
// The path calculation is based on how the Forge's webpack plugin generates the path for Electron files.
// Reference: https://github.com/electron/forge/blob/b298b2967bdc79bdc4e09681ea1ccc46a371635a/packages/plugin/webpack/src/WebpackConfig.ts#L113-L140
const modulePath = isProduction
? `require('path').resolve(__dirname, '..', 'main', '${ entry.name }.js')`
: JSON.stringify( path.resolve( __dirname, `.webpack/main/${ entry.name }.js` ) );
return new DefinePlugin( {
[ entry.exportName ]: modulePath,
} );
} );
return {
...mainBaseConfig,
plugins: [ ...( mainBaseConfig.plugins || [] ), ...definePlugins ],
};
}
export const mainBaseConfig: Configuration = {
entry: {
// This is the main entry point for your application, it's the first file
// that runs in the main process.
index: './src/index.ts',
// Inject extra entries into the webpack configuration.
// These entries are primarily used for worker threads and forked processes.
...extraEntries.reduce( ( accum, entry ) => {
return { ...accum, [ entry.name ]: entry.path };
}, {} ),
},
output: {
filename: '[name].js',
},
// Put your normal webpack config below here
devtool: 'source-map',
module: {
rules,
},
plugins: [
...plugins,
new DefinePlugin( {
COMMIT_HASH: JSON.stringify(
process.env.GITHUB_SHA ?? process.env.BUILDKITE_COMMIT ?? undefined
),
} ),
new CopyWebpackPlugin( {
patterns: [
// Copy about-menu.html into the main output directory
{
from: path.resolve( __dirname, 'src/about-menu/about-menu.html' ),
to: path.resolve( __dirname, '.webpack/main/menu' ),
},
{
from: path.resolve( __dirname, 'src/about-menu/studio-app-icon.png' ),
to: path.resolve( __dirname, '.webpack/main/menu' ),
},
...wasmDirs.map( ( dir ) => ( {
from: path.join( phpWasmDir, dir ),
to: path.resolve( __dirname, `.webpack/main/${ dir }` ),
} ) ),
],
} ),
],
resolve: {
extensions: [ '.js', '.ts', '.jsx', '.tsx', '.css', '.json' ],
},
};