webpack.config.js 11 KB

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