module.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const path = require('path');
  3. const consola = require('consola');
  4. const defu = require('defu');
  5. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  6. const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
  7. var name = "@nuxtjs/eslint-module";
  8. var version = "3.1.0";
  9. function moduleExists(name) {
  10. try {
  11. return require.resolve(name);
  12. } catch (e) {
  13. return false;
  14. }
  15. }
  16. const logger = consola__default["default"].withTag("nuxt:eslint");
  17. const CONFIG_KEY = "eslint";
  18. const nuxtModule = function(moduleOptions) {
  19. if (!this.options.dev) {
  20. return;
  21. }
  22. const DEFAULTS = {
  23. context: this.options.srcDir,
  24. eslintPath: "eslint",
  25. extensions: ["js", "ts", "vue"],
  26. cache: true,
  27. lintDirtyModulesOnly: true
  28. };
  29. const options = defu.defu(this.options[CONFIG_KEY], moduleOptions, DEFAULTS);
  30. if (!moduleExists(options.eslintPath)) {
  31. logger.warn(`The dependency \`${options.eslintPath}\` not found.`, "Please run `yarn add eslint --dev` or `npm install eslint --save-dev`");
  32. return;
  33. }
  34. const filesToWatch = [
  35. ".eslintrc",
  36. ".eslintrc.json",
  37. ".eslintrc.yaml",
  38. ".eslintrc.yml",
  39. ".eslintrc.js"
  40. ];
  41. this.options.watch = this.options.watch || [];
  42. this.options.watch.push(...filesToWatch.map((file) => path.resolve(this.options.rootDir, file)));
  43. this.extendBuild((config, { isDev, isClient }) => {
  44. if (isDev && isClient) {
  45. const EslintPlugin = require("eslint-webpack-plugin");
  46. config.plugins.push(new EslintPlugin(options));
  47. }
  48. });
  49. };
  50. nuxtModule.meta = { name, version };
  51. module.exports = nuxtModule;