From d120169876a2b4e0ebb328f4b5966c404abcae8b Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 11 Sep 2023 00:34:40 -0700 Subject: [PATCH 1/3] added workspace test --- tests/tests-basic/package.json | 2 ++ tests/tests-basic/workspaces/a/index.js | 3 +++ tests/tests-basic/workspaces/a/package.json | 6 ++++++ tests/tests-basic/workspaces/b/index.js | 5 +++++ tests/tests-basic/workspaces/b/package.json | 11 +++++++++++ tests/tests-basic/workspaces/b/test.js | 9 +++++++++ 6 files changed, 36 insertions(+) create mode 100644 tests/tests-basic/workspaces/a/index.js create mode 100644 tests/tests-basic/workspaces/a/package.json create mode 100644 tests/tests-basic/workspaces/b/index.js create mode 100644 tests/tests-basic/workspaces/b/package.json create mode 100644 tests/tests-basic/workspaces/b/test.js diff --git a/tests/tests-basic/package.json b/tests/tests-basic/package.json index d16d7e3..e78fc31 100644 --- a/tests/tests-basic/package.json +++ b/tests/tests-basic/package.json @@ -20,7 +20,9 @@ "nodejsexample_12_exports": "file:nodejsexample_12_exports", "resolvewithplus": "file:.." }, + "workspaces": ["workspaces/*"], "scripts": { + "test-workspace": "npm --prefix workspaces/b test", "test": "node --experimental-import-meta-resolve --test tests-basic.test.js tests-export-patterns.test.js tests-import-patterns.test.js" } } diff --git a/tests/tests-basic/workspaces/a/index.js b/tests/tests-basic/workspaces/a/index.js new file mode 100644 index 0000000..c970c82 --- /dev/null +++ b/tests/tests-basic/workspaces/a/index.js @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-basic/workspaces/a/package.json b/tests/tests-basic/workspaces/a/package.json new file mode 100644 index 0000000..e9ec9a1 --- /dev/null +++ b/tests/tests-basic/workspaces/a/package.json @@ -0,0 +1,6 @@ +{ + "name": "a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-basic/workspaces/b/index.js b/tests/tests-basic/workspaces/b/index.js new file mode 100644 index 0000000..cd0f9ec --- /dev/null +++ b/tests/tests-basic/workspaces/b/index.js @@ -0,0 +1,5 @@ +import {foo} from 'a' + +export default function test () { + return foo() +} diff --git a/tests/tests-basic/workspaces/b/package.json b/tests/tests-basic/workspaces/b/package.json new file mode 100644 index 0000000..0b16570 --- /dev/null +++ b/tests/tests-basic/workspaces/b/package.json @@ -0,0 +1,11 @@ +{ + "name": "b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --experimental-import-meta-resolve --test" + }, + "dependencies": { + "a": "^0.0.0" + } +} diff --git a/tests/tests-basic/workspaces/b/test.js b/tests/tests-basic/workspaces/b/test.js new file mode 100644 index 0000000..325b30b --- /dev/null +++ b/tests/tests-basic/workspaces/b/test.js @@ -0,0 +1,9 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import resolvewithplus from '../../../../resolvewithplus.js' + +test('should return workspace paths', () => { + assert.strictEqual( + import.meta.resolve('a'), + resolvewithplus('a')) +}) From 18c670b517438d2dba1ef2fcb16dad7ca5fb1054 Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 12 Sep 2023 16:17:11 -0700 Subject: [PATCH 2/3] detect typescript from parent path --- resolvewithplus.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/resolvewithplus.js b/resolvewithplus.js index 2f60dcb..b7a55f3 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -15,6 +15,8 @@ const isRelPathRe = /^.\.?(?=\/|\\)/ const isWin32PathRe = /\\/g const isSupportedIndexRe = /index.[tj]sx?$/ const isResolveWithPathRe = /[\\/]resolvewithplus[\\/]/ +const isJsExtnRe = /\.js$/ +const isTsExtnRe = /\.ts$/ const packageNameRe = /(^@[^/]*\/[^/]*|^[^/]*)\/?(.*)$/ const isESMImportSubpathRe = /^#/ const esmStrGlobRe = /(\*)/g @@ -291,8 +293,14 @@ const getasdirsync = (d, opts) => { if ((relpath = gettargetindex(jsonobj, opts))) { filepath = getasfilesync(path.join(d, relpath)) } else if ((relpath = jsonobj.main)) { - filepath = getasfilesync(path.join(d, relpath)) - || getasfilesync(path.join(d, path.join(relpath, 'index'))) + if (opts.isTypescript && isJsExtnRe.test(relpath)) { + filepath = getasfilesync(path.join(d, relpath.replace(isJsExtnRe, '.ts'))) + || getasfilesync(path.join(d, relpath)) + || getasfilesync(path.join(d, path.join(relpath, 'index'))) + } else { + filepath = getasfilesync(path.join(d, relpath)) + || getasfilesync(path.join(d, path.join(relpath, 'index'))) + } } else { supportedExtensions.some(f => ( (f = path.join(d, `index${f}`)) && isfilesync(f) && (filepath = f))) @@ -420,7 +428,7 @@ const begin = (requirepath, withpath, opts) => { } else { fullpath = isDirPathRe.test(requirepath) ? getasfileordir(pathToPosix(requirepath), withpath, opts) - : getasnode_module(requirepath, withpath) + : getasnode_module(requirepath, withpath, opts) fullpath = fullpath && ( opts.isposixpath @@ -431,11 +439,20 @@ const begin = (requirepath, withpath, opts) => { return fullpath } +const createopts = (moduleId, parent, opts) => { + opts = opts || {} + opts.isTypescript = typeof opts.isTypescript === 'boolean' + ? opts.isTypescript : isTsExtnRe.test(parent) + + return opts +} + const resolvewith = (requirepath, withpath, opts) => { let resolvedpath = cache[requirepath+withpath] if (resolvedpath) return resolvedpath - resolvedpath = begin(requirepath, withpath, opts || {}) + opts = createopts(requirepath, withpath, opts) + resolvedpath = begin(requirepath, withpath, opts) return cache[requirepath+withpath] = resolvedpath } From 8c98b653945d5c26199d8a5f499ee10034895ede Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 12 Sep 2023 16:49:01 -0700 Subject: [PATCH 3/3] engage typescript conditions when typescript parent path is detected --- tests/tests-basic/package.json | 12 +++++++++--- .../{workspaces/a => workspaces-js/js-a}/index.js | 0 .../a => workspaces-js/js-a}/package.json | 2 +- .../{workspaces/b => workspaces-js/js-b}/index.js | 0 .../b => workspaces-js/js-b}/package.json | 4 ++-- .../{workspaces/b => workspaces-js/js-b}/test.js | 4 ++-- tests/tests-basic/workspaces-ts/ts-a/index.ts | 3 +++ tests/tests-basic/workspaces-ts/ts-a/package.json | 6 ++++++ tests/tests-basic/workspaces-ts/ts-b/index.ts | 5 +++++ tests/tests-basic/workspaces-ts/ts-b/package.json | 14 ++++++++++++++ tests/tests-basic/workspaces-ts/ts-b/test.ts | 12 ++++++++++++ tests/tests-basic/workspaces-ts/ts-b/tsconfig.json | 8 ++++++++ 12 files changed, 62 insertions(+), 8 deletions(-) rename tests/tests-basic/{workspaces/a => workspaces-js/js-a}/index.js (100%) rename tests/tests-basic/{workspaces/a => workspaces-js/js-a}/package.json (78%) rename tests/tests-basic/{workspaces/b => workspaces-js/js-b}/index.js (100%) rename tests/tests-basic/{workspaces/b => workspaces-js/js-b}/package.json (79%) rename tests/tests-basic/{workspaces/b => workspaces-js/js-b}/test.js (76%) create mode 100644 tests/tests-basic/workspaces-ts/ts-a/index.ts create mode 100644 tests/tests-basic/workspaces-ts/ts-a/package.json create mode 100644 tests/tests-basic/workspaces-ts/ts-b/index.ts create mode 100644 tests/tests-basic/workspaces-ts/ts-b/package.json create mode 100644 tests/tests-basic/workspaces-ts/ts-b/test.ts create mode 100644 tests/tests-basic/workspaces-ts/ts-b/tsconfig.json diff --git a/tests/tests-basic/package.json b/tests/tests-basic/package.json index e78fc31..31b5056 100644 --- a/tests/tests-basic/package.json +++ b/tests/tests-basic/package.json @@ -20,9 +20,15 @@ "nodejsexample_12_exports": "file:nodejsexample_12_exports", "resolvewithplus": "file:.." }, - "workspaces": ["workspaces/*"], + "workspaces": [ + "workspaces-js/*", + "workspaces-ts/*" + ], "scripts": { - "test-workspace": "npm --prefix workspaces/b test", - "test": "node --experimental-import-meta-resolve --test tests-basic.test.js tests-export-patterns.test.js tests-import-patterns.test.js" + "test-workspaces-js": "npm --prefix workspaces-js/js-b test", + "test-workspaces-ts": "npm --prefix workspaces-ts/ts-b test", + "test-workspaces": "npm run test-workspaces-js && npm run test-workspaces-ts", + "test-vanilla": "node --experimental-import-meta-resolve --test tests-basic.test.js tests-export-patterns.test.js tests-import-patterns.test.js", + "test": "npm run test-workspaces && npm run test-vanilla" } } diff --git a/tests/tests-basic/workspaces/a/index.js b/tests/tests-basic/workspaces-js/js-a/index.js similarity index 100% rename from tests/tests-basic/workspaces/a/index.js rename to tests/tests-basic/workspaces-js/js-a/index.js diff --git a/tests/tests-basic/workspaces/a/package.json b/tests/tests-basic/workspaces-js/js-a/package.json similarity index 78% rename from tests/tests-basic/workspaces/a/package.json rename to tests/tests-basic/workspaces-js/js-a/package.json index e9ec9a1..17e9f94 100644 --- a/tests/tests-basic/workspaces/a/package.json +++ b/tests/tests-basic/workspaces-js/js-a/package.json @@ -1,5 +1,5 @@ { - "name": "a", + "name": "js-a", "version": "0.0.0", "type": "module", "main": "index.js" diff --git a/tests/tests-basic/workspaces/b/index.js b/tests/tests-basic/workspaces-js/js-b/index.js similarity index 100% rename from tests/tests-basic/workspaces/b/index.js rename to tests/tests-basic/workspaces-js/js-b/index.js diff --git a/tests/tests-basic/workspaces/b/package.json b/tests/tests-basic/workspaces-js/js-b/package.json similarity index 79% rename from tests/tests-basic/workspaces/b/package.json rename to tests/tests-basic/workspaces-js/js-b/package.json index 0b16570..835a4db 100644 --- a/tests/tests-basic/workspaces/b/package.json +++ b/tests/tests-basic/workspaces-js/js-b/package.json @@ -1,11 +1,11 @@ { - "name": "b", + "name": "js-b", "version": "0.0.0", "type": "module", "scripts": { "test": "node --experimental-import-meta-resolve --test" }, "dependencies": { - "a": "^0.0.0" + "js-a": "^0.0.0" } } diff --git a/tests/tests-basic/workspaces/b/test.js b/tests/tests-basic/workspaces-js/js-b/test.js similarity index 76% rename from tests/tests-basic/workspaces/b/test.js rename to tests/tests-basic/workspaces-js/js-b/test.js index 325b30b..e30f2f9 100644 --- a/tests/tests-basic/workspaces/b/test.js +++ b/tests/tests-basic/workspaces-js/js-b/test.js @@ -4,6 +4,6 @@ import resolvewithplus from '../../../../resolvewithplus.js' test('should return workspace paths', () => { assert.strictEqual( - import.meta.resolve('a'), - resolvewithplus('a')) + import.meta.resolve('js-a'), + resolvewithplus('js-a')) }) diff --git a/tests/tests-basic/workspaces-ts/ts-a/index.ts b/tests/tests-basic/workspaces-ts/ts-a/index.ts new file mode 100644 index 0000000..c970c82 --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-a/index.ts @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-basic/workspaces-ts/ts-a/package.json b/tests/tests-basic/workspaces-ts/ts-a/package.json new file mode 100644 index 0000000..b197b2f --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-a/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts-a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-basic/workspaces-ts/ts-b/index.ts b/tests/tests-basic/workspaces-ts/ts-b/index.ts new file mode 100644 index 0000000..cd0f9ec --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-b/index.ts @@ -0,0 +1,5 @@ +import {foo} from 'a' + +export default function test () { + return foo() +} diff --git a/tests/tests-basic/workspaces-ts/ts-b/package.json b/tests/tests-basic/workspaces-ts/ts-b/package.json new file mode 100644 index 0000000..0ba0065 --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-b/package.json @@ -0,0 +1,14 @@ +{ + "name": "ts-b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --loader=ts-node/esm --test test.ts" + }, + "dependencies": { + "ts-a": "^0.0.0" + }, + "devDependencies": { + "ts-node": "^10.9.1" + } +} diff --git a/tests/tests-basic/workspaces-ts/ts-b/test.ts b/tests/tests-basic/workspaces-ts/ts-b/test.ts new file mode 100644 index 0000000..509b072 --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-b/test.ts @@ -0,0 +1,12 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import resolvewithplus from '../../../../resolvewithplus.js' + +console.log('test here!') +/* +test('should return workspace paths', () => { + assert.strictEqual( + import.meta.resolve('ts-a'), + resolvewithplus('ts-a')) +}) +*/ diff --git a/tests/tests-basic/workspaces-ts/ts-b/tsconfig.json b/tests/tests-basic/workspaces-ts/ts-b/tsconfig.json new file mode 100644 index 0000000..5b17f4e --- /dev/null +++ b/tests/tests-basic/workspaces-ts/ts-b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "esm": true, + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "module": "ESNext", + "moduleResolution": "node" + } +}