webpack.config.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*eslint-env node*/
  2. /*eslint no-var:0 import/no-nodejs-modules: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 WEBPACK_DEV_PORT = process.env.WEBPACK_DEV_PORT;
  17. var SENTRY_DEVSERVER_PORT = process.env.SENTRY_DEVSERVER_PORT;
  18. var USE_HOT_MODULE_RELOAD = !IS_PRODUCTION && WEBPACK_DEV_PORT && SENTRY_DEVSERVER_PORT;
  19. var WITH_CSS_SOURCEMAPS = !!process.env.WITH_CSS_SOURCEMAPS || IS_PRODUCTION;
  20. var babelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc')));
  21. babelConfig.cacheDirectory = true;
  22. // only extract po files if we need to
  23. if (process.env.SENTRY_EXTRACT_TRANSLATIONS === '1') {
  24. babelConfig.plugins.push([
  25. 'babel-gettext-extractor',
  26. {
  27. fileName: 'build/javascript.po',
  28. baseDirectory: path.join(__dirname, 'src/sentry'),
  29. functionNames: {
  30. gettext: ['msgid'],
  31. ngettext: ['msgid', 'msgid_plural', 'count'],
  32. gettextComponentTemplate: ['msgid'],
  33. t: ['msgid'],
  34. tn: ['msgid', 'msgid_plural', 'count'],
  35. tct: ['msgid'],
  36. },
  37. },
  38. ]);
  39. }
  40. var appEntry = {
  41. app: ['app'],
  42. vendor: [
  43. 'babel-polyfill',
  44. // Yes this is included in prod builds, but has no effect on render and build size in prod
  45. 'react-hot-loader/patch',
  46. 'bootstrap/js/dropdown',
  47. 'bootstrap/js/tab',
  48. 'bootstrap/js/tooltip',
  49. 'bootstrap/js/alert',
  50. 'crypto-js/md5',
  51. 'jed',
  52. 'jquery',
  53. 'marked',
  54. 'moment',
  55. 'moment-timezone',
  56. 'raven-js',
  57. 'react',
  58. 'react-dom',
  59. 'react-dom/server',
  60. 'react-document-title',
  61. 'react-router',
  62. 'react-bootstrap/lib/Modal',
  63. 'react-sparklines',
  64. 'reflux',
  65. 'select2',
  66. 'vendor/simple-slider/simple-slider',
  67. 'ios-device-list',
  68. ],
  69. };
  70. // dynamically iterate over locale files and add to `entry` appConfig
  71. var localeCatalogPath = path.join(__dirname, 'src', 'sentry', 'locale', 'catalogs.json');
  72. var localeCatalog = JSON.parse(fs.readFileSync(localeCatalogPath, 'utf8'));
  73. var localeEntries = [];
  74. localeCatalog.supported_locales.forEach(function(locale) {
  75. if (locale === 'en') return;
  76. // Django locale names are "zh_CN", moment's are "zh-cn"
  77. var normalizedLocale = locale.toLowerCase().replace('_', '-');
  78. appEntry['locale/' + normalizedLocale] = [
  79. 'moment/locale/' + normalizedLocale,
  80. 'sentry-locale/' + locale + '/LC_MESSAGES/django.po', // relative to static/sentry
  81. ];
  82. localeEntries.push('locale/' + normalizedLocale);
  83. });
  84. /**
  85. * Main Webpack config for Sentry React SPA.
  86. */
  87. var appConfig = {
  88. entry: appEntry,
  89. context: path.join(__dirname, staticPrefix),
  90. module: {
  91. rules: [
  92. {
  93. test: /\.jsx?$/,
  94. loader: 'babel-loader',
  95. include: path.join(__dirname, staticPrefix),
  96. exclude: /(vendor|node_modules|dist)/,
  97. query: babelConfig,
  98. },
  99. {
  100. test: /\.po$/,
  101. loader: 'po-catalog-loader',
  102. query: {
  103. referenceExtensions: ['.js', '.jsx'],
  104. domain: 'sentry',
  105. },
  106. },
  107. {
  108. test: /\.json$/,
  109. loader: 'json-loader',
  110. },
  111. {
  112. test: /app\/icons\/.*\.svg$/,
  113. use: [
  114. {
  115. loader: 'svg-sprite-loader',
  116. },
  117. {
  118. loader: 'svgo-loader',
  119. },
  120. ],
  121. },
  122. // loader for dynamic styles imported into components (embedded as js)
  123. {
  124. test: /\.less$/,
  125. use: [
  126. {
  127. loader: 'style-loader',
  128. },
  129. {
  130. loader: 'css-loader',
  131. options: {
  132. minimize: IS_PRODUCTION,
  133. },
  134. },
  135. {
  136. loader: 'less-loader',
  137. },
  138. ],
  139. },
  140. {
  141. test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
  142. exclude: /app\/icons\/.*\.svg$/,
  143. loader: 'file-loader?name=' + '[name].[ext]',
  144. },
  145. ],
  146. noParse: [
  147. // don't parse known, pre-built javascript files (improves webpack perf)
  148. /dist\/jquery\.js/,
  149. /jed\/jed\.js/,
  150. /marked\/lib\/marked\.js/,
  151. ],
  152. },
  153. plugins: [
  154. new LodashModuleReplacementPlugin({
  155. collections: true,
  156. currying: true, // these are enabled to support lodash/fp/ features
  157. flattening: true, // used by a dependency of react-mentions
  158. }),
  159. new webpack.optimize.CommonsChunkPlugin({
  160. names: localeEntries.concat(['vendor']), // 'vendor' must be last entry
  161. }),
  162. new webpack.ProvidePlugin({
  163. $: 'jquery',
  164. jQuery: 'jquery',
  165. 'window.jQuery': 'jquery',
  166. 'root.jQuery': 'jquery',
  167. Raven: 'raven-js',
  168. }),
  169. new ExtractTextPlugin('[name].css'),
  170. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // ignore moment.js locale files
  171. new webpack.DefinePlugin({
  172. 'process.env': {
  173. NODE_ENV: JSON.stringify(process.env.NODE_ENV),
  174. IS_PERCY: JSON.stringify(
  175. process.env.CI && !!process.env.PERCY_TOKEN && !!process.env.TRAVIS
  176. ),
  177. },
  178. }),
  179. // restrict translation files pulled into dist/app.js to only those specified
  180. // in locale/catalogs.json
  181. new webpack.ContextReplacementPlugin(
  182. /locale$/,
  183. path.join(__dirname, 'src', 'sentry', 'locale', path.sep),
  184. true,
  185. new RegExp('(' + localeCatalog.supported_locales.join('|') + ')/.*\\.po$')
  186. ),
  187. ],
  188. resolve: {
  189. alias: {
  190. 'sentry-locale': path.join(__dirname, 'src', 'sentry', 'locale'),
  191. 'integration-docs-platforms': IS_TEST
  192. ? path.join(__dirname, 'tests/fixtures/integration-docs/_platforms.json')
  193. : path.join(__dirname, 'src/sentry/integration-docs/_platforms.json'),
  194. },
  195. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  196. extensions: ['.less', '.jsx', '.js', '.json'],
  197. },
  198. output: {
  199. path: distPath,
  200. filename: '[name].js',
  201. libraryTarget: 'var',
  202. library: 'exports',
  203. sourceMapFilename: '[name].js.map',
  204. },
  205. devtool: IS_PRODUCTION ? '#source-map' : '#cheap-module-eval-source-map',
  206. };
  207. /**
  208. * Webpack entry for password strength checker
  209. */
  210. var pwConfig = {
  211. entry: {
  212. pwstrength: './index',
  213. },
  214. context: path.resolve(path.join(__dirname, staticPrefix), 'js', 'pwstrength'),
  215. module: {},
  216. plugins: [],
  217. resolve: {
  218. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  219. extensions: ['.js'],
  220. },
  221. output: {
  222. path: distPath,
  223. filename: '[name].js',
  224. libraryTarget: 'window',
  225. library: 'sentrypw',
  226. sourceMapFilename: '[name].js.map',
  227. },
  228. devtool: IS_PRODUCTION ? '#source-map' : '#cheap-source-map',
  229. };
  230. /**
  231. * Legacy CSS Webpack appConfig for Django-powered views.
  232. * This generates a single "sentry.css" file that imports ALL component styles
  233. * for use on Django-powered pages.
  234. */
  235. var legacyCssConfig = {
  236. entry: {
  237. sentry: 'less/sentry.less',
  238. },
  239. context: path.join(__dirname, staticPrefix),
  240. output: {
  241. path: distPath,
  242. filename: '[name].css',
  243. },
  244. plugins: [new ExtractTextPlugin('[name].css')],
  245. resolve: {
  246. extensions: ['.less', '.js'],
  247. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  248. },
  249. module: {
  250. rules: [
  251. {
  252. test: /\.less$/,
  253. include: path.join(__dirname, staticPrefix),
  254. use: ExtractTextPlugin.extract({
  255. fallback: 'style-loader?sourceMap=false',
  256. use: [
  257. {
  258. loader: 'css-loader',
  259. options: {
  260. sourceMap: WITH_CSS_SOURCEMAPS,
  261. minimize: IS_PRODUCTION,
  262. },
  263. },
  264. {
  265. loader: 'less-loader',
  266. options: {
  267. sourceMap: WITH_CSS_SOURCEMAPS,
  268. },
  269. },
  270. ],
  271. }),
  272. },
  273. {
  274. test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
  275. loader: 'file-loader?name=' + '[name].[ext]',
  276. },
  277. ],
  278. },
  279. devtool: WITH_CSS_SOURCEMAPS ? '#source-map' : undefined,
  280. };
  281. // Dev only! Hot module reloading
  282. if (USE_HOT_MODULE_RELOAD) {
  283. // Otherwise with hot reloads we get module ID number
  284. appConfig.plugins.push(new webpack.NamedModulesPlugin());
  285. // HMR
  286. appConfig.plugins.push(new webpack.HotModuleReplacementPlugin());
  287. appConfig.devServer = {
  288. headers: {
  289. 'Access-Control-Allow-Origin': '*',
  290. 'Access-Control-Allow-Credentials': 'true',
  291. },
  292. // Required for getsentry
  293. disableHostCheck: true,
  294. contentBase: './src/sentry/static/sentry',
  295. hot: true,
  296. // If below is false, will reload on errors
  297. hotOnly: true,
  298. port: WEBPACK_DEV_PORT,
  299. };
  300. // Required, without this we get this on updates:
  301. // [HMR] Update failed: SyntaxError: Unexpected token < in JSON at position 12
  302. appConfig.output.publicPath = 'http://localhost:' + WEBPACK_DEV_PORT + '/';
  303. }
  304. var minificationPlugins = [
  305. // This compression-webpack-plugin generates pre-compressed files
  306. // ending in .gz, to be picked up and served by our internal static media
  307. // server as well as nginx when paired with the gzip_static module.
  308. new (require('compression-webpack-plugin'))({
  309. algorithm: function(buffer, options, callback) {
  310. require('zlib').gzip(buffer, callback);
  311. },
  312. regExp: /\.(js|map|css|svg|html|txt|ico|eot|ttf)$/,
  313. }),
  314. // Disable annoying UglifyJS warnings that pollute Travis log output
  315. // NOTE: This breaks -p in webpack 2. Must call webpack w/ NODE_ENV=production for minification.
  316. new webpack.optimize.UglifyJsPlugin({
  317. compress: {
  318. warnings: false,
  319. },
  320. // https://github.com/webpack/webpack/blob/951a7603d279c93c936e4b8b801a355dc3e26292/bin/convert-argv.js#L442
  321. sourceMap:
  322. appConfig.devtool &&
  323. (appConfig.devtool.indexOf('sourcemap') >= 0 ||
  324. appConfig.devtool.indexOf('source-map') >= 0),
  325. }),
  326. ];
  327. if (IS_PRODUCTION) {
  328. // NOTE: can't do plugins.push(Array) because webpack/webpack#2217
  329. minificationPlugins.forEach(function(plugin) {
  330. appConfig.plugins.push(plugin);
  331. pwConfig.plugins.push(plugin);
  332. legacyCssConfig.plugins.push(plugin);
  333. });
  334. }
  335. module.exports = [appConfig, legacyCssConfig, pwConfig];