Files
swagger-ui/webpack/_config-builder.js
Tim Lai 07d346b516 feat(build): webpack@5 and webpack-dev-server@4 (#7826)
SwaggerUI is now built using `webpack@5`, with dev support for `webpack-dev-server@4`
- ES Module output bundle path now points to `swagger-ui-es-bundle-core`, which does not include dependencies
- No change to CommonJS output bundle or path
- Now uses Asset Modules, which replaces `file-loader`, `raw-loader`, and `url-loader`
- Removed unused rules/loaders for `.woff | .woff2 | .ttf | .eot` fonts and html
- Node polyfills are no longer bundled with `webpack@5`, and must be loaded separately and/or use `resolve.fallback`. 
As an example, SwaggerUI loads `process`, `buffer`, and `stream-browserify` as `devDependencies` in order to build development and production bundles.

SwaggerUI-React
- Now imports `swagger-ui-es-bundle-core`, and similarly outputs `swagger-ui-es-bundle-core` to its `dist` directory

Dev notes:
- Order of execution matters for the production npm build scripts. `build-stylesheets` needs to get built first, 
then cleanup of any empty artifacts, before building the various production bundles
- `Dev-helpers` now relies on `HTMLWebpackPlugin` to inject css and bundle files
2022-03-01 12:08:50 -08:00

167 lines
4.5 KiB
JavaScript

/**
* @prettier
*/
import path from "path"
import fs from "fs"
import deepExtend from "deep-extend"
import webpack from "webpack"
import TerserPlugin from "terser-webpack-plugin"
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)$/,
type: "asset/source",
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
type: "asset/inline",
},
]
export default function buildConfig(
{
minimize = true,
mangle = true,
sourcemaps = true,
includeDependencies = true,
},
customConfig
) {
const gitInfo = getRepoInfo()
var plugins = [
new webpack.DefinePlugin({
buildInfo: JSON.stringify({
PACKAGE_VERSION: pkg.version,
GIT_COMMIT: gitInfo.hash,
GIT_DIRTY: gitInfo.dirty,
BUILD_TIME: new Date().toUTCString(),
}),
}),
new webpack.ProvidePlugin({
process: "process/browser",
Buffer: ["buffer", "Buffer"],
}),
]
const completeConfig = deepExtend(
{},
{
mode: "production",
entry: {},
output: {
path: path.join(projectBasePath, "dist"),
publicPath: "/dist",
filename: "[name].js",
chunkFilename: "[name].js",
globalObject: "this",
library: {
// when esm, library.name should be unset, so do not define here
// when esm, library.export should be unset, so do not define here
type: "umd",
}
},
target: "web",
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: {
modules: [path.join(projectBasePath, "./src"), "node_modules"],
extensions: [".web.js", ".js", ".jsx", ".json", ".less"],
alias: {
// these aliases make sure that we don't bundle same libraries twice
// when the versions of these libraries diverge between swagger-js and swagger-ui
"@babel/runtime-corejs3": path.resolve(__dirname, "..", "node_modules/@babel/runtime-corejs3"),
"js-yaml": path.resolve(__dirname, "..", "node_modules/js-yaml"),
"lodash": path.resolve(__dirname, "..", "node_modules/lodash"),
"isarray": path.resolve(__dirname, "..", "node_modules/stream-browserify/node_modules/isarray"),
"react-is": path.resolve(__dirname, "..", "node_modules/react-redux/node_modules/react-is"),
"safe-buffer": path.resolve(__dirname, "..", "node_modules/string_decoder/node_modules/safe-buffer"),
},
fallback: {
fs: false,
stream: require.resolve("stream-browserify"),
},
},
// If we're mangling, size is a concern -- so use trace-only sourcemaps
// Otherwise, provide heavy souremaps suitable for development
devtool: sourcemaps
? minimize
? "nosources-source-map"
: "cheap-module-source-map"
: false,
performance: {
hints: "error",
maxEntrypointSize: 1153434,
maxAssetSize: 1153434,
},
optimization: {
minimize: !!minimize,
minimizer: [
compiler =>
new TerserPlugin({
terserOptions: {
mangle: !!mangle,
},
}).apply(compiler)
],
},
},
customConfig
)
// deepExtend mangles Plugin instances, this doesn't
completeConfig.plugins = plugins.concat(customConfig.plugins || [])
return completeConfig
}