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.
 
 
 
 

166 lines
4.2 KiB

  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. var autoprefixer = require('autoprefixer')
  6. const {gitDescribeSync} = require('git-describe');
  7. var loadersByExtension = require('./build-tools/loadersByExtension')
  8. var pkg = require('./package.json')
  9. let gitInfo
  10. try {
  11. gitInfo = gitDescribeSync(__dirname)
  12. } catch(e) {
  13. gitInfo = {
  14. hash: 'noGit',
  15. dirty: false
  16. }
  17. }
  18. module.exports = function(options) {
  19. // Special options, that have logic in this file
  20. // ...with defaults
  21. var specialOptions = deepExtend({}, {
  22. hot: false,
  23. separateStylesheets: true,
  24. minimize: false,
  25. longTermCaching: false,
  26. sourcemaps: false,
  27. }, options._special)
  28. var loadersMap = {
  29. 'js(x)?': {
  30. loader: 'babel?retainLines=true',
  31. include: [ path.join(__dirname, 'src') ],
  32. },
  33. 'json': 'json-loader',
  34. 'txt|yaml': 'raw-loader',
  35. 'png|jpg|jpeg|gif|svg': specialOptions.disableAssets ? 'null-loader' : 'url-loader?limit=10000',
  36. 'woff|woff2': specialOptions.disableAssets ? 'null-loader' : 'url-loader?limit=100000',
  37. 'ttf|eot': specialOptions.disableAssets ? 'null-loader' : 'file-loader',
  38. "worker.js": ["worker-loader?inline=true", "babel"]
  39. }
  40. var plugins = []
  41. if( specialOptions.separateStylesheets ) {
  42. plugins.push(new ExtractTextPlugin('[name].css' + (specialOptions.longTermCaching ? '?[contenthash]' : ''), {
  43. allChunks: true
  44. }))
  45. }
  46. if( specialOptions.minimize ) {
  47. plugins.push(
  48. new webpack.optimize.UglifyJsPlugin({
  49. compressor: {
  50. warnings: false
  51. }
  52. }),
  53. new webpack.optimize.DedupePlugin()
  54. )
  55. plugins.push( new webpack.NoErrorsPlugin())
  56. }
  57. plugins.push(
  58. new webpack.DefinePlugin({
  59. 'process.env': {
  60. NODE_ENV: specialOptions.minimize ? JSON.stringify('production') : null,
  61. WEBPACK_INLINE_STYLES: !Boolean(specialOptions.separateStylesheets)
  62. },
  63. 'buildInfo': JSON.stringify({
  64. PACKAGE_VERSION: (pkg.version),
  65. GIT_COMMIT: gitInfo.hash,
  66. GIT_DIRTY: gitInfo.dirty
  67. })
  68. }))
  69. var cssLoader = 'css-loader!postcss-loader'
  70. var completeStylesheetLoaders = deepExtend({
  71. 'css': cssLoader,
  72. 'scss': cssLoader + '!' + 'sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true',
  73. 'less': cssLoader + '!' + 'less-loader',
  74. }, specialOptions.stylesheetLoaders)
  75. if(specialOptions.cssModules) {
  76. cssLoader = cssLoader + '?module' + (specialOptions.minimize ? '' : '&localIdentName=[path][name]---[local]---[hash:base64:5]')
  77. }
  78. Object.keys(completeStylesheetLoaders).forEach(function(ext) {
  79. var ori = completeStylesheetLoaders[ext]
  80. if(specialOptions.separateStylesheets) {
  81. completeStylesheetLoaders[ext] = ExtractTextPlugin.extract('style-loader', ori)
  82. } else {
  83. completeStylesheetLoaders[ext] = 'style-loader!' + ori
  84. }
  85. })
  86. var loaders = loadersByExtension(deepExtend({}, loadersMap, specialOptions.loaders, completeStylesheetLoaders))
  87. var extraLoaders = (options.module || {} ).loaders
  88. if(Array.isArray(extraLoaders)) {
  89. loaders = loaders.concat(extraLoaders)
  90. delete options.module.loaders
  91. }
  92. var completeConfig = deepExtend({
  93. entry: {},
  94. output: {
  95. path: path.join(__dirname, 'dist'),
  96. publicPath: '/',
  97. filename: '[name].js',
  98. chunkFilename: '[name].js'
  99. },
  100. target: 'web',
  101. // yaml-js has a reference to `fs`, this is a workaround
  102. node: {
  103. fs: 'empty'
  104. },
  105. module: {
  106. loaders: loaders,
  107. },
  108. resolveLoader: {
  109. root: path.join(__dirname, 'node_modules'),
  110. },
  111. externals: {
  112. 'buffertools': true // json-react-schema/deeper depends on buffertools, which fails.
  113. },
  114. resolve: {
  115. root: path.join(__dirname, './src'),
  116. modulesDirectories: ['node_modules'],
  117. extensions: ["", ".web.js", ".js", ".jsx", ".json", ".less"],
  118. packageAlias: 'browser',
  119. alias: {
  120. base: "getbase/src/less/base"
  121. }
  122. },
  123. postcss: function() {
  124. return [autoprefixer]
  125. },
  126. devtool: specialOptions.sourcemaps ? 'cheap-module-source-map' : null,
  127. plugins,
  128. }, options)
  129. return completeConfig
  130. }