vue.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. const CopyWebpackPlugin = require("copy-webpack-plugin");
  2. const AutoImport = require("unplugin-auto-import/webpack");
  3. const Components = require("unplugin-vue-components/webpack");
  4. const { ElementPlusResolver } = require("unplugin-vue-components/resolvers");
  5. const ExtReloader = require('webpack-ext-reloader');
  6. const ReplaceCssPlugin = require("./script/replaceCssPlugin.js");
  7. const path = require("path");
  8. const isProduction = process.env.NODE_ENV === "production";
  9. // 只需要复制的文件
  10. const copyFiles = [
  11. {
  12. from: path.resolve(`src/manifest.json`),
  13. to: `${path.resolve("dist")}/manifest.json`,
  14. },
  15. {
  16. from: path.resolve("src/assets"),
  17. to: path.resolve("dist/assets"),
  18. },
  19. ];
  20. const plugins = [
  21. new CopyWebpackPlugin({
  22. patterns: copyFiles,
  23. }),
  24. //仅支持webpack4.x 不支持5.x 需要自己手动改造5.x版本
  25. /* new ExtReloader({
  26. port: 9091, // Which port use to create the server
  27. reloadPage: true, // Force the reload of the page also
  28. entries: { // The entries used for the content/background scripts or extension pages
  29. contentScript: 'content',
  30. background: 'background',
  31. extensionPage: 'popup',
  32. },
  33. }), */
  34. new ReplaceCssPlugin(),
  35. AutoImport({
  36. include: [
  37. /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
  38. /\.vue$/,
  39. /\.vue\?vue/, // .vue
  40. ],
  41. imports: [
  42. // 插件预设好包
  43. "vue",
  44. // 自定义要自动按需引入的npm包
  45. {
  46. axios: [
  47. // default imports
  48. ["default", "axios"], // import { default as axios } from 'axios',
  49. ],
  50. },
  51. ],
  52. dts: false,
  53. eslintrc: {
  54. enabled: false, // 默认false, true启用。生成一次就可以,避免每次工程启动都生成
  55. filepath: "./.eslintrc-auto-import.json", // 生成json文件
  56. globalsPropValue: true,
  57. },
  58. resolvers: [ElementPlusResolver()],
  59. }),
  60. Components({
  61. resolvers: [ElementPlusResolver()],
  62. }),
  63. ];
  64. // 配置页面
  65. const pages = {};
  66. /**
  67. * popup 和 devtool 都需要html文件
  68. * 因此 chromeName 还可以添加devtool
  69. */
  70. const chromeName = ["popup"];
  71. chromeName.forEach((name) => {
  72. pages[name] = {
  73. entry: `src/${name}/index.js`,
  74. template: `src/${name}/index.html`,
  75. filename: `${name}.html`,
  76. };
  77. });
  78. module.exports = {
  79. pages,
  80. // 生产环境是否生成 sourceMap 文件
  81. productionSourceMap: false,
  82. /* pluginOptions: {
  83. 'style-resources-loader': {
  84. preProcessor: 'less',
  85. patterns: [path.resolve(__dirname, './src/assets/css/default.less')]
  86. }
  87. }, */
  88. configureWebpack: {
  89. // 多入口打包
  90. entry: {
  91. content: "./src/content/index.js",
  92. background: "./src/background/index.js",
  93. },
  94. resolve:{
  95. alias:{
  96. '@': path.resolve('src'),
  97. }
  98. },
  99. output: {
  100. filename: "js/[name].js",
  101. },
  102. plugins,
  103. optimization: {
  104. splitChunks: {
  105. chunks: "async",
  106. minSize: 20000,
  107. minRemainingSize: 0,
  108. minChunks: 1,
  109. maxAsyncRequests: 30,
  110. maxInitialRequests: 30,
  111. enforceSizeThreshold: 50000,
  112. cacheGroups: {
  113. defaultVendors: {
  114. test: /[\\/]node_modules[\\/]/,
  115. priority: -10,
  116. reuseExistingChunk: true,
  117. },
  118. default: {
  119. minChunks: 2,
  120. priority: -20,
  121. reuseExistingChunk: true,
  122. },
  123. },
  124. },
  125. },
  126. },
  127. //配置less解析和浏览器自动补全前缀
  128. chainWebpack: (config) => {
  129. config.module.rule('less')
  130. .test(/\.less$/)
  131. .use('style-loader')
  132. .loader('style-loader')
  133. .end()
  134. .use('css-loader')
  135. .loader('css-loader')
  136. .end()
  137. .use('postcss-loader')
  138. .loader('postcss-loader')
  139. .end()
  140. .use('less-loader')
  141. .loader('less-loader')
  142. .end()
  143. },
  144. css: {
  145. extract: {
  146. filename: "css/[name].css",
  147. },
  148. },
  149. /* devServer:{
  150. host: '0.0.0.0', // 让你的服务器可以被外部访问
  151. port: 8080, // 端口
  152. https: false, // 不使用 HTTP 提供服务
  153. hot: true,
  154. onBeforeSetupMiddleware: function(app, server,compiler) {
  155. // 在服务器启动之前执行的函数
  156. console.log(compiler,'compilercompilercompiler')
  157. },
  158. } */
  159. };