loader.dev.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* eslint-disable import/no-extraneous-dependencies */
  2. const path = require('path');
  3. const fs = require('fs');
  4. const autoprefixer = require('autoprefixer');
  5. const eslintFormatter = require('react-dev-utils/eslintFormatter');
  6. const paths = require('./paths');
  7. const lessToJs = require('less-vars-to-js');
  8. const themeVariables = lessToJs(fs.readFileSync(path.join(paths.appSrc, 'styles/ant-default.less'), 'utf8'));
  9. const iconPath = path.relative('./~/antd/lib/style/*', './src/styles/iconfont/antd-font/iconfont');
  10. //const iconPathNew = iconPath.replace(/\\/g, '//');
  11. //themeVariables['@icon-url'] = `'${iconPath}'`;
  12. const eslintLoader = {
  13. test: /\.(js|jsx)$/,
  14. enforce: 'pre',
  15. exclude: /node_modules/,
  16. include: paths.appSrc,
  17. use: [
  18. {
  19. loader: 'eslint-loader',
  20. options: {
  21. // Pass the formatter:
  22. formatter: eslintFormatter,
  23. },
  24. },
  25. ],
  26. };
  27. // js & jsx loader
  28. const jsLoader = {
  29. test: /\.(js|jsx)$/,
  30. include: paths.appSrc,
  31. exclude: /node_modules/,
  32. loader: require.resolve('babel-loader'),
  33. options: {
  34. cacheDirectory: true,
  35. babelrc: true,
  36. },
  37. };
  38. const postCssLoaderConfig = {
  39. loader: require.resolve('postcss-loader'),
  40. options: {
  41. ident: 'postcss',
  42. sourceMap: true,
  43. plugins: () => [
  44. require('postcss-flexbugs-fixes'),
  45. autoprefixer({
  46. browsers: [
  47. '>1%',
  48. 'last 4 versions',
  49. 'Firefox ESR',
  50. 'not ie < 9', // React doesn't support IE8 anyway
  51. ],
  52. flexbox: 'no-2009',
  53. }),
  54. ],
  55. },
  56. };
  57. const cssLoaderConfig = {
  58. loader: 'css-loader',
  59. options: {
  60. sourceMap: true,
  61. importLoaders: 1,
  62. },
  63. };
  64. const cssMoudleLoaderConfig = {
  65. loader: 'css-loader',
  66. options: {
  67. modules: false,
  68. sourceMap: true,
  69. importLoaders: 1,
  70. localIdentName: '[name]__[local]___[hash:base64:5]',
  71. },
  72. };
  73. const sassLoaderConfig = {
  74. loader: require.resolve('sass-loader'),
  75. options: {
  76. sourceMap: true,
  77. },
  78. };
  79. const lessLoaderConfig = {
  80. loader: 'less-loader',
  81. options: {
  82. modifyVars: themeVariables,
  83. javascriptEnabled: true,
  84. },
  85. };
  86. const cssLoaderRule = {
  87. test: /\.css$/,
  88. use: [{ loader: 'style-loader' }, cssLoaderConfig, postCssLoaderConfig, { loader: 'resolve-url-loader' }],
  89. };
  90. const scssLoaderRule = {
  91. test: /\.(scss|sass)$/,
  92. use: [{ loader: 'style-loader' }, cssLoaderConfig, { loader: 'resolve-url-loader' }, sassLoaderConfig],
  93. };
  94. const lessLoaderRule = {
  95. test: /\.(less)$/,
  96. use: [{ loader: 'style-loader' }, { loader: 'css-loader' }, postCssLoaderConfig, lessLoaderConfig],
  97. };
  98. // svg图标 loader
  99. const svgSpriteLoader = {
  100. test: /\.(pdf|svg)$/,
  101. loader: 'svg-sprite-loader',
  102. include: [path.resolve('src/styles/iconsvg')],
  103. options: {
  104. symbolId: 'icon-[name]',
  105. },
  106. };
  107. // font图标 loader
  108. const fontWoffLoader = {
  109. test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  110. // use: 'url-loader?limit=10000&mimetype=application/font-woff',
  111. use: [
  112. {
  113. loader: 'url-loader',
  114. options: {
  115. limit: 10000,
  116. mimetype: 'application/font-woff',
  117. name: 'assets/images/[name].[hash:7].[ext]',
  118. },
  119. },
  120. ],
  121. };
  122. // font图标 loader
  123. const fontTtfLoader = {
  124. test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  125. exclude: [path.resolve('src/styles/iconsvg')],
  126. use: [
  127. {
  128. loader: 'file-loader',
  129. options: {
  130. name: 'assets/images/[name].[hash:7].[ext]',
  131. },
  132. },
  133. ],
  134. };
  135. // image图片资源 loader
  136. const imageLoader = {
  137. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  138. exclude: [path.resolve('src/styles/iconsvg')],
  139. loader: 'file-loader',
  140. options: {
  141. name: 'assets/images/[name].[hash:7].[ext]',
  142. },
  143. // use: [
  144. // (loader: 'file-loader'),
  145. // {
  146. // // image-webpack-loader 插件安装老出问题。
  147. // loader: 'image-webpack-loader',
  148. // options: {
  149. // name: 'assets/images/[name].[hash:7].[ext]',
  150. // optipng: {
  151. // optimizationLevel: 7,
  152. // },
  153. // gifsicle: {
  154. // interlaced: false,
  155. // },
  156. // pngquant: {
  157. // quality: [0.65, 0.9],
  158. // speed: 4,
  159. // },
  160. // mozjpeg: {
  161. // quality: 65,
  162. // progressive: true,
  163. // },
  164. // },
  165. // },
  166. // ],
  167. };
  168. const mediaLoader = {
  169. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  170. loader: 'url-loader',
  171. options: {
  172. limit: 10000,
  173. name: 'assets/media/[name].[hash:7].[ext]',
  174. },
  175. };
  176. // 其他默认 loader
  177. const otherLoader = {
  178. exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/],
  179. loader: require.resolve('file-loader'),
  180. options: {
  181. name: 'assets/media/[name].[hash:7].[ext]',
  182. },
  183. };
  184. module.exports = {
  185. eslintLoader,
  186. jsLoader,
  187. postCssLoaderConfig,
  188. cssLoaderConfig,
  189. cssMoudleLoaderConfig,
  190. sassLoaderConfig,
  191. lessLoaderConfig,
  192. cssLoaderRule,
  193. scssLoaderRule,
  194. lessLoaderRule,
  195. svgSpriteLoader,
  196. fontWoffLoader,
  197. fontTtfLoader,
  198. imageLoader,
  199. mediaLoader,
  200. otherLoader,
  201. };