Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jsenv/importmap-eslint-resolver

Package Overview
Dependencies
Maintainers
2
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jsenv/importmap-eslint-resolver

importmap resolution for eslint.

  • 2.4.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42
decreased by-32.26%
Maintainers
2
Weekly downloads
 
Created
Source

importmap-eslint-resolver

Import maps resolution for ESLint.

github package npm package github ci codecov coverage

Table of contents

Presentation

Import maps are used to remap import to somewhere else. For instance the following importmap allows to remap "foo" to "./foo.js".

{
  "imports": {
    "foo": "./foo.js"
  }
}

By providing this importmap to the browser or Node.js, js imports resolution becomes aware of the importmap file remappings. You can write the following js file and it would search for file at "./foo.js".

import { value } from "foo"

console.log(value)

If you use import/no-unresolved rule from eslint-plugin-import these imports are reported as not resolved as shown in images below.

screenshot import not resolved in vscode
screenshot eslint error in vscode

This is why @jsenv/importmap-eslint-resolver exists: to make import/no-unresolved compatible with importmap file.

— see ESLint website
— see eslint-plugin-import on github
— see importmap spec on github

Installation

Follow the steps below to enable importmap resolution for ESLint.

Install eslint-plugin-import

If you already use this ESLint plugin you can skip this step.

npm install --save-dev eslint-plugin-import

Install importmap-eslint-resolver

npm install --save-dev @jsenv/importmap-eslint-resolver

Configure eslint

  • Your eslint config must enable eslint-plugin-import
  • Your eslint config must use @jsenv/importmap-eslint-resolver resolver

It means your minimal .eslintrc.cjs file looks like this:

module.exports = {
  plugins: ["import"],
  settings: {
    "import/resolver": {
      [require.resolve("@jsenv/importmap-eslint-resolver")]: {
        projectDirectoryUrl: __dirname,
      },
    },
  },
}

Set importmap file path

By default we will search for a file in your project directory named import-map.importmap. If the importmap file is located somewhere else you can use importMapFileRelativeUrl parameter to tell us where to look at.

module.exports = {
  plugins: ["import"],
  settings: {
    "import/resolver": {
      [require.resolve("@jsenv/importmap-eslint-resolver")]: {
        projectDirectoryUrl: __dirname,
        importMapFileRelativeUrl: "./project.importmap",
      },
    },
  },
}

Bare specifier

A specifier is what is written after the from keyword in an import statement.

import value from "specifier"

If there is no mapping of "specifier" to "./specifier.js" the imported file will not be found. This is because import map consider "specifier" as a special kind of specifier called bare specifier. And every bare specifier must have a mapping or it cannot be resolved. To fix this either add a mapping or put explicitely "./specifier.js".

Please note that "specifier.js" is also a bare specifier. You should write "./specifier.js" instead.

Extensionless import

Extensionless import means an import where the specifier omits the file extension.

import { value } from "./file"

But these type of specifier are problematic: you don't know where to look at for the corresponding file.

  • Is it ./file ?
  • Is it ./file.js ?
  • Is it ./file.ts ?

The best solution to avoid configuring your brain and your browser is to keep the extension on the specifier.

- import { value } from './file'
+ import { value } from './file.js'

But if for some reason this is problematic you can still configure @jsenv/importmap-eslint-resolver to understand these extensionless specifiers.

module.exports = {
  plugins: ["import"],
  settings: {
    "import/resolver": {
      [require.resolve("@jsenv/importmap-eslint-resolver")]: {
        projectDirectoryUrl: __dirname,
        defaultExtension: true,
      },
    },
  },
}

By passing defaultExtension: true you tell @jsenv/importmap-eslint-resolver to automatically add the importer extension when its omitted.

import { value } from "./file"

If written in index.js, searches file at file.js.
If written in index.ts, searches file at file.ts.

FAQs

Package last updated on 30 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc