no.plugins.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * 使用参数 --no-plugins参数编译时,app.json中将去除插件使用的配置,其它json文件去除带plugin-private://的配置
  3. */
  4. 'use strict';
  5. module.exports = {
  6. apply(compiler) {
  7. compiler.hooks.emit.tap('compilation', compilation => {
  8. if (!(process.argv && process.argv.indexOf('--no-plugins') > -1)) {
  9. return;
  10. }
  11. // 遍历所有资源文件
  12. for (let filePathName in compilation.assets) {
  13. if (/^app\.json/i.test(filePathName)) {
  14. console.log('\n- app.json中将去除插件使用的配置...');
  15. // 获取文件内容
  16. let content = compilation.assets[filePathName].source() || '';
  17. let data = JSON.parse(content);
  18. if (typeof data.plugins !== 'undefined') {
  19. delete data.plugins;
  20. }
  21. if (typeof data.subPackages !== 'undefined' && data.subPackages.length) {
  22. for (let i in data.subPackages) {
  23. if (typeof data.subPackages[i].plugins !== 'undefined') {
  24. delete data.subPackages[i].plugins;
  25. }
  26. }
  27. }
  28. content = JSON.stringify(data, null, 2);
  29. // 重写指定输出模块内容
  30. compilation.assets[filePathName] = {
  31. source() {
  32. return content;
  33. },
  34. size() {
  35. return content.length;
  36. }
  37. };
  38. } else if (/.*\.json/i.test(filePathName)) {
  39. let content = compilation.assets[filePathName].source() || '';
  40. let data = JSON.parse(content);
  41. if (typeof data.usingComponents === 'object') {
  42. for (let i in data.usingComponents) {
  43. if (/^plugin-private:\/\//i.test(data.usingComponents[i])) {
  44. delete data.usingComponents[i];
  45. }
  46. }
  47. }
  48. content = JSON.stringify(data, null, 2);
  49. // 重写指定输出模块内容
  50. compilation.assets[filePathName] = {
  51. source() {
  52. return content;
  53. },
  54. size() {
  55. return content.length;
  56. }
  57. };
  58. }
  59. }
  60. });
  61. },
  62. };