All posts

Eslint + Vue + Vscode + Webpack Aliases

I have identified the following configuration that fix the issues with eslint error: 'Unable to resolve path to module' import/no-unresolved On .eslintrc.js And having a ./webpa…

A development pipeline resolving code checks and module paths

Archive note: this guide was recovered from my former WordPress notebook. The original date is preserved; commands and links may be updated as I verify them.

I have identified the following configuration that fix the issues with eslint error: 'Unable to resolve path to module' import/no-unresolved

npm i eslint-import-resolver-webpack -D

On .eslintrc.js

module.exports = {
  ...
  settings: {
    'import/resolver': 'webpack',
  },
  ...
}

And having a ./webpack.config.js file whit:

// This file is required so that eslint-import-resolver-webpack can resolve our webpack aliases
module.exports = {
  resolve: {
    alias: {
      '~': require('path').resolve(__dirname, 'src')
    }
  }
};

Or by having a explicit webpack config object on the eslint config file:

module.exports = {
  ...
  settings: {
    'import/resolver': {
      webpack: {
        config: {
          resolve: {
            extensions: ['.js', '.vue'],
            alias: {
              '~': require('path').resolve(__dirname, 'src'),
              '@': require('path').resolve(__dirname, 'src/components')
            }
          }
        }
      }
    }
  },
  ...
}

if you do not set the extencions config on the above file and you working with a framework that provide a webpack chain config you can add:

chainWebpack: (config) => {
  config.resolve.alias.set('@', path.resolve(__dirname, 'src/components'));
  config.resolve.extensions.add('.js').add('.vue');
}

You can make babel-eslint to work with webpack with:

npm i eslint-import-resolver-alias eslint-loader eslint -D

and a webpack config like:

module.exports = {
  ...
  module: {
    rules: [
      {
        // This will apply the loader before the other ones
        enforce: 'pre',
        test: /\.(js|vue)$/,
        include: [path.resolve(__dirname, 'src/')],        
        exclude: (file) => /node_modules/.test(file) && !/\.vue\.js/.test(file),
        use: [
          {
            loader: 'babel-loader',
            loader: 'eslint-loader'
          }
        ]
        options: {
          fix: true
        }
      }
    ]
  }
};

And .eslintrc.js like:

module.exports = {
  ...
  settings: {
    'import/resolver': {
      alias: {
        map: [
          ['@', 'src/component'],
          ['~', 'src']
        ],
        extensions: ['.js', '.vue']
      }
    }
  },
  ...
}

but this does not helps with the intelligence of VScode

Good Link https://gist.github.com/tomdavies/b0aa328a40828ad6210d1f0b07e15914

To have vscode path intelligence working you can have a jsconfig.json like:

{
  "compilerOptions": {
    "target": "es2017",
    "allowSyntheticDefaultImports": false,
    "baseUrl": "./",
    "paths": {
      "~/*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "dist"]
}

or a .vscode/settings.json like:

{
  "path-intellisense.mappings": {
    "~/*": "${workspaceRoot}/src/*"
  }
}