webpack.common.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const tsImportPluginFactory = require('ts-import-plugin'); // 按需加载
  2. const path = require('path');
  3. const resolve = (dir) => {
  4. return path.resolve(process.cwd(), dir);
  5. };
  6. module.exports = {
  7. entry: {
  8. index: './src/index.tsx',
  9. framework: ['react', 'react-dom'],
  10. },
  11. output: {
  12. path: path.resolve(__dirname, '../dist'),
  13. sourceMapFilename: '[name].map',
  14. chunkFilename: 'static/js/[name].[chunkhash:8].js',
  15. filename: 'static/js/[name].[hash:8].js',
  16. },
  17. // stats: 'errors-warnings',
  18. resolve: {
  19. // 设置别名
  20. alias: {
  21. '@': resolve('src'), // 这样配置后 @ 可以指向 src 目录
  22. },
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.(jsx|tsx|js|ts)$/,
  28. loader: 'ts-loader',
  29. options: {
  30. transpileOnly: true,
  31. getCustomTransformers: () => ({
  32. before: [tsImportPluginFactory(/** options */)],
  33. }),
  34. compilerOptions: {
  35. module: 'es2015',
  36. },
  37. },
  38. exclude: /node_modules/,
  39. },
  40. /*{
  41. test: /\.(j|t)sx?$/,
  42. include: [resolve("src")],
  43. exclude: /node_modules/,
  44. loader: "babel-loader",
  45. options: {
  46. presets: [
  47. [
  48. "@babel/preset-env",
  49. {
  50. targets: { ie: 9 },
  51. ignoreBrowserslistConfig: true,
  52. useBuiltIns: false,
  53. modules: false,
  54. exclude: ["transform-typeof-symbol"]
  55. }
  56. ],
  57. [
  58. "@babel/preset-react",
  59. {
  60. targets: "last 2 versions, ie 11",
  61. modules: false
  62. }
  63. ],
  64. ["@babel/preset-typescript"]
  65. ],
  66. plugins: [
  67. ["@babel/plugin-syntax-dynamic-import"],
  68. ["@babel/plugin-proposal-decorators", { legacy: true }],
  69. ["@babel/plugin-proposal-class-properties", { loose: true }]
  70. ],
  71. sourceMap: true
  72. }
  73. },*/
  74. {
  75. test: /\.(png|jpe?g|gif|webp)(\?.*)?$/,
  76. use: [
  77. {
  78. loader: 'url-loader',
  79. options: {
  80. name: '[name].[ext]',
  81. outputPath: 'images/',
  82. limit: 4096,
  83. fallback: {
  84. loader: 'file-loader',
  85. options: {
  86. name: 'img/[name].[hash:8].[ext]',
  87. },
  88. },
  89. },
  90. },
  91. ],
  92. },
  93. {
  94. test: /\.(eot|ttf|svg|woff|woff2)$/,
  95. use: {
  96. loader: 'file-loader',
  97. options: {
  98. name: '[name]_[hash].[ext]',
  99. outputPath: 'font/',
  100. },
  101. },
  102. },
  103. ],
  104. },
  105. };