diff --git a/.eslintrc.json b/.eslintrc.json index 892a86c..69f640c 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -73,7 +73,7 @@ "no-return-assign": 0, "no-use-before-define": [2, "nofunc"], "no-nested-ternary": 0, - "array-bracket-spacing": [2, "always"], + "array-bracket-spacing": [2, "never"], "prefer-destructuring": 0, "class-methods-use-this": 0, "no-confusing-arrow": 0 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 57362e2..36f6c86 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -7,12 +7,10 @@ on: branches: [ main ] jobs: - build: + lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: lint - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 with: node-version: 20 - run: npm install diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b5b0d85..fc7283c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -8,8 +8,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 with: node-version: 20 - run: npm install @@ -34,7 +33,7 @@ jobs: needs: build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions/setup-node@v3 with: node-version: 20 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 12f7bd1..4e5cb2a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,9 +15,7 @@ jobs: node-version: [20.4, 20.x] os: [ubuntu-latest, windows-latest] steps: - - uses: actions/checkout@v3 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/CHANGELOG.md b/CHANGELOG.md index a24657a..def873c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # changelog - * 2.1.4 _Jan.01.2024_ + * 2.1.5 _Tbd.23.2024_ + * [add tests](https://github.com/iambumblehead/resolvewithplus/pull/66) for path pattern resolver + * 2.1.4 _Jan.23.2024_ * [resolve nested exports defined on named-properties](https://github.com/iambumblehead/resolvewithplus/pull/65) with wildcards, eg `exports: { './*': { default: './src/*/index.js' } }` resolves [this issue at esmock](https://github.com/iambumblehead/esmock/issues/289) * 2.1.3 _Oct.20.2023_ * resolve full path from package.json "main", "browser" and "module" definitions. resolves [this issue at esmock.](https://github.com/iambumblehead/esmock/issues/260) diff --git a/resolvewithplus.js b/resolvewithplus.js index cd0505f..681674a 100644 --- a/resolvewithplus.js +++ b/resolvewithplus.js @@ -23,7 +23,7 @@ const esmStrGlobRe = /(\*)/g const esmStrPathCharRe = /([./])/g const protocolNode = /^node:/ const protocolFile = /^file:/ -const supportedExtensions = [ '.js', '.mjs', '.ts', '.tsx', '.json', '.node' ] +const supportedExtensions = ['.js', '.mjs', '.ts', '.tsx', '.json', '.node'] const supportedIndexNames = supportedExtensions.map(extn => `index${extn}`) const node_modules = 'node_modules' const packagejson = 'package.json' @@ -101,7 +101,7 @@ const firstSyncFilePath = (dir, fileslist) => { // c. let I = I - 1 // 5. return DIRS const getasnode_module_paths = (start, parts = start.split(path.sep)) => { - const next_module_paths = (parts, tuple = [ [], [] ]) => { + const next_module_paths = (parts, tuple = [[], []]) => { if (!parts.length) return tuple[1] @@ -169,7 +169,6 @@ const getesmkeyvalglobreplaced = (esmkey, esmval, pathlocal) => { const isesmkeymatchRe = new RegExp( esmkey.replace(esmStrPathCharRe, '\\$1').replace(esmStrGlobRe, '(.*)')) - // eslint-disable-next-line prefer-destructuring const globmatch = (pathlocal.match(isesmkeymatchRe) || [])[1] return globmatch && esmval.replace('*', globmatch) } @@ -247,7 +246,7 @@ const esmparselist = (list, spec, specifier, opts, key = list[0]) => { } const esmparse = (spec, specifier, opts = {}) => { - const priority = opts.priority || [ specruntime, specdefault ] + const priority = opts.priority || [specruntime, specdefault] let indexval = false if (typeof spec === 'string') @@ -514,7 +513,7 @@ const esmparseimportpkg = (pspecifier, start, opts) => { // b. LOAD_AS_DIRECTORY(DIR/X) // const getasnode_module = (targetpath, start, opts) => { - const [ pname, pspecifier ] = gettargetnameandspecifier(targetpath) + const [pname, pspecifier] = gettargetnameandspecifier(targetpath) if (isESMImportSubpathRe.test(pname)) return esmparseimportpkg(targetpath, start, opts) @@ -576,7 +575,7 @@ const createopts = (moduleId, parent, opts) => { // packagejson mock to more easily test different patternsn opts.packagejsonmap = opts.packagejsonmap || null if (!Array.isArray(opts.priority)) { - opts.priority = opts.isbrowser ? [ specbrowser ] : [] + opts.priority = opts.isbrowser ? [specbrowser] : [] opts.priority.push(spectype) opts.priority.push(specruntime) opts.priority.push(specdefault) @@ -609,5 +608,6 @@ export default Object.assign(resolvewith, { }) export { - gettargetindextop + gettargetindextop, + getesmkeyvalglobreplaced } diff --git a/tests/tests-basic/tests-basic.test.js b/tests/tests-basic/tests-basic.test.js index 6d778b8..45eeb4b 100644 --- a/tests/tests-basic/tests-basic.test.js +++ b/tests/tests-basic/tests-basic.test.js @@ -6,8 +6,10 @@ import url from 'url' import path from 'path' import test from 'node:test' import assert from 'node:assert/strict' + import resolvewithplus, { - gettargetindextop + gettargetindextop, + getesmkeyvalglobreplaced } from '../../resolvewithplus.js' const tofileurl = p => url.pathToFileURL(p).href @@ -349,10 +351,10 @@ test('should handle mixed exports, import', () => { type: 'module', exports: { './package.json': './package.json', - '.': [ { + '.': [{ import: resolvingpackagejsonmodulerelpath, require: resolvingpackagejsonmodulerelpathother - }, './index.cjs' ], + }, './index.cjs'], './helpers': { import: resolvingpackagejsonmodulerelpathother, require: resolvingpackagejsonmodulerelpathother @@ -361,10 +363,10 @@ test('should handle mixed exports, import', () => { import: resolvingpackagejsonmodulerelpathother, types: resolvingpackagejsonmodulerelpathother }, - './yargs': [ { + './yargs': [{ import: resolvingpackagejsonmodulerelpathother, require: resolvingpackagejsonmodulerelpathother - }, './yargs' ] + }, './yargs'] } } } @@ -382,10 +384,10 @@ test('should handle mixed exports, commonjs', () => { type: 'commonjs', exports: { './package.json': './package.json', - '.': [ { + '.': [{ import: resolvingpackagejsonmodulerelpathother, require: resolvingpackagejsonmodulerelpath - }, resolvingpackagejsonmodulerelpathother ], + }, resolvingpackagejsonmodulerelpathother], './helpers': { import: resolvingpackagejsonmodulerelpathother, require: resolvingpackagejsonmodulerelpathother @@ -394,10 +396,10 @@ test('should handle mixed exports, commonjs', () => { import: resolvingpackagejsonmodulerelpathother, types: './browser.d.ts' }, - './yargs': [ { + './yargs': [{ import: resolvingpackagejsonmodulerelpathother, require: resolvingpackagejsonmodulerelpathother - }, resolvingpackagejsonmodulerelpathother ] + }, resolvingpackagejsonmodulerelpathother] } } } @@ -514,7 +516,7 @@ test('resolve full path for older main, browser and export fields', () => { test('should return browser or import whichiver first', () => { const resolvedbrowser = resolvewithplus('test', import.meta.url, { - priority: [ 'browser', 'import', 'default' ], + priority: ['browser', 'import', 'default'], packagejsonmap: { [resolvingpackagejsonpath]: { name: 'test', @@ -533,7 +535,7 @@ test('should return browser or import whichiver first', () => { assert.strictEqual(resolvedbrowser, resolvingpackagejsonmoduleurlpath) const resolveddefault = resolvewithplus('test', import.meta.url, { - priority: [ 'default', 'browser', 'import' ], + priority: ['default', 'browser', 'import'], packagejsonmap: { [resolvingpackagejsonpath]: { name: 'test', @@ -588,7 +590,7 @@ test('should detect module type from package.json', () => { assert.strictEqual(resolvedmodule, resolvingpackagejsonmoduleurlpath) const resolvedmodule2 = resolvewithplus('test', import.meta.url, { - priority: [ 'import', 'browser', 'default' ], + priority: ['import', 'browser', 'default'], packagejsonmap: { [resolvingpackagejsonpath]: { name: 'test', @@ -663,7 +665,7 @@ test('gettargetindextop should resolve a fullpath', () => { version: '4.1.0', description: 'Light-weight Fetch ...', module: resolvingpackagejsonmodulerelpath - }, { priority: [ 'import' ] }, dir) + }, { priority: ['import'] }, dir) assert.strictEqual( indexpathmodule, @@ -680,3 +682,23 @@ test('gettargetindextop should resolve a fullpath', () => { indexpathmain, url.fileURLToPath(resolvingpackagejsonmoduleurlpath)) }) + +test('getesmkeyvalglobreplaced should expand globby path groups', () => { + [[ + 'src/mystuff/index.js', + 'mystuff/*', 'src/mystuff/*', 'mystuff/index.js' + ], [ + 'types/mystuff/index.d.ts', + 'mystuff/*', 'types/mystuff/*', 'mystuff/index.d.ts' + ], [ + './src/features/x.js', + './features/*.js', './src/features/*.js', './features/x.js' + ], [ + './src/features/y/y.js', + './features/*.js', './src/features/*.js', './features/y/y.js' + ], [ + './src/internal/z.js', + '#internal/*.js', './src/internal/*.js', '#internal/z.js' + ]].map(tuple => console.log(tuple) || assert.strictEqual( + getesmkeyvalglobreplaced(...tuple.slice(1)), tuple[0])) +}) diff --git a/tests/tests-basic/workspaces-js/js-b/test.js b/tests/tests-basic/workspaces-js/js-b/test.js index e30f2f9..6dda13f 100644 --- a/tests/tests-basic/workspaces-js/js-b/test.js +++ b/tests/tests-basic/workspaces-js/js-b/test.js @@ -2,8 +2,16 @@ 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')) -}) +if (import.meta.resolve.constructor.name === 'AsyncFunction') { + test('should return workspace paths', async () => { + assert.strictEqual( + await import.meta.resolve('js-a'), + resolvewithplus('js-a')) + }) +} else { + test('should return workspace paths', () => { + assert.strictEqual( + import.meta.resolve('js-a'), + resolvewithplus('js-a')) + }) +}