webpack.dev.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. const merge = require('webpack-merge');
  3. const common = require('./webpack.common.js');
  4. const path = require('path');
  5. const webpack = require('webpack');
  6. const HtmlWebpackPlugin = require("html-webpack-plugin");
  7. const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
  8. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  9. const CleanWebpackPlugin = require('clean-webpack-plugin');
  10. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  11. const TARGET = process.env.npm_lifecycle_event;
  12. const HOT = TARGET === 'start';
  13. const extractSass = new ExtractTextPlugin({
  14. filename: "grafana.[name].css",
  15. disable: HOT
  16. });
  17. const entries = HOT ? {
  18. app: [
  19. 'webpack-dev-server/client?http://localhost:3333',
  20. './public/app/dev.ts',
  21. ],
  22. vendor: require('./dependencies'),
  23. } : {
  24. app: './public/app/index.ts',
  25. dark: './public/sass/grafana.dark.scss',
  26. light: './public/sass/grafana.light.scss',
  27. vendor: require('./dependencies'),
  28. };
  29. const output = HOT ? {
  30. path: path.resolve(__dirname, '../../public/build'),
  31. filename: '[name].[hash].js',
  32. publicPath: "/public/build/",
  33. } : {
  34. path: path.resolve(__dirname, '../../public/build'),
  35. filename: '[name].[hash].js',
  36. // Keep publicPath relative for host.com/grafana/ deployments
  37. publicPath: "public/build/",
  38. };
  39. module.exports = merge(common, {
  40. devtool: "cheap-module-source-map",
  41. entry: entries,
  42. output: output,
  43. resolve: {
  44. extensions: ['.scss', '.ts', '.tsx', '.es6', '.js', '.json', '.svg', '.woff2', '.png'],
  45. },
  46. devServer: {
  47. publicPath: '/public/build/',
  48. hot: HOT,
  49. port: 3333,
  50. proxy: {
  51. '!/public/build': 'http://localhost:3000'
  52. }
  53. },
  54. module: {
  55. rules: [
  56. {
  57. test: /\.tsx?$/,
  58. enforce: 'pre',
  59. exclude: /node_modules/,
  60. use: {
  61. loader: 'tslint-loader',
  62. options: {
  63. emitErrors: true,
  64. typeCheck: false,
  65. }
  66. }
  67. },
  68. {
  69. test: /\.tsx?$/,
  70. exclude: /node_modules/,
  71. use: {
  72. loader: 'awesome-typescript-loader',
  73. options: {
  74. useCache: true,
  75. useBabel: HOT,
  76. babelOptions: {
  77. babelrc: false,
  78. plugins: [
  79. 'syntax-dynamic-import',
  80. 'react-hot-loader/babel'
  81. ]
  82. }
  83. },
  84. }
  85. },
  86. require('./sass.rule.js')({
  87. sourceMap: true, minimize: false, preserveUrl: HOT
  88. }, extractSass),
  89. {
  90. test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
  91. loader: 'file-loader'
  92. },
  93. {
  94. test: /\.(png|jpg|gif)$/,
  95. use: [
  96. {
  97. loader: 'file-loader',
  98. options: {}
  99. }
  100. ]
  101. },
  102. ]
  103. },
  104. plugins: [
  105. new CleanWebpackPlugin('../public/build', { allowExternal: true }),
  106. extractSass,
  107. new HtmlWebpackPlugin({
  108. filename: path.resolve(__dirname, '../../public/views/index.html'),
  109. template: path.resolve(__dirname, '../../public/views/index.template.html'),
  110. inject: 'body',
  111. chunks: ['manifest', 'vendor', 'app'],
  112. alwaysWriteToDisk: HOT
  113. }),
  114. new HtmlWebpackHarddiskPlugin(),
  115. new webpack.NamedModulesPlugin(),
  116. new webpack.HotModuleReplacementPlugin(),
  117. new webpack.DefinePlugin({
  118. 'GRAFANA_THEME': JSON.stringify(process.env.GRAFANA_THEME || 'dark'),
  119. 'process.env': {
  120. 'NODE_ENV': JSON.stringify('development')
  121. }
  122. }),
  123. new webpack.optimize.CommonsChunkPlugin({
  124. names: ['vendor', 'manifest'],
  125. }),
  126. // new BundleAnalyzerPlugin({
  127. // analyzerPort: 8889
  128. // })
  129. ]
  130. });