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

Add workspace test ts #47

Merged
merged 3 commits into from
Sep 13, 2023
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
25 changes: 21 additions & 4 deletions resolvewithplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
10 changes: 9 additions & 1 deletion tests/tests-basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 3 additions & 0 deletions tests/tests-basic/workspaces-js/js-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo () {
return 'foo'
}
6 changes: 6 additions & 0 deletions tests/tests-basic/workspaces-js/js-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "js-a",
"version": "0.0.0",
"type": "module",
"main": "index.js"
}
5 changes: 5 additions & 0 deletions tests/tests-basic/workspaces-js/js-b/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {foo} from 'a'

export default function test () {
return foo()
}
11 changes: 11 additions & 0 deletions tests/tests-basic/workspaces-js/js-b/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
9 changes: 9 additions & 0 deletions tests/tests-basic/workspaces-js/js-b/test.js
Original file line number Diff line number Diff line change
@@ -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'))
})
3 changes: 3 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-a/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo () {
return 'foo'
}
6 changes: 6 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ts-a",
"version": "0.0.0",
"type": "module",
"main": "index.js"
}
5 changes: 5 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-b/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {foo} from 'a'

export default function test () {
return foo()
}
14 changes: 14 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-b/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
12 changes: 12 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-b/test.ts
Original file line number Diff line number Diff line change
@@ -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'))
})
*/
8 changes: 8 additions & 0 deletions tests/tests-basic/workspaces-ts/ts-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"esm": true,
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"module": "ESNext",
"moduleResolution": "node"
}
}