webpack.mix.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const mix = require('laravel-mix');
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Mix Asset Management
  5. |--------------------------------------------------------------------------
  6. |
  7. | Mix provides a clean, fluent API for defining some Webpack build steps
  8. | for your Laravel application. By default, we are compiling the Sass
  9. | file for the application as well as bundling up all the JS files.
  10. |
  11. */
  12. //复制资源
  13. mix.copy('resources/assets/statics/public', 'public');
  14. //生成vue页面js
  15. mix.js('resources/assets/js/main/app.js', 'js/app.js');
  16. //生成css样式文件
  17. mix.sass('resources/assets/sass/app.scss', 'css');
  18. //配置webpack
  19. mix.webpackConfig({
  20. output: {
  21. // 路由懒加载文件路径
  22. chunkFilename: 'js/build/[name].js?[hash:8]',
  23. },
  24. devServer: {
  25. disableHostCheck: true,
  26. },
  27. module: {
  28. rules: [{
  29. // 图片资源保存路径规则
  30. test: /(\.(png|jpe?g|gif)$|^((?!font).)*\.svg$)/,
  31. loaders: [
  32. {
  33. loader: 'file-loader',
  34. options: {
  35. name: path => {
  36. path = path.replace(/\\/g, '/');
  37. // 自定义部分
  38. try {
  39. if (/\/resources\/assets\//.test(path)) {
  40. let file = path.substring(path.indexOf('/resources/assets/') + '/resources/assets/'.length);
  41. if (file) {
  42. if (file.substring(0, 3) === 'js/') file = 'pages/' + file.substring(3);
  43. if (file.substring(0, 7) === 'images/') file = file.substring(7);
  44. return 'images/' + file + '?[hash:8]'
  45. }
  46. }
  47. } catch (e) { }
  48. // 系统定义部分
  49. if (!/node_modules|bower_components/.test(path)) {
  50. return (
  51. Config.fileLoaderDirs.images + '/[name].[ext]?[hash:8]'
  52. );
  53. }
  54. return (
  55. Config.fileLoaderDirs.images + '/vendor/' + path.replace(
  56. /((.*(node_modules|bower_components))|images|image|img|assets)\//g,
  57. ''
  58. ) + '?[hash:8]'
  59. );
  60. },
  61. publicPath: Config.resourceRoot
  62. }
  63. }, {
  64. loader: 'img-loader',
  65. options: Config.imgLoaderOptions
  66. }
  67. ]
  68. }, {
  69. // 字体资源保存路径规则
  70. test: /(\.(woff2?|ttf|eot|otf)$|font.*\.svg$)/,
  71. loader: 'file-loader',
  72. options: {
  73. name: path => {
  74. path = path.replace(/\\/g, '/');
  75. // 自定义部分
  76. try {
  77. if (/\/resources\/assets\//.test(path)) {
  78. let file = path.substring(path.indexOf('/resources/assets/') + '/resources/assets/'.length);
  79. if (file) {
  80. if (file.substring(0, 3) === 'js/') file = 'pages/' + file.substring(3);
  81. if (file.substring(0, 7) === 'fonts/') file = file.substring(7);
  82. return 'fonts/' + file + '?[hash:8]'
  83. }
  84. }
  85. } catch (e) { }
  86. // 系统定义部分
  87. if (!/node_modules|bower_components/.test(path)) {
  88. return Config.fileLoaderDirs.fonts + '/[name].[ext]?[hash:8]';
  89. }
  90. return (
  91. Config.fileLoaderDirs.fonts + '/vendor/' + path.replace(
  92. /((.*(node_modules|bower_components))|fonts|font|assets)\//g,
  93. ''
  94. ) + '?[hash:8]'
  95. );
  96. },
  97. publicPath: Config.resourceRoot
  98. }
  99. }]
  100. }
  101. }).options({
  102. uglify: {
  103. uglifyOptions: {
  104. compress: { inline: false }
  105. }
  106. },
  107. hmrOptions: {
  108. port: '8088'
  109. }
  110. });