webpack.common.config.js 2.6 KB

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