forked from rstackjs/rspack-dev-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-client-modules.cjs
More file actions
151 lines (144 loc) · 3.3 KB
/
build-client-modules.cjs
File metadata and controls
151 lines (144 loc) · 3.3 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
/**
* The following code is modified based on
* https://github.com/webpack/webpack-dev-server
*
* MIT Licensed
* Author Tobias Koppers @sokra
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
*/
'use strict';
const path = require('node:path');
const rspack = require('@rspack/core');
const { merge } = require('webpack-merge');
const fs = require('node:fs');
const modulesDir = path.resolve(__dirname, '../client/modules');
if (fs.existsSync(modulesDir)) {
fs.rmSync(modulesDir, { recursive: true });
}
const library = {
library: {
// type: "module",
type: 'commonjs',
},
};
const baseForModules = {
context: path.resolve(__dirname, '../client-src'),
devtool: false,
mode: 'development',
// TODO enable this in future after fix bug with `eval` in webpack
// experiments: {
// outputModule: true,
// },
resolve: {
extensions: ['.js', '.ts'],
tsConfig: path.resolve(__dirname, '../tsconfig.client.json'),
},
output: {
path: modulesDir,
...library,
},
target: ['web', 'es5'],
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'builtin:swc-loader',
},
],
},
{
test: /\.ts$/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
},
},
},
type: 'javascript/auto',
},
],
},
};
const configs = [
merge(baseForModules, {
entry: path.resolve(__dirname, '../client-src/modules/logger/index.ts'),
output: {
filename: 'logger/index.js',
},
resolve: {
extensions: ['.js', '.ts'],
tsConfig: path.resolve(__dirname, '../tsconfig.client.json'),
},
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'builtin:swc-loader',
},
],
},
{
test: /\.ts$/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
},
},
},
type: 'javascript/auto',
},
],
},
plugins: [
new rspack.DefinePlugin({
Symbol:
'(typeof Symbol !== "undefined" ? Symbol : function (i) { return i; })',
}),
new rspack.NormalModuleReplacementPlugin(
/^tapable$/,
path.resolve(__dirname, '../client-src/modules/logger/tapable.js'),
),
],
}),
merge(baseForModules, {
entry: path.resolve(
__dirname,
'../client-src/modules/sockjs-client/index.ts',
),
output: {
filename: 'sockjs-client/index.js',
library: {
name: 'SockJS',
type: 'umd',
},
globalObject: "(typeof self !== 'undefined' ? self : this)",
},
}),
];
const compiler = rspack(configs);
compiler.run((err, stats) => {
if (err) {
console.error('Build fatal:');
console.error(err);
process.exit(1);
}
const errors = stats.toJson().errors;
if (errors.length > 0) {
console.error('Build errors:');
errors.forEach((error) => {
console.error(error.message);
});
process.exit(1);
}
console.log('Build completed');
process.exit(0);
});