All Projects → softwareventures → resolve-typescript-plugin

softwareventures / resolve-typescript-plugin

Licence: ISC license
webpack plugin to resolve TypeScript files when importing with js file extension in ESM projects

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to resolve-typescript-plugin

find-unused-exports
A Node.js CLI and equivalent JS API to find unused ECMAScript module exports in a project.
Stars: ✭ 30 (-23.08%)
Mutual labels:  esm
to-absolute-glob
Make a glob pattern absolute, ensuring that negative globs and patterns with trailing slashes are correctly handled.
Stars: ✭ 16 (-58.97%)
Mutual labels:  resolve
fyrlykt
Connect Loupedeck+ to DaVinci Resolve
Stars: ✭ 15 (-61.54%)
Mutual labels:  resolve
reboost
A super fast dev server for rapid web development
Stars: ✭ 59 (+51.28%)
Mutual labels:  esm
Esm
Tomorrow's ECMAScript modules today!
Stars: ✭ 5,093 (+12958.97%)
Mutual labels:  esm
open-display-transform
Open Display Transform is a collection of tools and experiments for rendering wide-gamut scene-linear data into an image for an SDR or HDR display device.
Stars: ✭ 120 (+207.69%)
Mutual labels:  resolve
greenwood
Greenwood is your workbench for the web, focused on supporting modern web standards and development to help you create your next project.
Stars: ✭ 48 (+23.08%)
Mutual labels:  esm
vitext
The Next.js like React framework for better User & Developer experience!
Stars: ✭ 376 (+864.1%)
Mutual labels:  esm
Aleph.js
The Full-stack Framework in Deno.
Stars: ✭ 3,448 (+8741.03%)
Mutual labels:  esm
go-multiaddr-dns
Go library and CLI tool for /dns4, /dns6, /dnsaddr multiaddr resolution
Stars: ✭ 24 (-38.46%)
Mutual labels:  resolve
absurdum
The Ridiculous Application of Reduce
Stars: ✭ 61 (+56.41%)
Mutual labels:  esm
Webpack
A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.
Stars: ✭ 60,034 (+153833.33%)
Mutual labels:  esm
requireg
Resolve and require local & global modules in node.js like a boss
Stars: ✭ 45 (+15.38%)
Mutual labels:  resolve
observable-to-standalone
Importing an Observable notebook into a standalone application
Stars: ✭ 31 (-20.51%)
Mutual labels:  esm
mesh
Решает тесты с МЭШ.
Stars: ✭ 19 (-51.28%)
Mutual labels:  resolve
async-folder-walker
A recursive async iterator of the files and directories in a given directory. Can take multiple directories and files, limit walk depth and filter based on path names and stat results.
Stars: ✭ 18 (-53.85%)
Mutual labels:  esm
cacheable-lookup
A cacheable dns.lookup(…) that respects TTL 🎉
Stars: ✭ 121 (+210.26%)
Mutual labels:  resolve
babel-plugin-rewire-exports
Babel plugin for stubbing [ES6, ES2015] module exports
Stars: ✭ 62 (+58.97%)
Mutual labels:  esm
MicroDNSSrv
A micro DNS server for MicroPython to simply respond to A queries on multi-domains with or without wildcards (used on Pycom modules & ESP32)
Stars: ✭ 43 (+10.26%)
Mutual labels:  resolve
sonar-issueresolver-plugin
Export and import resolved issues
Stars: ✭ 12 (-69.23%)
Mutual labels:  resolve

resolve-typescript-plugin

A webpack plugin to resolve TypeScript files imported using the .js extension when using ESM imports.

Obsolete

webpack has equivalent functionality built-in since v5.74.0. This plugin is no longer needed unless you are using an older version of webpack.

To migrate from this plugin, set resolve.extensionAlias in webpack.config.js:

export default {
    resolve: {
        extensionAlias: {
            ".js": [".ts", ".js"],
            ".mjs": [".mts", ".mjs"]
        }
    }
};

and remove new ResolveTypeScriptPlugin() from resolve.plugins.

Why?

If you are using webpack in conjunction with TypeScript and ES Modules, you need this plugin for full compliance with the ES Modules ecosystem.

ES Modules require imports to specify the runtime path of the file to be imported, including file extension. For TypeScript files, this means that you must import using the extension .js even though the source file uses the extension .ts or .tsx. This is because TypeScript compiles to a .js file that will be used at runtime.

However, webpack behaves differently, even when configured for ES Modules. webpack expects that files will be imported by specifying the compile-time path of the file, including the compile-time extension. For TypeScript files this will be .ts or .tsx. Alternatively, webpack expects that files will be imported with no extension, in which case webpack will resolve the extension automatically according to the resolve.extensions option. Neither of these behaviours is consistent with browser or node ES Module environments.

This plugin extends webpack module resolution so that imports specifying a .js extension will resolve to the corresponding .ts or .tsx file if available, and fall back to .js otherwise.

If you want to create ES Modules in TypeScript that are consistent between webpack, browser, and node environments, use this plugin.

See ts-loader#1110 for more background on this issue.

Install

With npm:

npm install --save-dev resolve-typescript-plugin

or yarn:

yarn add --dev resolve-typescript-plugin

Usage

Include the following in package.json to configure your project to be an ES Module:

{
    "type": "module"
}

Include something like the following in webpack.config.js:

import ResolveTypeScriptPlugin from "resolve-typescript-plugin";

export default {
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader"
            }
        ]
    },
    resolve: {
        plugins: [new ResolveTypeScriptPlugin()]
    }
};

You will also need to have ts-loader (or another TypeScript loader) installed and configured.

Previous versions of this README recommended setting resolve.fullySpecified to true. This is no longer recommended because it breaks compatibility with webpack-dev-server and possibly other webpack tooling.

If you use this plugin, you should probably remove .ts and .tsx from resolve.extensions.

Options

Pass options to the plugin as an argument to the constructor, as follows:

new ResolveTypeScriptPlugin({
    includeNodeModules: false
});

includeNodeModules

By default, the plugin does not resolve TypeScript files inside node_modules subdirectories. To enable this, set includeNodeModules: true.

Default: false.

Webpack 4 Compatibility

This plugin supports webpack versions 4.x and 5.x. However, there are some caveats when using webpack 4.x in conjunction with ES modules.

Webpack 4.x does not support webpack.config files in ES module format, so if you set "type": "module" in package.json then you must mark the webpack.config file as a CommonJS file by naming it webpack.config.cjs (with a .cjs extension). Of course, you must also use CommonJS format, for example:

const ResolveTypeScriptPlugin = require("resolve-typescript-plugin");

module.exports = {
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: "ts-loader"
            }
        ]
    },
    resolve: {
        plugins: [new ResolveTypeScriptPlugin()]
    }
};

Webpack 4.x also will not discover the webpack.config file automatically if it is named with a .cjs extension, so you must specify the path to the configuration file explicitly when running webpack, for example: webpack --config ./webpack.config.cjs.

Webpack 5.x has none of these caveats. In Webpack 5.x, configuration files may be in ES Module or CommonJS format, and will be discovered automatically if they are named with any of .js, .cjs, or .mjs file extensions.

Feedback

We're seeking community feedback on this plugin.

Please report bugs, problems, and missing features on the GitHub Issue Tracker.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].