Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CustomTasksPlugin.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. let Log = require('../Log');
  2. let collect = require('collect.js');
  3. class CustomTasksPlugin {
  4. /**
  5. * Apply the plugin.
  6. *
  7. * @param {Object} compiler
  8. */
  9. apply(compiler) {
  10. compiler.plugin('done', stats => {
  11. Mix.tasks.forEach(task => this.runTask(task, stats));
  12. if (Mix.components.get('version')) {
  13. this.applyVersioning();
  14. }
  15. if (Mix.inProduction()) {
  16. this.minifyAssets();
  17. }
  18. if (Mix.isWatching()) {
  19. Mix.tasks.forEach(task => task.watch(Mix.isPolling()));
  20. }
  21. Mix.manifest.refresh();
  22. });
  23. }
  24. /**
  25. * Execute the task.
  26. *
  27. * @param {Task} task
  28. */
  29. runTask(task, stats) {
  30. task.run();
  31. task.assets.forEach(asset => {
  32. Mix.manifest.add(asset.pathFromPublic());
  33. // Update the Webpack assets list for better terminal output.
  34. stats.compilation.assets[asset.pathFromPublic()] = {
  35. size: () => asset.size(),
  36. emitted: true
  37. };
  38. });
  39. }
  40. /**
  41. * Minify the given asset file.
  42. */
  43. minifyAssets() {
  44. collect(Mix.tasks)
  45. .where('constructor.name', '!==', 'VersionFilesTask')
  46. .where('constructor.name', '!==', 'CopyFilesTask')
  47. .each(({ assets }) =>
  48. assets.forEach(asset => {
  49. try {
  50. asset.minify();
  51. } catch (e) {
  52. Log.error(
  53. `Whoops! We had trouble minifying "${asset.relativePath()}". ` +
  54. `Perhaps you need to use mix.babel() instead?`
  55. );
  56. throw e;
  57. }
  58. })
  59. );
  60. }
  61. /**
  62. * Version all files that are present in the manifest.
  63. */
  64. applyVersioning() {
  65. collect(Mix.manifest.get()).each((value, key) =>
  66. Mix.manifest.hash(key)
  67. );
  68. }
  69. }
  70. module.exports = CustomTasksPlugin;