-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
webpack.config.js
165 lines (155 loc) · 5.38 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
const path = require('path');
const webpack = require('webpack');
const fs=require('fs');
const { rollup } = require('rollup');
const nodeResolve = require('@rollup/plugin-node-resolve');
const dts = require('rollup-plugin-dts').default;
// const {generateDtsBundle}=require('dts-bundle-generator');
async function bundleDts(
input,
output
) {
try {
// 创建 bundle
const bundle = await rollup({
input,
plugins: [
nodeResolve(),
dts()
]
});
// 写入文件
await bundle.write({
file: output,
format: 'es'
});
// 清理
await bundle.close();
console.log('Successfully bundled .d.ts files');
} catch (error) {
console.error('Error bundling .d.ts files:', error);
throw error;
}
}
class MyCustomPlugin {
apply(compiler) {
// 当Webpack完成构建过程后执行
compiler.hooks.done.tap('MyCustomPlugin',async (stats) => {
// 在这里加入你的自定义代码
fs.copyFileSync(path.resolve(__dirname,'dist','uds.js'),path.resolve(__dirname,'resources','lib','js','uds.js'))
fs.copyFileSync(path.resolve(__dirname,'dist','sa.node'),path.resolve(__dirname,'resources','lib','js','sa.node'))
fs.copyFileSync(path.resolve(__dirname,'dist','crc.js'),path.resolve(__dirname,'resources','lib','js','crc.js'))
fs.copyFileSync(path.resolve(__dirname,'dist','cryptoExt.js'),path.resolve(__dirname,'resources','lib','js','cryptoExt.js'))
//copy uds.d.ts
const udsDTs=path.resolve(__dirname,'dist','src/main/worker','uds.d.ts')
await bundleDts(udsDTs,udsDTs)
let content=fs.readFileSync(udsDTs,'utf-8')
//reaplace
content=content.replace("import { ServiceItem } from '../share/uds';",'')
content=content.replace("service: ServiceItem;","")
content=content.replace("constructor(service: ServiceItem, isRequest: boolean, data?: Buffer);","")
content=content.replace('declare const serviceList: readonly ["{{{serviceName}}}"];',
`declare const serviceList: readonly [
{{#each this.services}}
"{{this}}",
{{/each}}
];`
)
content=content.replace('declare const testerList: readonly ["{{{testerName}}}"];',
`declare const testerList: readonly [
{{#each this.testers}}
"{{this}}",
{{/each}}
];`
)
content=content.replace('declare const allServicesSend: readonly ["{{{serviceName}}}.send"];',
`declare const allServicesSend: readonly [
{{#each this.services}}
"{{this}}.send",
{{/each}}
];`)
content=content.replace('declare const allServicesPreSend: readonly ["{{{serviceName}}}.preSend"];',
`declare const allServicesPreSend: readonly [
{{#each this.services}}
"{{this}}.preSend",
{{/each}}
];`)
content=content.replace('declare const allServicesRecv: readonly ["{{{serviceName}}}.recv"];',
`declare const allServicesRecv: readonly [
{{#each this.services}}
"{{this}}.recv",
{{/each}}
];`)
content=content.replace(
`interface Jobs {
string: (data: Buffer) => string;
}`,
`interface Jobs {
{{#each this.jobs}}
"{{this.name}}": ({{#each this.param}}{{this}},{{/each}}) => DiagRequest[]|Promise<DiagRequest[]>;
{{/each}}
}`)
//write
fs.writeFileSync(path.resolve(__dirname,'src','main','share','uds.d.ts.html'),content)
//bundle cryptoExt.d.ts
const crcFile=path.resolve(__dirname,'dist','src/main/worker','crc.d.ts')
// const v=generateDtsBundle([{
// filePath: cryptoFile,
// }],)
await bundleDts(crcFile,path.resolve(__dirname,'src','main','share','crc.d.ts.html'))
//bundle cryptoExt.d.ts
const cryptoFile=path.resolve(__dirname,'dist','src/main/worker','cryptoExt.d.ts')
// const v=generateDtsBundle([{
// filePath: cryptoFile,
// }],)
await bundleDts(cryptoFile,path.resolve(__dirname,'src','main','share','cryptoExt.d.ts.html'))
// fs.writeFileSync(path.resolve(__dirname,'src','share','cryptoExt.d.ts.html'),v[0])
console.log('构建过程完成!');
});
}
}
module.exports = {
entry: {
uds:path.resolve(__dirname,'src/main/worker') + '/uds.ts',
crc:path.resolve(__dirname,'src/main/worker') + '/crc.ts',
cryptoExt:path.resolve(__dirname,'src/main/worker') + '/cryptoExt.ts',
},
output: {
path: path.resolve(__dirname,'dist'),
filename: '[name].js',
library: {
type: 'commonjs',
},
},
target: 'node',
plugins: [
new MyCustomPlugin()
],
module: {
rules: [
{
test: /\.node$/,
use: [{
loader: 'node-loader',
options: {
name: '[name].[ext]',
}
}]
},
{
test: /\.ts$/,
use: [{
loader: 'ts-loader',
options: {
configFile: "tsconfig.worker.json",
}
}],
exclude: /node_modules/,
},
]
},
resolve: {
extensions: ['.js', '.ts'],
},
devtool: false
};