webpack.config.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*eslint-env node*/
  2. /*eslint no-var:0*/
  3. var path = require('path'),
  4. fs = require('fs'),
  5. webpack = require('webpack'),
  6. ExtractTextPlugin = require('extract-text-webpack-plugin'),
  7. LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
  8. var staticPrefix = 'src/sentry/static/sentry',
  9. distPath = path.join(__dirname, staticPrefix, 'dist');
  10. // this is set by setup.py sdist
  11. if (process.env.SENTRY_STATIC_DIST_PATH) {
  12. distPath = process.env.SENTRY_STATIC_DIST_PATH;
  13. }
  14. var IS_PRODUCTION = process.env.NODE_ENV === 'production';
  15. var IS_TEST = process.env.NODE_ENV === 'TEST' || process.env.TEST_SUITE;
  16. var REFRESH = process.env.WEBPACK_LIVERELOAD === '1';
  17. var babelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc')));
  18. babelConfig.cacheDirectory = true;
  19. // only extract po files if we need to
  20. if (process.env.SENTRY_EXTRACT_TRANSLATIONS === '1') {
  21. babelConfig.plugins.push([
  22. 'babel-gettext-extractor',
  23. {
  24. fileName: 'build/javascript.po',
  25. baseDirectory: path.join(__dirname, 'src/sentry'),
  26. functionNames: {
  27. gettext: ['msgid'],
  28. ngettext: ['msgid', 'msgid_plural', 'count'],
  29. gettextComponentTemplate: ['msgid'],
  30. t: ['msgid'],
  31. tn: ['msgid', 'msgid_plural', 'count'],
  32. tct: ['msgid']
  33. }
  34. }
  35. ]);
  36. }
  37. var appEntry = {
  38. app: 'app',
  39. vendor: [
  40. 'babel-polyfill',
  41. 'bootstrap/js/dropdown',
  42. 'bootstrap/js/tab',
  43. 'bootstrap/js/tooltip',
  44. 'bootstrap/js/alert',
  45. 'crypto-js/md5',
  46. 'jed',
  47. 'jquery',
  48. 'marked',
  49. 'moment',
  50. 'moment-timezone',
  51. 'raven-js',
  52. 'react',
  53. 'react-dom',
  54. 'react-dom/server',
  55. 'react-document-title',
  56. 'react-router',
  57. 'react-bootstrap/lib/Modal',
  58. 'react-sparklines',
  59. 'reflux',
  60. 'select2',
  61. 'vendor/simple-slider/simple-slider',
  62. 'ios-device-list'
  63. ]
  64. };
  65. // dynamically iterate over locale files and add to `entry` appConfig
  66. var localeCatalogPath = path.join(__dirname, 'src', 'sentry', 'locale', 'catalogs.json');
  67. var localeCatalog = JSON.parse(fs.readFileSync(localeCatalogPath, 'utf8'));
  68. var localeEntries = [];
  69. localeCatalog.supported_locales.forEach(function(locale) {
  70. if (locale === 'en') return;
  71. // Django locale names are "zh_CN", moment's are "zh-cn"
  72. var normalizedLocale = locale.toLowerCase().replace('_', '-');
  73. appEntry['locale/' + normalizedLocale] = [
  74. 'moment/locale/' + normalizedLocale,
  75. 'sentry-locale/' + locale + '/LC_MESSAGES/django.po' // relative to static/sentry
  76. ];
  77. localeEntries.push('locale/' + normalizedLocale);
  78. });
  79. /**
  80. * Main Webpack config for Sentry React SPA.
  81. */
  82. var appConfig = {
  83. entry: appEntry,
  84. context: path.join(__dirname, staticPrefix),
  85. module: {
  86. rules: [
  87. {
  88. test: /\.jsx?$/,
  89. loader: 'babel-loader',
  90. include: path.join(__dirname, staticPrefix),
  91. exclude: /(vendor|node_modules|dist)/,
  92. query: babelConfig
  93. },
  94. {
  95. test: /\.po$/,
  96. loader: 'po-catalog-loader',
  97. query: {
  98. referenceExtensions: ['.js', '.jsx'],
  99. domain: 'sentry'
  100. }
  101. },
  102. {
  103. test: /\.json$/,
  104. loader: 'json-loader'
  105. },
  106. // loader for dynamic styles imported into components (embedded as js)
  107. {
  108. test: /\.less$/,
  109. use: [
  110. {
  111. loader: 'style-loader'
  112. },
  113. {
  114. loader: 'css-loader' + (IS_PRODUCTION ? '?minimize=true' : '')
  115. },
  116. {
  117. loader: 'less-loader'
  118. }
  119. ]
  120. },
  121. {
  122. test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
  123. loader: 'file-loader?name=' + '[name].[ext]'
  124. }
  125. ],
  126. noParse: [
  127. // don't parse known, pre-built javascript files (improves webpack perf)
  128. /dist\/jquery\.js/,
  129. /jed\/jed\.js/,
  130. /marked\/lib\/marked\.js/
  131. ]
  132. },
  133. plugins: [
  134. new LodashModuleReplacementPlugin({
  135. collections: true,
  136. currying: true, // these are enabled to support lodash/fp/ features
  137. flattening: true // used by a dependency of react-mentions
  138. }),
  139. new webpack.optimize.CommonsChunkPlugin({
  140. names: localeEntries.concat(['vendor']) // 'vendor' must be last entry
  141. }),
  142. new webpack.ProvidePlugin({
  143. $: 'jquery',
  144. jQuery: 'jquery',
  145. 'window.jQuery': 'jquery',
  146. 'root.jQuery': 'jquery',
  147. Raven: 'raven-js'
  148. }),
  149. new ExtractTextPlugin('[name].css'),
  150. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // ignore moment.js locale files
  151. new webpack.DefinePlugin({
  152. 'process.env': {
  153. NODE_ENV: JSON.stringify(process.env.NODE_ENV)
  154. }
  155. }),
  156. // restrict translation files pulled into dist/app.js to only those specified
  157. // in locale/catalogs.json
  158. new webpack.ContextReplacementPlugin(
  159. /locale$/,
  160. path.join(__dirname, 'src', 'sentry', 'locale', path.sep),
  161. true,
  162. new RegExp('(' + localeCatalog.supported_locales.join('|') + ')\/.*\\.po$')
  163. )
  164. ],
  165. resolve: {
  166. alias: {
  167. 'sentry-locale': path.join(__dirname, 'src', 'sentry', 'locale'),
  168. 'integration-docs-platforms': IS_TEST
  169. ? path.join(__dirname, 'tests/fixtures/_platforms.json')
  170. : path.join(__dirname, 'src/sentry/integration-docs/_platforms.json')
  171. },
  172. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  173. extensions: ['.less', '.jsx', '.js', '.json']
  174. },
  175. output: {
  176. path: distPath,
  177. filename: '[name].js',
  178. libraryTarget: 'var',
  179. library: 'exports',
  180. sourceMapFilename: '[name].js.map'
  181. },
  182. devtool: IS_PRODUCTION ? '#source-map' : '#cheap-source-map'
  183. };
  184. if (!IS_PRODUCTION && REFRESH) {
  185. appConfig.plugins.push(
  186. new (require('webpack-livereload-plugin'))({appendScriptTag: true})
  187. );
  188. }
  189. var minificationPlugins = [
  190. // This compression-webpack-plugin generates pre-compressed files
  191. // ending in .gz, to be picked up and served by our internal static media
  192. // server as well as nginx when paired with the gzip_static module.
  193. new (require('compression-webpack-plugin'))({
  194. algorithm: function(buffer, options, callback) {
  195. require('zlib').gzip(buffer, callback);
  196. },
  197. regExp: /\.(js|map|css|svg|html|txt|ico|eot|ttf)$/
  198. }),
  199. // Disable annoying UglifyJS warnings that pollute Travis log output
  200. // NOTE: This breaks -p in webpack 2. Must call webpack w/ NODE_ENV=production for minification.
  201. new webpack.optimize.UglifyJsPlugin({
  202. compress: {
  203. warnings: false
  204. },
  205. // https://github.com/webpack/webpack/blob/951a7603d279c93c936e4b8b801a355dc3e26292/bin/convert-argv.js#L442
  206. sourceMap: appConfig.devtool &&
  207. (appConfig.devtool.indexOf('sourcemap') >= 0 ||
  208. appConfig.devtool.indexOf('source-map') >= 0)
  209. })
  210. ];
  211. if (IS_PRODUCTION) {
  212. // NOTE: can't do plugins.push(Array) because webpack/webpack#2217
  213. minificationPlugins.forEach(function(plugin) {
  214. appConfig.plugins.push(plugin);
  215. });
  216. }
  217. /**
  218. * Legacy CSS Webpack appConfig for Django-powered views.
  219. * This generates a single "sentry.css" file that imports ALL component styles
  220. * for use on Django-powered pages.
  221. */
  222. var legacyCssConfig = {
  223. entry: {
  224. sentry: 'less/sentry.less'
  225. },
  226. context: path.join(__dirname, staticPrefix),
  227. output: {
  228. path: distPath,
  229. filename: '[name].css'
  230. },
  231. plugins: [new ExtractTextPlugin('[name].css')],
  232. resolve: {
  233. extensions: ['.less'],
  234. modules: [path.join(__dirname, staticPrefix), 'node_modules']
  235. },
  236. module: {
  237. rules: [
  238. {
  239. test: /\.less$/,
  240. include: path.join(__dirname, staticPrefix),
  241. loader: ExtractTextPlugin.extract({
  242. fallbackLoader: 'style-loader?sourceMap=false',
  243. loader: 'css-loader' + (IS_PRODUCTION ? '?minimize=true' : '') + '!less-loader'
  244. })
  245. },
  246. {
  247. test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
  248. loader: 'file-loader?name=' + '[name].[ext]'
  249. }
  250. ]
  251. }
  252. };
  253. if (IS_PRODUCTION) {
  254. // NOTE: can't do plugins.push(Array) because webpack/webpack#2217
  255. minificationPlugins.forEach(function(plugin) {
  256. legacyCssConfig.plugins.push(plugin);
  257. });
  258. }
  259. module.exports = [appConfig, legacyCssConfig];