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.
 
 
 
 

165 lines
3.8 KiB

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