inject.js 892 B

1234567891011121314151617181920212223242526272829303132333435
  1. const glob = require('glob');
  2. const fs = require('fs');
  3. const filenames = [];
  4. var entryFiles = glob.sync('./**/*.vue');
  5. entryFiles.forEach(filePath => {
  6. filenames.push(filePath);
  7. });
  8. const getImportItemStr = path => {
  9. const filename = path.substring(
  10. path.lastIndexOf('/') + 1,
  11. path.lastIndexOf('.')
  12. );
  13. return `import ${filename} from '${path}';`;
  14. };
  15. const getRegistItemStr = path => {
  16. const filename = path.substring(
  17. path.lastIndexOf('/') + 1,
  18. path.lastIndexOf('.')
  19. );
  20. return `Vue.component('${filename}', ${filename});`;
  21. };
  22. const content = [
  23. `import Vue from 'vue';`,
  24. '\r\n',
  25. filenames.map(x => getImportItemStr(x)).join('\r\n'),
  26. '\r\n\r\n',
  27. filenames.map(x => getRegistItemStr(x)).join('\r\n'),
  28. '\r\n'
  29. ];
  30. fs.writeFile('./index.js', content.join(''), function(err) {
  31. if (err) console.log(err);
  32. else console.log('写文件操作成功');
  33. });