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.

make-webpack-config.js 3.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var path = require("path")
  2. var webpack = require("webpack")
  3. var ExtractTextPlugin = require("extract-text-webpack-plugin")
  4. var deepExtend = require("deep-extend")
  5. const {gitDescribeSync} = require("git-describe")
  6. const os = require("os")
  7. var pkg = require("./package.json")
  8. let gitInfo
  9. try {
  10. gitInfo = gitDescribeSync(__dirname)
  11. } catch(e) {
  12. gitInfo = {
  13. hash: "noGit",
  14. dirty: false
  15. }
  16. }
  17. var commonRules = [
  18. { test: /\.(js(x)?)(\?.*)?$/,
  19. use: [{
  20. loader: "babel-loader",
  21. options: {
  22. retainLines: true
  23. }
  24. }],
  25. include: [ path.join(__dirname, "src") ]
  26. },
  27. { test: /\.(txt|yaml)(\?.*)?$/,
  28. loader: "raw-loader" },
  29. { test: /\.(png|jpg|jpeg|gif|svg)(\?.*)?$/,
  30. loader: "url-loader?limit=10000" },
  31. { test: /\.(woff|woff2)(\?.*)?$/,
  32. loader: "url-loader?limit=100000" },
  33. { test: /\.(ttf|eot)(\?.*)?$/,
  34. loader: "file-loader" }
  35. ]
  36. module.exports = function(rules, options) {
  37. // Special options, that have logic in this file
  38. // ...with defaults
  39. var specialOptions = deepExtend({}, {
  40. hot: false,
  41. separateStylesheets: true,
  42. minimize: false,
  43. longTermCaching: false,
  44. sourcemaps: false,
  45. }, options._special)
  46. var plugins = []
  47. if( specialOptions.separateStylesheets ) {
  48. plugins.push(new ExtractTextPlugin({
  49. filename: "[name].css" + (specialOptions.longTermCaching ? "?[contenthash]" : ""),
  50. allChunks: true
  51. }))
  52. }
  53. if( specialOptions.minimize ) {
  54. plugins.push(
  55. new webpack.optimize.UglifyJsPlugin({
  56. sourceMap: true,
  57. }),
  58. new webpack.LoaderOptionsPlugin({
  59. options: {
  60. context: __dirname
  61. }
  62. })
  63. )
  64. plugins.push( new webpack.NoEmitOnErrorsPlugin())
  65. }
  66. plugins.push(
  67. new webpack.DefinePlugin({
  68. "process.env": {
  69. NODE_ENV: specialOptions.minimize ? JSON.stringify("production") : null,
  70. WEBPACK_INLINE_STYLES: !specialOptions.separateStylesheets
  71. },
  72. "buildInfo": JSON.stringify({
  73. PACKAGE_VERSION: (pkg.version),
  74. GIT_COMMIT: gitInfo.hash,
  75. GIT_DIRTY: gitInfo.dirty,
  76. HOSTNAME: os.hostname(),
  77. BUILD_TIME: new Date().toUTCString()
  78. })
  79. }))
  80. delete options._special
  81. var completeConfig = deepExtend({
  82. entry: {},
  83. output: {
  84. path: path.join(__dirname, "dist"),
  85. publicPath: "/",
  86. filename: "[name].js",
  87. chunkFilename: "[name].js"
  88. },
  89. target: "web",
  90. // yaml-js has a reference to `fs`, this is a workaround
  91. node: {
  92. fs: "empty"
  93. },
  94. module: {
  95. rules: commonRules.concat(rules),
  96. },
  97. resolveLoader: {
  98. modules: [path.join(__dirname, "node_modules")],
  99. },
  100. externals: {
  101. "buffertools": true // json-react-schema/deeper depends on buffertools, which fails.
  102. },
  103. resolve: {
  104. modules: [
  105. path.join(__dirname, "./src"),
  106. "node_modules"
  107. ],
  108. extensions: [".web.js", ".js", ".jsx", ".json", ".less"],
  109. alias: {
  110. base: "getbase/src/less/base",
  111. }
  112. },
  113. devtool: specialOptions.sourcemaps ? "cheap-module-source-map" : null,
  114. plugins,
  115. }, options)
  116. return completeConfig
  117. }