forked from swagger-api/swagger-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_config-builder.js
196 lines (173 loc) · 4.95 KB
/
_config-builder.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* @prettier
*/
import path from "path"
import os from "os"
import fs from "fs"
import deepExtend from "deep-extend"
import webpack from "webpack"
import TerserPlugin from "terser-webpack-plugin"
import RemoveSourcemapsLackingMatchingAssetsPlugin from "./_RemoveSourcemapsLackingMatchingAssetsPlugin.babel.js"
import { getRepoInfo } from "./_helpers"
import pkg from "../package.json"
const nodeModules = fs.readdirSync("node_modules").filter(function(x) {
return x !== ".bin"
})
const projectBasePath = path.join(__dirname, "../")
const baseRules = [
{
test: /\.jsx?$/,
include: [
path.join(projectBasePath, "src"),
path.join(projectBasePath, "node_modules", "object-assign-deep"),
],
loader: "babel-loader",
options: {
retainLines: true,
cacheDirectory: true,
},
},
{ test: /\.(txt|yaml)$/, loader: "raw-loader" },
{
test: /\.(png|jpg|jpeg|gif|svg)$/, use: [
{
loader: "url-loader",
options: {
esModule: false,
},
},
],
},
{
test: /\.(woff|woff2)$/,
loader: "url-loader?",
options: {
limit: 10000,
},
},
{ test: /\.(ttf|eot)$/, loader: "file-loader" },
]
export default function buildConfig(
{
minimize = true,
mangle = true,
sourcemaps = true,
includeDependencies = true,
emitWorkerAssets = false,
},
customConfig
) {
const gitInfo = getRepoInfo()
const plugins = [
new webpack.DefinePlugin({
"process.env.CI": process.env.CI || false,
buildInfo: JSON.stringify({
PACKAGE_VERSION: pkg.version,
GIT_COMMIT: gitInfo.hash,
GIT_DIRTY: gitInfo.dirty,
HOSTNAME: os.hostname(),
BUILD_TIME: new Date().toUTCString(),
}),
}),
new RemoveSourcemapsLackingMatchingAssetsPlugin(),
]
//// Workers
baseRules.push({
test: /\.worker\.js$/,
use: [
{
loader: "worker-loader",
options: {
inline: true,
name: "[name].js",
fallback: !!emitWorkerAssets,
},
},
"babel-loader",
],
})
const completeConfig = deepExtend(
{},
{
mode: "production",
entry: {},
output: {
path: path.join(projectBasePath, "dist"),
publicPath: "/dist",
filename: "[name].js",
chunkFilename: "[id].[chunkhash].js",
libraryTarget: "umd",
libraryExport: "default", // TODO: enable
},
target: "web",
node: {
// yaml-js has a reference to `fs`, this is a workaround
fs: "empty",
},
module: {
rules: baseRules,
},
externals: includeDependencies
? {
// json-react-schema/deeper depends on buffertools, which fails.
buffertools: true,
esprima: true,
}
: (context, request, cb) => {
// webpack injects some stuff into the resulting file,
// these libs need to be pulled in to keep that working.
var exceptionsForWebpack = ["ieee754", "base64-js"]
if (
nodeModules.indexOf(request) !== -1 ||
exceptionsForWebpack.indexOf(request) !== -1
) {
cb(null, "commonjs " + request)
return
}
cb()
},
resolve: {
extensions: [".js", ".jsx", "json"],
alias: {
react: path.resolve(projectBasePath, "node_modules", "react"),
"react-dom": path.resolve(projectBasePath, "node_modules", "react-dom"),
"react-is": path.resolve(projectBasePath, "node_modules", "react-is"),
"regenerator-runtime": path.resolve(projectBasePath, "node_modules", "@babel", "runtime-corejs3", "node_modules", "regenerator-runtime"),
"reselect": path.resolve(projectBasePath, "node_modules", "reselect"),
// this alias avoids bundling the React twice
"swagger-ui": path.resolve(projectBasePath, "node_modules", "swagger-ui", "dist", "swagger-ui-es-bundle-core.js"),
brace: path.resolve(projectBasePath, "node_modules", "brace"),
},
},
// If we're mangling, size is a concern -- so use trace-only sourcemaps
// Otherwise, provide heavy sourcemaps suitable for development
devtool: sourcemaps
? minimize
? "nosource-source-map"
: "module-source-map"
: false,
performance: {
hints: "error",
maxEntrypointSize: 1024000,
maxAssetSize: 1024000,
},
optimization: {
minimize: !!minimize,
minimizer: [
compiler =>
new TerserPlugin({
cache: true,
sourceMap: sourcemaps,
terserOptions: {
mangle: !!mangle,
},
}).apply(compiler),
],
},
},
customConfig
)
// deepExtend mangles Plugin instances, this doesn't
completeConfig.plugins = plugins.concat(customConfig.plugins || [])
return completeConfig
}