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 } diff --git a/tests/tests-basic/package.json b/tests/tests-basic/package.json index d16d7e3..31b5056 100644 --- a/tests/tests-basic/package.json +++ b/tests/tests-basic/package.json @@ -20,7 +20,15 @@ "nodejsexample_12_exports": "file:nodejsexample_12_exports", "resolvewithplus": "file:.." }, + "workspaces": [ + "workspaces-js/*", + "workspaces-ts/*" + ], "scripts": { - "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-js/js-a/index.js b/tests/tests-basic/workspaces-js/js-a/index.js new file mode 100644 index 0000000..c970c82 --- /dev/null +++ b/tests/tests-basic/workspaces-js/js-a/index.js @@ -0,0 +1,3 @@ +export function foo () { + return 'foo' +} diff --git a/tests/tests-basic/workspaces-js/js-a/package.json b/tests/tests-basic/workspaces-js/js-a/package.json new file mode 100644 index 0000000..17e9f94 --- /dev/null +++ b/tests/tests-basic/workspaces-js/js-a/package.json @@ -0,0 +1,6 @@ +{ + "name": "js-a", + "version": "0.0.0", + "type": "module", + "main": "index.js" +} diff --git a/tests/tests-basic/workspaces-js/js-b/index.js b/tests/tests-basic/workspaces-js/js-b/index.js new file mode 100644 index 0000000..cd0f9ec --- /dev/null +++ b/tests/tests-basic/workspaces-js/js-b/index.js @@ -0,0 +1,5 @@ +import {foo} from 'a' + +export default function test () { + return foo() +} diff --git a/tests/tests-basic/workspaces-js/js-b/package.json b/tests/tests-basic/workspaces-js/js-b/package.json new file mode 100644 index 0000000..835a4db --- /dev/null +++ b/tests/tests-basic/workspaces-js/js-b/package.json @@ -0,0 +1,11 @@ +{ + "name": "js-b", + "version": "0.0.0", + "type": "module", + "scripts": { + "test": "node --experimental-import-meta-resolve --test" + }, + "dependencies": { + "js-a": "^0.0.0" + } +} diff --git a/tests/tests-basic/workspaces-js/js-b/test.js b/tests/tests-basic/workspaces-js/js-b/test.js new file mode 100644 index 0000000..e30f2f9 --- /dev/null +++ b/tests/tests-basic/workspaces-js/js-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('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" + } +}