webpack.config.js 10 KB

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