Skip to content

Latest commit

 

History

History
177 lines (137 loc) · 2.24 KB

source.md

File metadata and controls

177 lines (137 loc) · 2.24 KB

source

Options for resolving other source modules in the source files (import foo from './bar').

Must be set to an object to be enabled. That object may contain the following options.

source.extensions

A list of extensions to resolve, and optionally the extension that should be used once transpiled.

Takes the form of one of the following.

  1. Resolve .js or .json in that order.
[".js", ".json"]
  1. Resolve .ts or .json in that order, uses .js for imports of .ts modules in transpiled code.
[[".ts", ".js"], ".json"]
  1. Resolve .ts, .tsx, or .json in that order, uses .js for imports of .ts and .tsx modules in transpiled code.
[[[".ts", ".tsx"], ".js"], ".json"]

source.ignoreUnresolved

Set to true to ignore any source modules that cannot be resolved.

Examples

Example .js -> .js

.babelrc

{
	"presets": [
		[
			"@babel/preset-env",
			{
				"modules": false,
				"targets": {
					"node": "current"
				}
			}
		]
	],
	"plugins": [
		[
			"esm-resolver",
			{
				"source": {
					"extensions": [".js"]
				}
			}
		]
	]
}

src/main.js

import {foo} from './bar';

src/bar.js

export const foo = 123;

output:

import {foo} from './bar.js';

Example .ts -> .js

(In practice the TypeScript preset would probably also be used)

.babelrc

{
	"presets": [
		[
			"@babel/preset-env",
			{
				"modules": false,
				"targets": {
					"node": "current"
				}
			}
		]
	],
	"plugins": [
		[
			"esm-resolver",
			{
				"source": {
					"extensions": [[".ts", ".js"]]
				}
			}
		]
	]
}

output:

import {foo} from './bar.js';

Example .ts + .js -> .js

.babelrc

{
	"presets": [
		[
			"@babel/preset-env",
			{
				"modules": false,
				"targets": {
					"node": "current"
				}
			}
		]
	],
	"plugins": [
		[
			"esm-resolver",
			{
				"source": {
					"extensions": [[[".ts", ".js"], ".js"]]
				}
			}
		]
	]
}

src/main.ts

import {foo} from './bar';
import {bar} from './foo';

src/bar.ts

export const foo = 123;

src/foo.js

export const bar = 123;

output:

import {foo} from './bar.js';
import {bar} from './foo.js';