選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

54 行
1.0 KiB

  1. const pify = require('pify');
  2. const importCwd = require('import-cwd');
  3. const path = require('path');
  4. const getFileName = filepath => path.basename(filepath);
  5. function loadModule(moduleId) {
  6. // Trying to load module normally (relative to plugin directory)
  7. try {
  8. return require(moduleId);
  9. } catch (_) {
  10. // Ignore error
  11. }
  12. // Then, trying to load it relative to CWD
  13. return importCwd.silent(moduleId);
  14. }
  15. module.exports = {
  16. name: 'less',
  17. test: /\.less$/,
  18. async process({
  19. code
  20. }) {
  21. const less = loadModule('less');
  22. if (!less) {
  23. throw new Error('You need to install "less" packages in order to process Less files');
  24. }
  25. let {
  26. css,
  27. map,
  28. imports
  29. } = await pify(less.render.bind(less))(code, {
  30. ...this.options,
  31. sourceMap: this.sourceMap && { outputSourceFiles: true },
  32. filename: this.id
  33. });
  34. for (const dep of imports) {
  35. this.dependencies.add(dep);
  36. }
  37. if (map) {
  38. map = JSON.parse(map);
  39. map.sources = map.sources.map(source => getFileName(source));
  40. }
  41. return {
  42. code: css,
  43. map
  44. };
  45. }
  46. };