Skip to content

Commit 7e8436b

Browse files
committed
Merge branch 'alpha' into main
# Conflicts: # package.json # yarn.lock
2 parents e162381 + 3478109 commit 7e8436b

File tree

6 files changed

+572
-49
lines changed

6 files changed

+572
-49
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,52 @@ env:
44
CI: true
55
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
66
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
7-
DEPLOY_NODE_VERSION: 14.x
7+
DEPLOY_NODE_VERSION: 16.x
88
jobs:
99
build-and-test:
1010
name: Build and Test
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
node-version: [10.x, 12.x, 14.x]
14+
node-version: [12.x, 14.x, 16.x]
1515
steps:
1616
- name: Find yarn cache
1717
id: find-yarn-cache
1818
run: echo "::set-output name=dir::$(yarn cache dir)"
1919
- name: git checkout
2020
uses: actions/checkout@v2
2121
- name: Cache yarn dependencies
22-
uses: actions/cache@v1
22+
uses: actions/cache@v2
2323
with:
2424
path: ${{steps.find-yarn-cache.outputs.dir}}
2525
key: ${{runner.os}}-node${{matrix.node-version}}-yarn-${{hashFiles('**/yarn.lock')}}
2626
restore-keys: ${{runner.os}}-node${{matrix.node-version}}-yarn-
2727
- name: Set up Node.js v${{matrix.node-version}}
28-
uses: actions/setup-node@v1
28+
uses: actions/setup-node@v2
2929
with:
3030
node-version: ${{matrix.node-version}}
3131
- run: yarn
3232
- run: yarn test
3333
- run: yarn lint
34-
# deploy:
35-
# name: Deploy
36-
# runs-on: ubuntu-latest
37-
# needs: build-and-test
38-
# steps:
39-
# - name: Find yarn cache
40-
# id: find-yarn-cache
41-
# run: echo "::set-output name=dir::$(yarn cache dir)"
42-
# - name: git checkout
43-
# uses: actions/checkout@v2
44-
# - name: Cache yarn dependencies
45-
# uses: actions/cache@v1
46-
# with:
47-
# path: ${{steps.find-yarn-cache.outputs.dir}}
48-
# key: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-${{hashFiles('**/yarn.lock')}}
49-
# restore-keys: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-
50-
# - name: Set up Node.js
51-
# uses: actions/setup-node@v1
52-
# with:
53-
# node-version: ${{env.DEPLOY_NODE_VERSION}}
54-
# - run: yarn
55-
# - run: yarn semantic-release
34+
deploy:
35+
name: Deploy
36+
runs-on: ubuntu-latest
37+
needs: build-and-test
38+
steps:
39+
- name: Find yarn cache
40+
id: find-yarn-cache
41+
run: echo "::set-output name=dir::$(yarn cache dir)"
42+
- name: git checkout
43+
uses: actions/checkout@v2
44+
- name: Cache yarn dependencies
45+
uses: actions/cache@v2
46+
with:
47+
path: ${{steps.find-yarn-cache.outputs.dir}}
48+
key: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-${{hashFiles('**/yarn.lock')}}
49+
restore-keys: ${{runner.os}}-node${{env.DEPLOY_NODE_VERSION}}-yarn-
50+
- name: Set up Node.js
51+
uses: actions/setup-node@v2
52+
with:
53+
node-version: ${{env.DEPLOY_NODE_VERSION}}
54+
- run: yarn
55+
- run: yarn semantic-release

