-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.widget.js
More file actions
148 lines (145 loc) · 6.19 KB
/
webpack.widget.js
File metadata and controls
148 lines (145 loc) · 6.19 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
const path = require("path")
const webpack = require("webpack")
const { merge } = require("webpack-merge")
const common = require("./webpack.common")
const SCW = require("slightning-coco-widget--webpack")
const { project } = require("./project")
/**
* @typedef {Object} AllowInfo
* @property {string} user
* @property {string} usingWork
* @property {string} connectingWork
*/
/**
* @param {{ mode?: webpack.Configuration["mode"], platform?: string } & AllowInfo} env
* @param {string[]} argv
* @returns {webpack.Configuration}
*/
module.exports = function (env, argv) {
const { mode = "production", platform = "编程猫 CoCo" } = env
let editionName = `(${platform} 控件版)`, isExclusiveEdition = false
if (mode == "development") {
editionName += "(开发调试版)"
} else if (
typeof env.user == "string" &&
typeof env.usingWork == "string" &&
typeof env.connectingWork == "string"
) {
isExclusiveEdition = true
editionName += `(用户 ${env.user} 在 ${env.usingWork} 中连接 ${env.connectingWork} 的专用版)`
} else {
editionName += "(修改受限版)"
}
const comments = [
"@name " + project.name + editionName,
"@author " + project.author,
"@version " + project.version,
"@license " + project.license
]
return merge({
entry: {
[project.name + editionName + ".js"]: "./src/wrapper/kitten-cloud-function-codemao-coco-widget/index.ts",
[project.name + editionName + ".min.js"]: "./src/wrapper/kitten-cloud-function-codemao-coco-widget/index.ts"
},
output: {
path: mode == "development" ? path.resolve(__dirname, "out", "dev") : path.resolve(__dirname, "out")
},
module: {
rules: [
{
test: /kitten-cloud-function-codemao-coco-widget[\\\/]widget\.ts$/,
exclude: /node_modules/,
use: mode == "development" ? [
{
loader: "string-replace-loader",
options: {
search: "KITTEN_CLOUD_FUNCTION_DEVELOP",
replace: "true"
}
}
] : [
{
loader: "obfuscator-loader",
options: /** @type {import("javascript-obfuscator").ObfuscatorOptions} */({
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
identifierNamesGenerator: "mangled-shuffled",
renameGlobals: true,
stringArrayEncoding: ["rc4"],
stringArrayThreshold: 1,
target: "browser-no-eval"
})
}, {
loader: "string-replace-loader",
options: { multiple: [
{
search: "KITTEN_CLOUD_FUNCTION_DEVELOP",
replace: "false"
}, {
search: "KITTEN_CLOUD_FUNCTION_ALLOW_USER",
replace: isExclusiveEdition ? JSON.stringify(env.user) : "null"
}, {
search: "KITTEN_CLOUD_FUNCTION_ALLOW_USING_WORK",
replace: isExclusiveEdition ? JSON.stringify(env.usingWork) : "null"
}, {
search: "KITTEN_CLOUD_FUNCTION_ALLOW_CONNECTING_WORK",
replace: isExclusiveEdition ? JSON.stringify(env.connectingWork) : "null"
}
]}
}, {
loader: "babel-loader",
options: /** @type {import("@babel/core").TransformOptions} */({
plugins: [path.resolve(__dirname, "obfuscator.js")],
babelrc: false
})
}, {
loader: "babel-loader",
options: /** @type {import("@babel/core").TransformOptions} */({
presets: [
["@babel/preset-env", {
targets: {
ie: "11"
}
}]
],
babelrc: false
})
}
]
}, {
test: /\.(t|j)sx?$/,
use: {
loader: SCW.Loaders.ExternalImport,
options: {
"@slightning/anything-to-string": "https://unpkg.com/@slightning/anything-to-string@1/dist/cjs/bundle.min.js",
"axios": "https://unpkg.com/axios@1/dist/axios.min.js",
"diff": "https://unpkg.com/diff@5/dist/diff.min.js"
}
}
}
]
},
externalsType: "commonjs",
externals: {
"crypto-js": "crypto-js"
},
plugins: [
new webpack.IgnorePlugin({
resourceRegExp: /^(fs|os|path|websocket|appdirsjs)$/
}),
new webpack.BannerPlugin({
banner: `require("crypto-js");`,
raw: true,
entryOnly: true
})
]
}, SCW.config, {
plugins: [
new webpack.BannerPlugin({
banner: "/**" + "\n" + comments.map(line => ` * ${line}\n`).join("") + " */" + "\n",
raw: true,
entryOnly: true
})
]
}, common({ ...env, comments }, argv))
}