Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(wmr) - support tsconfig-paths out of the box #875

Merged
merged 6 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/honest-vans-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': minor
---

Add a tsconfig-paths plugin that will check for a tsconfig and respect the paths option when present
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.18, 14.x]
node-version: [12.x, 14.x]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.18, 14.x]
node-version: [12.x, 14.x]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down Expand Up @@ -96,7 +96,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.18, 14.x]
node-version: [12.x, 14.x]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions examples/demo/public/foo/lib/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'I am a tsconfig path';
2 changes: 2 additions & 0 deletions examples/demo/public/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LocationProvider, Router, lazy, ErrorBoundary, hydrate } from 'preact-iso';
// import { test } from 'lib/test.ts';
import Home from './pages/home.js';
// import About from './pages/about/index.js';
import NotFound from './pages/_404.js';
Expand Down Expand Up @@ -27,6 +28,7 @@ function hideLoading() {
}

export function App() {

return (
<LocationProvider>
<div class="app">
Expand Down
9 changes: 9 additions & 0 deletions examples/demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"baseUrl": "./public",
"paths": {
"lib/*": ["foo/lib/*"]
}
},
"include": ["**/*.ts", "**/*.tsx"]
}
1 change: 1 addition & 0 deletions packages/wmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"terser": "^5.7.0",
"tmp-promise": "^3.0.2",
"totalist": "^1.1.0",
"tsconfig-paths": "^3.11.0",
"utf-8-validate": "^5.0.2",
"ws": "^7.3.1"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/wmr/src/lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { prefreshPlugin } from '../plugins/preact/prefresh.js';
import { absolutePathPlugin } from '../plugins/absolute-path-plugin.js';
import { lessPlugin } from '../plugins/less-plugin.js';
import { workerPlugin } from '../plugins/worker-plugin.js';
import tsConfigPathsPlugin from '../plugins/tsconfig-paths-plugin.js';

/**
* @param {import("wmr").Options & { isIIFEWorker?: boolean}} options
Expand Down Expand Up @@ -66,6 +67,7 @@ export function getPlugins(options) {
jsonPlugin({ root }),
bundlePlugin({ inline: !production, cwd: root }),
absolutePathPlugin({ root }),
tsConfigPathsPlugin({ cwd: root }),
aliasPlugin({ alias }),
sucrasePlugin({
typescript: true,
Expand Down
22 changes: 22 additions & 0 deletions packages/wmr/src/plugins/tsconfig-paths-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { loadConfig, createMatchPathAsync } from 'tsconfig-paths';

export default function tsConfigPathsPlugin({ cwd }) {
const tsconfig = loadConfig(cwd);

if (tsconfig.resultType === 'failed' || !tsconfig.paths) {
return {};
}

const matchPath = createMatchPathAsync(cwd, tsconfig.paths, ['exports', 'module', 'main'], false);

return {
resolveId(id) {
return new Promise((resolve, reject) => {
matchPath(id, undefined, undefined, ['js', 'ts', 'tsx', 'jsx'], (err, resolved) => {
if (err) reject(err);
else resolve(resolved);
});
});
}
};
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8959,6 +8959,16 @@ ts-interface-checker@^0.1.9:
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==

tsconfig-paths@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.11.0.tgz#954c1fe973da6339c78e06b03ce2e48810b65f36"
integrity sha512-7ecdYDnIdmv639mmDwslG6KQg1Z9STTz1j7Gcz0xa+nshh/gKDAHcPxRbWOsA3SPp0tXP2leTcY9Kw+NAkfZzA==
dependencies:
"@types/json5" "^0.0.29"
json5 "^1.0.1"
minimist "^1.2.0"
strip-bom "^3.0.0"

tsconfig-paths@^3.9.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
Expand Down