Skip to content

Commit

Permalink
Merge branch 'alpha' into main
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	yarn.lock
  • Loading branch information
djcsdy committed Jul 7, 2021
2 parents e162381 + 3478109 commit 7e8436b
Show file tree
Hide file tree
Showing 6 changed files with 572 additions and 49 deletions.
52 changes: 26 additions & 26 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ env:
CI: true
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
DEPLOY_NODE_VERSION: 14.x
DEPLOY_NODE_VERSION: 16.x
jobs:
build-and-test:
name: Build and Test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [12.x, 14.x, 16.x]
steps:
- name: Find yarn cache
id: find-yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: git checkout
uses: actions/checkout@v2
- name: Cache yarn dependencies
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{steps.find-yarn-cache.outputs.dir}}
key: ${{runner.os}}-node${{matrix.node-version}}-yarn-${{hashFiles('**/yarn.lock')}}
restore-keys: ${{runner.os}}-node${{matrix.node-version}}-yarn-
- name: Set up Node.js v${{matrix.node-version}}
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: ${{matrix.node-version}}
- run: yarn
- run: yarn test
- run: yarn lint
# deploy:
# name: Deploy
# runs-on: ubuntu-latest
# needs: build-and-test
# steps:
# - name: Find yarn cache
# id: find-yarn-cache
# run: echo "::set-output name=dir::$(yarn cache dir)"
# - name: git checkout
# uses: actions/checkout@v2
# - name: Cache yarn dependencies
# uses: actions/cache@v1
# with:
# path: ${{steps.find-yarn-cache.outputs.dir}}
# key: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-${{hashFiles('**/yarn.lock')}}
# restore-keys: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-
# - name: Set up Node.js
# uses: actions/setup-node@v1
# with:
# node-version: ${{env.DEPLOY_NODE_VERSION}}
# - run: yarn
# - run: yarn semantic-release
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: build-and-test
steps:
- name: Find yarn cache
id: find-yarn-cache
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: git checkout
uses: actions/checkout@v2
- name: Cache yarn dependencies
uses: actions/cache@v2
with:
path: ${{steps.find-yarn-cache.outputs.dir}}
key: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-${{hashFiles('**/yarn.lock')}}
restore-keys: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: ${{env.DEPLOY_NODE_VERSION}}
- run: yarn
- run: yarn semantic-release
13 changes: 13 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2021 Software Ventures Limited

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# resolve-typescript-plugin

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

## 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`][1] 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][2]. 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][3] for more background on this issue.

## Install

With npm:

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

or yarn:

```bash
yarn add --dev resolve-typescript-plugin
```

## Usage

Configure webpack something like this:

```js
const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;

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

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

## Feedback

We're seeking [community feedback][5] on this plugin.

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

[1]: https://github.com/microsoft/TypeScript/issues/16577#issuecomment-703190339
[2]: https://github.com/TypeStrong/ts-loader/issues/1110
[3]: https://webpack.js.org/configuration/resolve/#resolveextensions
[4]: https://www.npmjs.com/package/ts-loader
[5]: https://github.com/softwareventures/resolve-typescript-plugin/issues/5
[6]: https://github.com/softwareventures/resolve-typescript-plugin/issues
57 changes: 57 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {ResolveOptions} from "webpack";

type Resolver = NonNullable<ResolveOptions["resolver"]>;

const pluginName = "ResolveTypescriptPlugin";

export interface ResolveTypescriptPluginOptions {
includeNodeModules?: boolean;
}

export default class ResolveTypescriptPlugin {
private static defaultOptions: ResolveTypescriptPluginOptions = {
includeNodeModules: false
};

private options: ResolveTypescriptPluginOptions;

public constructor(options: ResolveTypescriptPluginOptions = {}) {
this.options = {...ResolveTypescriptPlugin.defaultOptions, ...options};
}

public apply(resolver: Resolver): void {
const target = resolver.ensureHook("file");
for (const extension of [".ts", ".tsx"]) {
resolver
.getHook("raw-file")
.tapAsync(pluginName, (request, resolveContext, callback) => {
if (
!request.path ||
(!this.options.includeNodeModules &&
request.path.match(/(^|[\\/])node_modules($|[\\/])/))
) {
return callback();
}

const path = request.path.replace(/\.js$/, extension);
if (path === request.path) {
callback();
} else {
resolver.doResolve(
target,
{
...request,
path,
relativePath:
request.relativePath &&
request.relativePath.replace(/\.js$/, extension)
},
`using path: ${path}`,
resolveContext,
callback
);
}
});
}
}
}
23 changes: 16 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
{
"private": true,
"name": "resolve-typescript-plugin",
"version": "0.0.0-development",
"description": "webpack plugin to resolve TypeScript files when importing with js file extension in ESM projects",
"keywords": [
"TypeScript",
"webpack",
"ESM",
"TypeScript",
"plugin",
"resolve",
"plugin"
"webpack"
],
"author": "Daniel Cassidy <mail@danielcassidy.me.uk>",
"homepage": "https://github.com/softwareventures/resolve-typescript-plugin",
"bugs": "https://github.com/softwareventures/resolve-typescript-plugin/issues",
"repository": "github:softwareventures/resolve-typescript-plugin",
"license": "ISC",
"scripts": {
"fix": "eslint . --fix && prettier --write .",
"lint": "eslint . && prettier --check .",
"fix": "tsc --noEmit && eslint . --fix && prettier --write .",
"lint": "tsc --noEmit && eslint . && prettier --check .",
"prepare": "tsc",
"semantic-release": "semantic-release",
"test": "ava"
},
"engines": {
"node": "^12 || ^14 || >=16"
},
"dependencies": {
"tslib": "2.3.0"
},
"peerDependencies": {
"webpack": "^5.0.0"
},
"devDependencies": {
"@softwareventures/eslint-config": "3.6.3",
"@softwareventures/prettier-config": "1.0.2",
Expand All @@ -36,7 +41,8 @@
"prettier": "2.3.2",
"semantic-release": "17.4.4",
"ts-node": "9.1.1",
"typescript": "4.3.5"
"typescript": "4.3.5",
"webpack": "5.43.0"
},
"eslintConfig": {
"root": true,
Expand All @@ -58,5 +64,8 @@
},
"release": {
"extends": "@softwareventures/semantic-release-config"
},
"publishConfig": {
"access": "public"
}
}
Loading

0 comments on commit 7e8436b

Please sign in to comment.