From 2d768a13e7b42ff62430a0a573706f5620b842c6 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 25 Sep 2021 14:28:59 +0200 Subject: [PATCH 1/6] support tsconfig-paths out of the box --- .changeset/honest-vans-float.md | 5 +++++ examples/demo/package.json | 2 +- examples/demo/public/foo/lib/test.ts | 1 + examples/demo/public/index.tsx | 3 +++ examples/demo/tsconfig.json | 9 ++++++++ packages/wmr/package.json | 1 + packages/wmr/src/lib/plugins.js | 2 ++ .../wmr/src/plugins/tsconfig-paths-plugin.js | 22 +++++++++++++++++++ yarn.lock | 10 +++++++++ 9 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 .changeset/honest-vans-float.md create mode 100644 examples/demo/public/foo/lib/test.ts create mode 100644 examples/demo/tsconfig.json create mode 100644 packages/wmr/src/plugins/tsconfig-paths-plugin.js diff --git a/.changeset/honest-vans-float.md b/.changeset/honest-vans-float.md new file mode 100644 index 000000000..541ddd2a1 --- /dev/null +++ b/.changeset/honest-vans-float.md @@ -0,0 +1,5 @@ +--- +'wmr': minor +--- + +Add a tsconfig-paths plugin that will check for a tsconfig and respect the paths option when present diff --git a/examples/demo/package.json b/examples/demo/package.json index 58eb6c1d7..e584ecc60 100644 --- a/examples/demo/package.json +++ b/examples/demo/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "scripts": { - "start": "cross-env DEBUG=true node ../../packages/wmr/src/cli.js", + "start": "cross-env DEBUG=false node ../../packages/wmr/src/cli.js", "build": "yarn start build --prerender", "serve": "yarn start serve", "start:prod": "cross-env DEBUG=true wmr", diff --git a/examples/demo/public/foo/lib/test.ts b/examples/demo/public/foo/lib/test.ts new file mode 100644 index 000000000..442a2bd46 --- /dev/null +++ b/examples/demo/public/foo/lib/test.ts @@ -0,0 +1 @@ +export const test = 'I am a tsconfig path'; diff --git a/examples/demo/public/index.tsx b/examples/demo/public/index.tsx index c75a72f31..084e0d4bc 100644 --- a/examples/demo/public/index.tsx +++ b/examples/demo/public/index.tsx @@ -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'; @@ -9,6 +10,8 @@ const sleep = t => new Promise(r => setTimeout(r, t)); const IS_CLIENT = typeof window !== 'undefined'; +console.log(test); + const About = lazy(() => import('./pages/about/index.js')); const LazyAndLate = lazy(async () => (IS_CLIENT && (await sleep(1500)), import('./pages/about/index.js'))); const CompatPage = lazy(() => import('./pages/compat.js')); diff --git a/examples/demo/tsconfig.json b/examples/demo/tsconfig.json new file mode 100644 index 000000000..e7d0f0326 --- /dev/null +++ b/examples/demo/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": "./public", + "paths": { + "lib/*": ["foo/lib/*"] + } + }, + "include": ["**/*.ts", "**/*.tsx"] +} diff --git a/packages/wmr/package.json b/packages/wmr/package.json index 0a8e57cd8..f761b6e10 100644 --- a/packages/wmr/package.json +++ b/packages/wmr/package.json @@ -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" }, diff --git a/packages/wmr/src/lib/plugins.js b/packages/wmr/src/lib/plugins.js index adf82b39e..89c43e110 100644 --- a/packages/wmr/src/lib/plugins.js +++ b/packages/wmr/src/lib/plugins.js @@ -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 @@ -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, diff --git a/packages/wmr/src/plugins/tsconfig-paths-plugin.js b/packages/wmr/src/plugins/tsconfig-paths-plugin.js new file mode 100644 index 000000000..9202d1f72 --- /dev/null +++ b/packages/wmr/src/plugins/tsconfig-paths-plugin.js @@ -0,0 +1,22 @@ +import { loadConfig, createMatchPathAsync } from 'tsconfig-paths'; + +export default function tsConfigPathsPlugin({ cwd }) { + const tsconfig = loadConfig(cwd); + + if (tsconfig.resultType === 'failed') { + return {}; + } + + const matchPath = createMatchPathAsync(cwd, tsconfig.paths, ['exports', 'module', 'main'], false); + + return { + resolveId(id) { + return new Promise((resolve, reject) => { + matchPath(id, undefined, undefined, undefined, (err, resolved) => { + if (err) reject(err); + else resolve(resolved); + }); + }); + } + }; +} diff --git a/yarn.lock b/yarn.lock index 25e489c37..d69450745 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" From 5e76ef04cea901a3d64e22789ae67b66d4fd8a36 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 25 Sep 2021 14:30:13 +0200 Subject: [PATCH 2/6] revert debug=false change --- examples/demo/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo/package.json b/examples/demo/package.json index e584ecc60..58eb6c1d7 100644 --- a/examples/demo/package.json +++ b/examples/demo/package.json @@ -4,7 +4,7 @@ "private": true, "type": "module", "scripts": { - "start": "cross-env DEBUG=false node ../../packages/wmr/src/cli.js", + "start": "cross-env DEBUG=true node ../../packages/wmr/src/cli.js", "build": "yarn start build --prerender", "serve": "yarn start serve", "start:prod": "cross-env DEBUG=true wmr", From b20f9d887225afb5369a9b09285de588be506900 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 25 Sep 2021 15:03:32 +0200 Subject: [PATCH 3/6] fix missing require.extensions --- packages/wmr/src/plugins/tsconfig-paths-plugin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/wmr/src/plugins/tsconfig-paths-plugin.js b/packages/wmr/src/plugins/tsconfig-paths-plugin.js index 9202d1f72..c48e010ab 100644 --- a/packages/wmr/src/plugins/tsconfig-paths-plugin.js +++ b/packages/wmr/src/plugins/tsconfig-paths-plugin.js @@ -3,7 +3,7 @@ import { loadConfig, createMatchPathAsync } from 'tsconfig-paths'; export default function tsConfigPathsPlugin({ cwd }) { const tsconfig = loadConfig(cwd); - if (tsconfig.resultType === 'failed') { + if (tsconfig.resultType === 'failed' || !tsconfig.paths) { return {}; } @@ -12,7 +12,7 @@ export default function tsConfigPathsPlugin({ cwd }) { return { resolveId(id) { return new Promise((resolve, reject) => { - matchPath(id, undefined, undefined, undefined, (err, resolved) => { + matchPath(id, undefined, undefined, ['js', 'ts', 'tsx', 'jsx'], (err, resolved) => { if (err) reject(err); else resolve(resolved); }); From 2518ebd1ad808da030679313f7d1df0880bd723d Mon Sep 17 00:00:00 2001 From: jdecroock Date: Tue, 28 Sep 2021 20:45:54 +0200 Subject: [PATCH 4/6] comment out test import --- examples/demo/public/index.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/demo/public/index.tsx b/examples/demo/public/index.tsx index 084e0d4bc..127d642fb 100644 --- a/examples/demo/public/index.tsx +++ b/examples/demo/public/index.tsx @@ -1,5 +1,5 @@ import { LocationProvider, Router, lazy, ErrorBoundary, hydrate } from 'preact-iso'; -import { test } from 'lib/test.ts'; +// 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'; @@ -10,8 +10,6 @@ const sleep = t => new Promise(r => setTimeout(r, t)); const IS_CLIENT = typeof window !== 'undefined'; -console.log(test); - const About = lazy(() => import('./pages/about/index.js')); const LazyAndLate = lazy(async () => (IS_CLIENT && (await sleep(1500)), import('./pages/about/index.js'))); const CompatPage = lazy(() => import('./pages/compat.js')); @@ -30,6 +28,7 @@ function hideLoading() { } export function App() { + return (
From 14064d82d9df3d637634624425cc2f375f9da2cd Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 29 Sep 2021 13:10:13 +0200 Subject: [PATCH 5/6] use node 12.x --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 03dc1b012..26406a26f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 From 9773ccac00e929b25fc647c3002f3544721d34d4 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Wed, 29 Sep 2021 13:17:19 +0200 Subject: [PATCH 6/6] update other matrices --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 26406a26f..5ea7ff647 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 @@ -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