permanaj

webpack.mix.js

Nov 6th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  |--------------------------------------------------------------------------
  3.  | Mix Asset Management
  4.  |--------------------------------------------------------------------------
  5.  |
  6.  | Mix provides a clean, fluent API for defining some Webpack build steps
  7.  | for your application. See https://github.com/JeffreyWay/laravel-mix.
  8.  |
  9.  */
  10. let defaultConfig = {
  11.   'proxy': 'http://d8accelerator.localhost',
  12.   'source': 'src',
  13.   'dist': 'assets',
  14.   'inc': 'includes'
  15. };
  16. let config;
  17.  
  18. // Create config.local.json to override the setup, mainly proxy
  19. try {
  20.   config = require('./config.local.json');
  21. } catch (ex) {
  22.   config = defaultConfig;
  23. }
  24.  
  25. const proxy = config.proxy,
  26.   mix = require('laravel-mix'),
  27.   fs = require('fs');
  28.  
  29. /*
  30.  |--------------------------------------------------------------------------
  31.  | Configuration
  32.  |--------------------------------------------------------------------------
  33.  */
  34. mix
  35.   .setPublicPath('assets')
  36.   .disableNotifications()
  37.   .webpackConfig({
  38.     externals: {
  39.       jquery: 'jQuery'
  40.     }
  41.   })
  42.   .options({
  43.     processCssUrls: false
  44.   });
  45.  
  46. // mix.webpackConfig(webpack => {
  47. //   return {
  48. //     plugins: [
  49. //       new webpack.ProvidePlugin({
  50. //         $: "jquery",
  51. //         jQuery: "jquery",
  52. //         "window.jQuery": "jquery"
  53. //       })
  54. //     ]
  55. //   };
  56. // });
  57.  
  58. /*
  59.  |--------------------------------------------------------------------------
  60.  | Browsersync
  61.  |--------------------------------------------------------------------------
  62.  */
  63. mix.browserSync({
  64.   proxy: proxy,
  65.   files: [config.source + '/js/**/*.js', config.source + '/css/**/*.css', config.source + '/images/**/**.*'],
  66.   stream: true,
  67. });
  68. /*
  69.  |--------------------------------------------------------------------------
  70.  | SASS
  71.  |--------------------------------------------------------------------------
  72.  */
  73. // Minify all the css.
  74. mix
  75.   .sass(config.source + '/sass/priority.style.scss', 'css')
  76.   .minify(config.dist + '/css/priority.style.css');
  77. mix
  78.   .sass(config.source + '/sass/main.style.scss', 'css')
  79.   .minify(config.dist + '/css/main.style.css');
  80. mix
  81.   .sass(config.source + '/sass/ckeditor.style.scss', 'css')
  82.   .minify(config.dist + '/css/ckeditor.style.css');
  83. mix
  84.   .sass(config.source + '/sass/admin.style.scss', 'css')
  85.   .minify(config.dist + '/css/admin.style.css');
  86.  
  87. if (!mix.inProduction()) {
  88.   mix.webpackConfig({
  89.       devtool: 'source-map'
  90.     })
  91.     .sourceMaps();
  92. }
  93.  
  94. // Disable css url
  95. mix.options({
  96.   processCssUrls: false
  97. });
  98.  
  99. /*
  100.  |--------------------------------------------------------------------------
  101.  | JS
  102.  |--------------------------------------------------------------------------
  103.  */
  104. mix.js(config.source + '/js/main.script.js', 'js')
  105.   .extract(["popper.js", "bootstrap"]);
  106. // mix.scripts([
  107. //   config.dist + '/js/main.script.js',
  108. //   config.source + '/js/partials/*.js',
  109. //   config.source + '/components/**/*.js'
  110. // ], config.dist + '/js/main.script.js');
  111.  
  112. /*
  113.  |--------------------------------------------------------------------------
  114.  | Image
  115.  |--------------------------------------------------------------------------
  116.  */
  117. // Copy images whilst optimising them
  118. require('laravel-mix-imagemin');
  119. mix.imagemin(
  120.   'images/**/**.*', // pattern to catch
  121.   {
  122.     context: config.source, // within this directory. Publish path is inherited.
  123.     force: true // force override
  124.   }, {
  125.     optipng: {
  126.       optimizationLevel: 3 // 0 ~ 10
  127.     },
  128.     jpegtran: null,
  129.     plugins: [
  130.       require('imagemin-mozjpeg')({
  131.         quality: 70,
  132.         progressive: false,
  133.       }),
  134.     ],
  135.   }
  136. );
  137.  
  138. /*
  139.  |--------------------------------------------------------------------------
  140.  | Update Cache busting string
  141.  |--------------------------------------------------------------------------
  142.  */
  143. mix.extend('cachebust', function(i, cb) {
  144.   let d = new Date(),
  145.     dateString = d.getFullYear() + '.' + ((d.getMonth() < 10 ? '0' : '') + d.getMonth()) + '.' + ((d.getDate() < 10 ? '0' : '') + d.getDate() + '.' + d.getMilliseconds());
  146.   fs.writeFile(
  147.     config.inc + '/version.inc',
  148.     '<?php define("CSS_BUILD_VERSION", "' + dateString + '");',
  149.     function() {}
  150.   );
  151. });
  152.  
  153. if (mix.inProduction()) {
  154.   mix.cachebust();
  155. }
Advertisement
Add Comment
Please, Sign In to add comment