webpack.config.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. '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. 'reflux',
  64. 'vendor/simple-slider/simple-slider',
  65. 'emotion',
  66. 'react-emotion',
  67. 'grid-emotion',
  68. 'emotion-theming',
  69. ],
  70. };
  71. // dynamically iterate over locale files and add to `entry` appConfig
  72. var localeCatalogPath = path.join(__dirname, 'src', 'sentry', 'locale', 'catalogs.json');
  73. var localeCatalog = JSON.parse(fs.readFileSync(localeCatalogPath, 'utf8'));
  74. var localeEntries = [];
  75. localeCatalog.supported_locales.forEach(function(locale) {
  76. if (locale === 'en') return;
  77. // Django locale names are "zh_CN", moment's are "zh-cn"
  78. var normalizedLocale = locale.toLowerCase().replace('_', '-');
  79. appEntry['locale/' + normalizedLocale] = [
  80. 'moment/locale/' + normalizedLocale,
  81. 'sentry-locale/' + locale + '/LC_MESSAGES/django.po', // relative to static/sentry
  82. ];
  83. localeEntries.push('locale/' + normalizedLocale);
  84. });
  85. /**
  86. * Main Webpack config for Sentry React SPA.
  87. */
  88. var appConfig = {
  89. entry: appEntry,
  90. context: path.join(__dirname, staticPrefix),
  91. module: {
  92. rules: [
  93. {
  94. test: /\.jsx?$/,
  95. loader: 'babel-loader',
  96. include: path.join(__dirname, staticPrefix),
  97. exclude: /(vendor|node_modules|dist)/,
  98. query: babelConfig,
  99. },
  100. {
  101. test: /\.po$/,
  102. loader: 'po-catalog-loader',
  103. query: {
  104. referenceExtensions: ['.js', '.jsx'],
  105. domain: 'sentry',
  106. },
  107. },
  108. {
  109. test: /\.json$/,
  110. loader: 'json-loader',
  111. },
  112. {
  113. test: /app\/icons\/.*\.svg$/,
  114. use: [
  115. {
  116. loader: 'svg-sprite-loader',
  117. },
  118. {
  119. loader: 'svgo-loader',
  120. },
  121. ],
  122. },
  123. // loader for dynamic styles imported into components (embedded as js)
  124. {
  125. test: /\.less$/,
  126. use: [
  127. {
  128. loader: 'style-loader',
  129. },
  130. {
  131. loader: 'css-loader',
  132. options: {
  133. minimize: IS_PRODUCTION,
  134. },
  135. },
  136. {
  137. loader: 'less-loader',
  138. },
  139. ],
  140. },
  141. {
  142. test: /\.(woff|woff2|ttf|eot|svg|png|gif|ico|jpg)($|\?)/,
  143. exclude: /app\/icons\/.*\.svg$/,
  144. loader: 'file-loader?name=' + '[name].[ext]',
  145. },
  146. ],
  147. noParse: [
  148. // don't parse known, pre-built javascript files (improves webpack perf)
  149. /dist\/jquery\.js/,
  150. /jed\/jed\.js/,
  151. /marked\/lib\/marked\.js/,
  152. ],
  153. },
  154. plugins: [
  155. new LodashModuleReplacementPlugin({
  156. collections: true,
  157. currying: true, // these are enabled to support lodash/fp/ features
  158. flattening: true, // used by a dependency of react-mentions
  159. shorthands: true,
  160. }),
  161. new webpack.optimize.CommonsChunkPlugin({
  162. names: localeEntries.concat(['vendor']), // 'vendor' must be last entry
  163. }),
  164. new webpack.ProvidePlugin({
  165. $: 'jquery',
  166. jQuery: 'jquery',
  167. 'window.jQuery': 'jquery',
  168. 'root.jQuery': 'jquery',
  169. }),
  170. new ExtractTextPlugin('[name].css'),
  171. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/), // ignore moment.js locale files
  172. new webpack.DefinePlugin({
  173. 'process.env': {
  174. NODE_ENV: JSON.stringify(process.env.NODE_ENV),
  175. IS_PERCY: JSON.stringify(
  176. process.env.CI && !!process.env.PERCY_TOKEN && !!process.env.TRAVIS
  177. ),
  178. },
  179. }),
  180. // restrict translation files pulled into dist/app.js to only those specified
  181. // in locale/catalogs.json
  182. new webpack.ContextReplacementPlugin(
  183. /locale$/,
  184. path.join(__dirname, 'src', 'sentry', 'locale', path.sep),
  185. true,
  186. new RegExp('(' + localeCatalog.supported_locales.join('|') + ')/.*\\.po$')
  187. ),
  188. ],
  189. resolve: {
  190. alias: {
  191. app: path.join(__dirname, 'src', 'sentry', 'static', 'sentry', 'app'),
  192. 'sentry-locale': path.join(__dirname, 'src', 'sentry', 'locale'),
  193. 'integration-docs-platforms': IS_TEST
  194. ? path.join(__dirname, 'tests/fixtures/integration-docs/_platforms.json')
  195. : path.join(__dirname, 'src/sentry/integration-docs/_platforms.json'),
  196. },
  197. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  198. extensions: ['.less', '.jsx', '.js', '.json'],
  199. },
  200. output: {
  201. path: distPath,
  202. filename: '[name].js',
  203. libraryTarget: 'var',
  204. library: 'exports',
  205. sourceMapFilename: '[name].js.map',
  206. },
  207. devtool: IS_PRODUCTION ? '#source-map' : '#cheap-module-eval-source-map',
  208. };
  209. /**
  210. * Webpack entry for password strength checker
  211. */
  212. var pwConfig = {
  213. entry: {
  214. pwstrength: './index',
  215. },
  216. context: path.resolve(path.join(__dirname, staticPrefix), 'js', 'pwstrength'),
  217. module: {},
  218. plugins: [],
  219. resolve: {
  220. modules: [path.join(__dirname, staticPrefix), 'node_modules'],
  221. extensions: ['.js'],
  222. },
  223. output: {
  224. path: distPath,
  225. filename: '[name].js',
  226. libraryTarget: 'window',
  227. library: 'sentrypw',
  228. sourceMapFilename: '[name].js.map',
  229. },
  230. devtool: IS_PRODUCTION ? '#source-map' : '#cheap-source-map',
  231. };
  232. /**
  233. * Legacy CSS Webpack appConfig for Django-powered views.
  234. * This generates a single "sentry.css" file that imports ALL component styles
  235. * for use on Django-powered pages.
  236. */
  237. var legacyCssConfig = {
  238. entry: {
  239. sentry: 'less/sentry.less',
  240. // Below is for old plugins that use select2 when creating a new issue for a plugin
  241. // e.g. Trello, Teamwork
  242. select2: 'less/select2.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];