You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

163 rivejä
4.1 KiB

  1. /**
  2. * @prettier
  3. */
  4. import path from "path"
  5. import os from "os"
  6. import fs from "fs"
  7. import deepExtend from "deep-extend"
  8. import webpack from "webpack"
  9. import TerserPlugin from "terser-webpack-plugin"
  10. import { getRepoInfo } from "./_helpers"
  11. import pkg from "../package.json"
  12. const nodeModules = fs.readdirSync("node_modules").filter(function(x) {
  13. return x !== ".bin"
  14. })
  15. const projectBasePath = path.join(__dirname, "../")
  16. const baseRules = [
  17. {
  18. test: /\.jsx?$/,
  19. include: [
  20. path.join(projectBasePath, "src"),
  21. path.join(projectBasePath, "node_modules", "object-assign-deep"),
  22. ],
  23. loader: "babel-loader",
  24. options: {
  25. retainLines: true,
  26. cacheDirectory: true,
  27. },
  28. },
  29. { test: /\.(txt|yaml)$/, loader: "raw-loader" },
  30. { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: "url-loader" },
  31. {
  32. test: /\.(woff|woff2)$/,
  33. loader: "url-loader?",
  34. options: {
  35. limit: 10000,
  36. },
  37. },
  38. { test: /\.(ttf|eot)$/, loader: "file-loader" },
  39. ]
  40. export default function buildConfig(
  41. {
  42. minimize = true,
  43. mangle = true,
  44. sourcemaps = true,
  45. includeDependencies = true,
  46. },
  47. customConfig
  48. ) {
  49. const gitInfo = getRepoInfo()
  50. var plugins = [
  51. new webpack.DefinePlugin({
  52. buildInfo: JSON.stringify({
  53. PACKAGE_VERSION: pkg.version,
  54. GIT_COMMIT: gitInfo.hash,
  55. GIT_DIRTY: gitInfo.dirty,
  56. HOSTNAME: os.hostname(),
  57. BUILD_TIME: new Date().toUTCString(),
  58. }),
  59. }),
  60. ]
  61. const completeConfig = deepExtend(
  62. {},
  63. {
  64. mode: "production",
  65. entry: {},
  66. output: {
  67. path: path.join(projectBasePath, "dist"),
  68. publicPath: "/dist",
  69. filename: "[name].js",
  70. chunkFilename: "[name].js",
  71. libraryTarget: "umd",
  72. libraryExport: "default", // TODO: enable
  73. },
  74. target: "web",
  75. node: {
  76. // yaml-js has a reference to `fs`, this is a workaround
  77. fs: "empty",
  78. },
  79. module: {
  80. rules: baseRules,
  81. },
  82. externals: includeDependencies
  83. ? {
  84. // json-react-schema/deeper depends on buffertools, which fails.
  85. buffertools: true,
  86. esprima: true,
  87. }
  88. : (context, request, cb) => {
  89. // webpack injects some stuff into the resulting file,
  90. // these libs need to be pulled in to keep that working.
  91. var exceptionsForWebpack = ["ieee754", "base64-js"]
  92. if (
  93. nodeModules.indexOf(request) !== -1 ||
  94. exceptionsForWebpack.indexOf(request) !== -1
  95. ) {
  96. cb(null, "commonjs " + request)
  97. return
  98. }
  99. cb()
  100. },
  101. resolve: {
  102. modules: [path.join(projectBasePath, "./src"), "node_modules"],
  103. extensions: [".web.js", ".js", ".jsx", ".json", ".less"],
  104. // these aliases make sure that we don't bundle same libraries twice
  105. // when the versions of these libraries diverge between swagger-js and swagger-ui
  106. alias: {
  107. "@babel/runtime-corejs2": path.resolve(__dirname, "..", "node_modules/@babel/runtime-corejs2"),
  108. "js-yaml": path.resolve(__dirname, "..", "node_modules/js-yaml"),
  109. "lodash": path.resolve(__dirname, "..", "node_modules/lodash")
  110. },
  111. },
  112. // If we're mangling, size is a concern -- so use trace-only sourcemaps
  113. // Otherwise, provide heavy souremaps suitable for development
  114. devtool: sourcemaps
  115. ? minimize
  116. ? "nosource-source-map"
  117. : "module-source-map"
  118. : false,
  119. performance: {
  120. hints: "error",
  121. maxEntrypointSize: 1024000,
  122. maxAssetSize: 1024000,
  123. },
  124. optimization: {
  125. minimize: !!minimize,
  126. minimizer: [
  127. compiler =>
  128. new TerserPlugin({
  129. cache: true,
  130. sourceMap: sourcemaps,
  131. terserOptions: {
  132. mangle: !!mangle,
  133. },
  134. }).apply(compiler)
  135. ],
  136. },
  137. },
  138. customConfig
  139. )
  140. // deepExtend mangles Plugin instances, this doesn't
  141. completeConfig.plugins = plugins.concat(customConfig.plugins || [])
  142. return completeConfig
  143. }