25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

156 lines
3.5 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?limit=10000" },
  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. beautify: !specialOptions.mangle,
  65. keep_fnames: true
  66. },
  67. sourceMap: true,
  68. }),
  69. new webpack.LoaderOptionsPlugin({
  70. options: {
  71. context: __dirname
  72. }
  73. })
  74. )
  75. plugins.push( new webpack.NoEmitOnErrorsPlugin())
  76. } else { // development mode
  77. plugins.push(new CopyWebpackPlugin([ { from: "test/e2e/specs", to: "test-specs" } ]))
  78. }
  79. plugins.push(
  80. new webpack.DefinePlugin({
  81. "process.env": {
  82. NODE_ENV: specialOptions.minimize ? JSON.stringify("production") : null,
  83. WEBPACK_INLINE_STYLES: !specialOptions.separateStylesheets
  84. },
  85. "buildInfo": JSON.stringify({
  86. PACKAGE_VERSION: (pkg.version),
  87. GIT_COMMIT: gitInfo.hash,
  88. GIT_DIRTY: gitInfo.dirty,
  89. HOSTNAME: os.hostname(),
  90. BUILD_TIME: new Date().toUTCString()
  91. })
  92. }))
  93. delete options._special
  94. var completeConfig = deepExtend({
  95. entry: {},
  96. output: {
  97. path: path.join(__dirname, "dist"),
  98. publicPath: "/",
  99. filename: "[name].js",
  100. chunkFilename: "[name].js"
  101. },
  102. target: "web",
  103. // yaml-js has a reference to `fs`, this is a workaround
  104. node: {
  105. fs: "empty"
  106. },
  107. module: {
  108. rules: commonRules.concat(rules),
  109. },
  110. resolveLoader: {
  111. modules: [path.join(__dirname, "node_modules")],
  112. },
  113. externals: {
  114. "buffertools": true // json-react-schema/deeper depends on buffertools, which fails.
  115. },
  116. resolve: {
  117. modules: [
  118. path.join(__dirname, "./src"),
  119. "node_modules"
  120. ],
  121. extensions: [".web.js", ".js", ".jsx", ".json", ".less"],
  122. alias: {}
  123. },
  124. devtool: specialOptions.sourcemaps ? "nosource-source-map" : false,
  125. plugins,
  126. }, options)
  127. return completeConfig
  128. }