LICENSE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2021 Software Ventures Limited
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose
4+
with or without fee is hereby granted, provided that the above copyright notice
5+
and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13+
THIS SOFTWARE.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# resolve-typescript-plugin
2+
3+
A webpack plugin to resolve TypeScript files imported using the `.js` extension
4+
when using ESM imports.
5+
6+
## Why?
7+
8+
If you are using webpack in conjunction with TypeScript and ES Modules, you need
9+
this plugin for full compliance with the ES Modules ecosystem.
10+
11+
ES Modules require imports to specify the runtime path of the file to be
12+
imported, including file extension. For TypeScript files, this means that [you
13+
must import using the extension `.js`][1] even though the source file uses the
14+
extension `.ts` or `.tsx`. This is because TypeScript compiles to a `.js` file
15+
that will be used at runtime.
16+
17+
However, webpack behaves differently, even when configured for ES Modules.
18+
webpack expects that files will be imported by specifying the compile-time path
19+
of the file, including the compile-time extension. For TypeScript files this
20+
will be `.ts` or `.tsx`. Alternatively, webpack expects that files will be
21+
imported with no extension, in which case webpack will resolve the extension
22+
automatically according to the [`resolve.extensions` option][2]. Neither of
23+
these behaviours is consistent with browser or node ES Module environments.
24+
25+
This plugin extends webpack module resolution so that imports specifying a `.js`
26+
extension will resolve to the corresponding `.ts` or `.tsx` file if available,
27+
and fall back to `.js` otherwise.
28+
29+
If you want to create ES Modules in TypeScript that are consistent between
30+
webpack, browser, and node environments, use this plugin.
31+
32+
See [ts-loader#1110][3] for more background on this issue.
33+
34+
## Install
35+
36+
With npm:
37+
38+
```bash
39+
npm install --save-dev resolve-typescript-plugin
40+
```
41+
42+
or yarn:
43+
44+
```bash
45+
yarn add --dev resolve-typescript-plugin
46+
```
47+
48+
## Usage
49+
50+
Configure webpack something like this:
51+
52+
```js
53+
const ResolveTypeScriptPlugin = require("resolve-typescript-plugin").default;
54+
55+
exports = {
56+
module: {
57+
rules: [
58+
{
59+
test: /\.tsx?$/,
60+
use: "ts-loader"
61+
}
62+
]
63+
},
64+
resolve: {
65+
fullySpecfied: true,
66+
plugins: [new ResolveTypeScriptPlugin()]
67+
}
68+
};
69+
```
70+
71+
You will also need to have [ts-loader][4] (or another TypeScript loader)
72+
installed and configured.
73+
74+
## Feedback
75+
76+
We're seeking [community feedback][5] on this plugin.
77+
78+
Please report bugs, problems, and missing features on the [GitHub Issue
79+
Tracker][6].
80+
81+
[1]: https://github.com/microsoft/TypeScript/issues/16577#issuecomment-703190339
82+
[2]: https://github.com/TypeStrong/ts-loader/issues/1110
83+
[3]: https://webpack.js.org/configuration/resolve/#resolveextensions
84+
[4]: https://www.npmjs.com/package/ts-loader
85+
[5]: https://github.com/softwareventures/resolve-typescript-plugin/issues/5
86+
[6]: https://github.com/softwareventures/resolve-typescript-plugin/issues

index.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {ResolveOptions} from "webpack";
2+
3+
type Resolver = NonNullable<ResolveOptions["resolver"]>;
4+
5+
const pluginName = "ResolveTypescriptPlugin";
6+
7+
export interface ResolveTypescriptPluginOptions {
8+
includeNodeModules?: boolean;
9+
}
10+
11+
export default class ResolveTypescriptPlugin {
12+
private static defaultOptions: ResolveTypescriptPluginOptions = {
13+
includeNodeModules: false
14+
};
15+
16+
private options: ResolveTypescriptPluginOptions;
17+
18+
public constructor(options: ResolveTypescriptPluginOptions = {}) {
19+
this.options = {...ResolveTypescriptPlugin.defaultOptions, ...options};
20+
}
21+
22+
public apply(resolver: Resolver): void {
23+
const target = resolver.ensureHook("file");
24+
for (const extension of [".ts", ".tsx"]) {
25+
resolver
26+
.getHook("raw-file")
27+
.tapAsync(pluginName, (request, resolveContext, callback) => {
28+
if (
29+
!request.path ||
30+
(!this.options.includeNodeModules &&
31+
request.path.match(/(^|[\\/])node_modules($|[\\/])/))
32+
) {
33+
return callback();
34+
}
35+
36+
const path = request.path.replace(/\.js$/, extension);
37+
if (path === request.path) {
38+
callback();
39+
} else {
40+
resolver.doResolve(
41+
target,
42+
{
43+
...request,
44+
path,
45+
relativePath:
46+
request.relativePath &&
47+
request.relativePath.replace(/\.js$/, extension)
48+
},
49+
`using path: ${path}`,
50+
resolveContext,
51+
callback
52+
);
53+
}
54+
});
55+
}
56+
}
57+
}

package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
{
2-
"private": true,
32
"name": "resolve-typescript-plugin",
43
"version": "0.0.0-development",
54
"description": "webpack plugin to resolve TypeScript files when importing with js file extension in ESM projects",
65
"keywords": [
7-
"TypeScript",
8-
"webpack",
96
"ESM",
7+
"TypeScript",
8+
"plugin",
109
"resolve",
11-
"plugin"
10+
"webpack"
1211
],
1312
"author": "Daniel Cassidy <mail@danielcassidy.me.uk>",
1413
"homepage": "https://github.com/softwareventures/resolve-typescript-plugin",
1514
"bugs": "https://github.com/softwareventures/resolve-typescript-plugin/issues",
1615
"repository": "github:softwareventures/resolve-typescript-plugin",
1716
"license": "ISC",
1817
"scripts": {
19-
"fix": "eslint . --fix && prettier --write .",
20-
"lint": "eslint . && prettier --check .",
18+
"fix": "tsc --noEmit && eslint . --fix && prettier --write .",
19+
"lint": "tsc --noEmit && eslint . && prettier --check .",
2120
"prepare": "tsc",
2221
"semantic-release": "semantic-release",
2322
"test": "ava"
2423
},
24+
"engines": {
25+
"node": "^12 || ^14 || >=16"
26+
},
2527
"dependencies": {
2628
"tslib": "2.3.0"
2729
},
30+
"peerDependencies": {
31+
"webpack": "^5.0.0"
32+
},
2833
"devDependencies": {
2934
"@softwareventures/eslint-config": "3.6.3",
3035
"@softwareventures/prettier-config": "1.0.2",
@@ -36,7 +41,8 @@
3641
"prettier": "2.3.2",
3742
"semantic-release": "17.4.4",
3843
"ts-node": "9.1.1",
39-
"typescript": "4.3.5"
44+
"typescript": "4.3.5",
45+
"webpack": "5.43.0"
4046
},
4147
"eslintConfig": {
4248
"root": true,
@@ -58,5 +64,8 @@
5864
},
5965
"release": {
6066
"extends": "@softwareventures/semantic-release-config"
67+
},
68+
"publishConfig": {
69+
"access": "public"
6170
}
6271
}

0 commit comments

Comments
 (0)