webpack.config.dev.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. 'use strict';
  2. const autoprefixer = require('autoprefixer');
  3. const path = require('path');
  4. const webpack = require('webpack');
  5. const HtmlWebpackPlugin = require('html-webpack-plugin');
  6. const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
  7. const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
  8. const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
  9. const eslintFormatter = require('react-dev-utils/eslintFormatter');
  10. const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
  11. const getClientEnvironment = require('./env');
  12. const paths = require('./paths');
  13. // Webpack uses `publicPath` to determine where the app is being served from.
  14. // In development, we always serve from the root. This makes config easier.
  15. const publicPath = '/';
  16. // `publicUrl` is just like `publicPath`, but we will provide it to our app
  17. // as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
  18. // Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
  19. const publicUrl = '';
  20. // Get environment variables to inject into our app.
  21. const env = getClientEnvironment(publicUrl);
  22. // This is the development configuration.
  23. // It is focused on developer experience and fast rebuilds.
  24. // The production configuration is different and lives in a separate file.
  25. module.exports = {
  26. // You may want 'eval' instead if you prefer to see the compiled output in DevTools.
  27. // See the discussion in https://github.com/facebookincubator/create-react-app/issues/343.
  28. devtool: 'cheap-module-source-map',
  29. // These are the "entry points" to our application.
  30. // This means they will be the "root" imports that are included in JS bundle.
  31. // The first two entry points enable "hot" CSS and auto-refreshes for JS.
  32. entry: [
  33. // We ship a few polyfills by default:
  34. require.resolve('./polyfills'),
  35. // Include an alternative client for WebpackDevServer. A client's job is to
  36. // connect to WebpackDevServer by a socket and get notified about changes.
  37. // When you save a file, the client will either apply hot updates (in case
  38. // of CSS changes), or refresh the page (in case of JS changes). When you
  39. // make a syntax error, this client will display a syntax error overlay.
  40. // Note: instead of the default WebpackDevServer client, we use a custom one
  41. // to bring better experience for Create React App users. You can replace
  42. // the line below with these two lines if you prefer the stock client:
  43. // require.resolve('webpack-dev-server/client') + '?/',
  44. // require.resolve('webpack/hot/dev-server'),
  45. require.resolve('react-dev-utils/webpackHotDevClient'),
  46. // Finally, this is your app's code:
  47. paths.appIndexJs,
  48. // We include the app code last so that if there is a runtime error during
  49. // initialization, it doesn't blow up the WebpackDevServer client, and
  50. // changing JS code would still trigger a refresh.
  51. ],
  52. output: {
  53. // Add /* filename */ comments to generated require()s in the output.
  54. pathinfo: true,
  55. // This does not produce a real file. It's just the virtual path that is
  56. // served by WebpackDevServer in development. This is the JS bundle
  57. // containing code from all our entry points, and the Webpack runtime.
  58. filename: 'static/js/bundle.js',
  59. // There are also additional JS chunk files if you use code splitting.
  60. chunkFilename: 'static/js/[name].chunk.js',
  61. // This is the URL that app is served from. We use "/" in development.
  62. publicPath: publicPath,
  63. // Point sourcemap entries to original disk location (format as URL on Windows)
  64. devtoolModuleFilenameTemplate: info =>
  65. path.resolve(info.absoluteResourcePath).replace(/\\/g, '/'),
  66. },
  67. resolve: {
  68. // This allows you to set a fallback for where Webpack should look for modules.
  69. // We placed these paths second because we want `node_modules` to "win"
  70. // if there are any conflicts. This matches Node resolution mechanism.
  71. // https://github.com/facebookincubator/create-react-app/issues/253
  72. modules: ['node_modules', paths.appNodeModules].concat(
  73. // It is guaranteed to exist because we tweak it in `env.js`
  74. process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
  75. ),
  76. // These are the reasonable defaults supported by the Node ecosystem.
  77. // We also include JSX as a common component filename extension to support
  78. // some tools, although we do not recommend using it, see:
  79. // https://github.com/facebookincubator/create-react-app/issues/290
  80. // `web` extension prefixes have been added for better support
  81. // for React Native Web.
  82. extensions: ['.web.js', '.mjs', '.js', '.json', '.web.jsx', '.jsx'],
  83. alias: {
  84. // Support React Native Web
  85. // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
  86. 'react-native': 'react-native-web',
  87. },
  88. plugins: [
  89. // Prevents users from importing files from outside of src/ (or node_modules/).
  90. // This often causes confusion because we only process files within src/ with babel.
  91. // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
  92. // please link the files into your node_modules/ and let module-resolution kick in.
  93. // Make sure your source files are compiled, as they will not be processed in any way.
  94. new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
  95. ],
  96. },
  97. module: {
  98. strictExportPresence: true,
  99. rules: [
  100. // TODO: Disable require.ensure as it's not a standard language feature.
  101. // We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
  102. // { parser: { requireEnsure: false } },
  103. // First, run the linter.
  104. // It's important to do this before Babel processes the JS.
  105. {
  106. test: /\.(js|jsx|mjs)$/,
  107. enforce: 'pre',
  108. use: [
  109. {
  110. options: {
  111. formatter: eslintFormatter,
  112. eslintPath: require.resolve('eslint'),
  113. },
  114. loader: require.resolve('eslint-loader'),
  115. },
  116. ],
  117. include: paths.appSrc,
  118. },
  119. {
  120. // "oneOf" will traverse all following loaders until one will
  121. // match the requirements. When no loader matches it will fall
  122. // back to the "file" loader at the end of the loader list.
  123. oneOf: [
  124. // "url" loader works like "file" loader except that it embeds assets
  125. // smaller than specified limit in bytes as data URLs to avoid requests.
  126. // A missing `test` is equivalent to a match.
  127. {
  128. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  129. loader: require.resolve('url-loader'),
  130. options: {
  131. limit: 10000,
  132. name: 'static/media/[name].[hash:8].[ext]',
  133. },
  134. },
  135. // Process JS with Babel.
  136. {
  137. test: /\.(js|jsx|mjs)$/,
  138. include: paths.appSrc,
  139. loader: require.resolve('babel-loader'),
  140. options: {
  141. // This is a feature of `babel-loader` for webpack (not Babel itself).
  142. // It enables caching results in ./node_modules/.cache/babel-loader/
  143. // directory for faster rebuilds.
  144. cacheDirectory: true,
  145. },
  146. },
  147. // "postcss" loader applies autoprefixer to our CSS.
  148. // "css" loader resolves paths in CSS and adds assets as dependencies.
  149. // "style" loader turns CSS into JS modules that inject <style> tags.
  150. // In production, we use a plugin to extract that CSS to a file, but
  151. // in development "style" loader enables hot editing of CSS.
  152. {
  153. test: /\.css$/,
  154. use: [
  155. require.resolve('style-loader'),
  156. {
  157. loader: require.resolve('css-loader'),
  158. options: {
  159. importLoaders: 1,
  160. },
  161. },
  162. {
  163. loader: require.resolve('postcss-loader'),
  164. options: {
  165. // Necessary for external CSS imports to work
  166. // https://github.com/facebookincubator/create-react-app/issues/2677
  167. ident: 'postcss',
  168. plugins: () => [
  169. require('postcss-px-to-viewport')({
  170. unitToConvert: 'px', //需要转换的单位,默认为"px"
  171. viewportWidth: 750, // 视窗的宽度,对应的是我们设计稿的宽度
  172. viewportHeight: 667,//视窗的高度,根据375设备的宽度来指定,一般指定667,也可以不配置
  173. unitPrecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
  174. propList: ['*'], // 能转化为vw的属性列表
  175. viewportUnit: 'vw', // 指定需要转换成的视窗单位,建议使用vw
  176. fontViewportUnit: 'vw', //字体使用的视口单位
  177. selectorBlackList: ['.ignore-', '.hairlines'], //指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
  178. minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
  179. mediaQuery: false, // 允许在媒体查询中转换`px`
  180. replace: true, //是否直接更换属性值,而不添加备用属性
  181. exclude: [
  182. /RightBar/,
  183. /gotop.vue/,
  184. ], //忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
  185. landscape: false, //是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
  186. landscapeUnit: 'vw', //横屏时使用的单位
  187. }
  188. ),
  189. require('postcss-flexbugs-fixes'),
  190. autoprefixer({
  191. browsers: [
  192. '>1%',
  193. 'last 4 versions',
  194. 'Firefox ESR',
  195. 'not ie < 9', // React doesn't support IE8 anyway
  196. ],
  197. flexbox: 'no-2009',
  198. }),
  199. ],
  200. },
  201. },
  202. ],
  203. },
  204. // "file" loader makes sure those assets get served by WebpackDevServer.
  205. // When you `import` an asset, you get its (virtual) filename.
  206. // In production, they would get copied to the `build` folder.
  207. // This loader doesn't use a "test" so it will catch all modules
  208. // that fall through the other loaders.
  209. {
  210. // Exclude `js` files to keep "css" loader working as it injects
  211. // it's runtime that would otherwise processed through "file" loader.
  212. // Also exclude `html` and `json` extensions so they get processed
  213. // by webpacks internal loaders.
  214. exclude: [/\.js$/, /\.html$/, /\.json$/],
  215. loader: require.resolve('file-loader'),
  216. options: {
  217. name: 'static/media/[name].[hash:8].[ext]',
  218. },
  219. },
  220. ],
  221. },
  222. // ** STOP ** Are you adding a new loader?
  223. // Make sure to add the new loader(s) before the "file" loader.
  224. ],
  225. },
  226. plugins: [
  227. // Makes some environment variables available in index.html.
  228. // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
  229. // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  230. // In development, this will be an empty string.
  231. new InterpolateHtmlPlugin(env.raw),
  232. // Generates an `index.html` file with the <script> injected.
  233. new HtmlWebpackPlugin({
  234. inject: true,
  235. template: paths.appHtml,
  236. }),
  237. // Add module names to factory functions so they appear in browser profiler.
  238. new webpack.NamedModulesPlugin(),
  239. // Makes some environment variables available to the JS code, for example:
  240. // if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
  241. new webpack.DefinePlugin(env.stringified),
  242. // This is necessary to emit hot updates (currently CSS only):
  243. new webpack.HotModuleReplacementPlugin(),
  244. // Watcher doesn't work well if you mistype casing in a path so we use
  245. // a plugin that prints an error when you attempt to do this.
  246. // See https://github.com/facebookincubator/create-react-app/issues/240
  247. new CaseSensitivePathsPlugin(),
  248. // If you require a missing module and then `npm install` it, you still have
  249. // to restart the development server for Webpack to discover it. This plugin
  250. // makes the discovery automatic so you don't have to restart.
  251. // See https://github.com/facebookincubator/create-react-app/issues/186
  252. new WatchMissingNodeModulesPlugin(paths.appNodeModules),
  253. // Moment.js is an extremely popular library that bundles large locale files
  254. // by default due to how Webpack interprets its code. This is a practical
  255. // solution that requires the user to opt into importing specific locales.
  256. // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
  257. // You can remove this if you don't use Moment.js:
  258. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  259. ],
  260. // Some libraries import Node modules but don't use them in the browser.
  261. // Tell Webpack to provide empty mocks for them so importing them works.
  262. node: {
  263. dgram: 'empty',
  264. fs: 'empty',
  265. net: 'empty',
  266. tls: 'empty',
  267. child_process: 'empty',
  268. },
  269. // Turn off performance hints during development because we don't do any
  270. // splitting or minification in interest of speed. These warnings become
  271. // cumbersome.
  272. performance: {
  273. hints: false,
  274. },
  275. };