From d1971dbe32b21c5a675d9d6f0c1eb24f8b2f37c6 Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 17 Oct 2022 21:21:16 -0700 Subject: [PATCH 1/7] add test script variants using --experimental-import-meta-resolve --- .github/workflows/node.js.yml | 2 +- tests/tests-jest-ts/package.json | 8 ++++++-- tests/tests-jest/package.json | 8 ++++++-- tests/tests-node/package.json | 3 ++- tests/tests-nodets/package.json | 4 +++- tests/tests-source-map/package.json | 4 +++- tests/tests-tsm/package.json | 4 +++- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 33a55c9e..724944e9 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -12,7 +12,7 @@ on: jobs: build: runs-on: ${{ matrix.os }} - timeout-minutes: 6 + timeout-minutes: 12 strategy: matrix: node-version: [14.x, 16.x, 18.x] diff --git a/tests/tests-jest-ts/package.json b/tests/tests-jest-ts/package.json index b928a79b..a60db5fe 100644 --- a/tests/tests-jest-ts/package.json +++ b/tests/tests-jest-ts/package.json @@ -20,8 +20,12 @@ "runner": "jest-light-runner" }, "scripts": { - "test:default": "NODE_OPTIONS=\"--loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts", - "test:win32": "set \"NODE_OPTIONS=--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts", + "test:default-metaresolve": "NODE_OPTIONS=\"--experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts", + "test:default-nometaresolve": "NODE_OPTIONS=\"--loader=ts-node/esm --loader=esmock\" jest esmock.node-jest.test.ts", + "test:default": "npm run test:default-metaresolve && npm run test:default-nometaresolve", + "test:win32-metaresolve": "set \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts", + "test:win32-nometaresolve": "set \"NODE_OPTIONS=--loader=ts-node/esm --loader=esmock\" && jest esmock.node-jest.test.ts", + "test:win32": "npm run test:win32-metaresolve && npm run test:win32-nometaresolve", "test": "run-script-os" } } diff --git a/tests/tests-jest/package.json b/tests/tests-jest/package.json index 20cbf9d6..d19697b5 100644 --- a/tests/tests-jest/package.json +++ b/tests/tests-jest/package.json @@ -19,8 +19,12 @@ "runner": "jest-light-runner" }, "scripts": { - "test:default": "NODE_OPTIONS=--loader=esmock jest", - "test:win32": "set NODE_OPTIONS=--loader=esmock && jest", + "test:default-metaresolve": "NODE_OPTIONS=\"--experimental-import-meta-resolve --loader=esmock\" jest", + "test:default-nometaresolve": "NODE_OPTIONS=--loader=esmock jest", + "test:default": "npm run test:default-metaresolve && npm run test:default-nometaresolve", + "test:win32-metaresolve": "set \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=esmock\" && jest", + "test:win32-nometaresolve": "set NODE_OPTIONS=--loader=esmock && jest", + "test:win32": "npm run test:win32-metaresolve && npm run test:win32-metaresolve", "test": "run-script-os" } } diff --git a/tests/tests-node/package.json b/tests/tests-node/package.json index 0abfe517..456cfaba 100644 --- a/tests/tests-node/package.json +++ b/tests/tests-node/package.json @@ -13,6 +13,7 @@ "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" }, "scripts": { - "test": "node --loader=esmock --test" + "test:metaresolve": "node --experimental-import-meta-resolve --loader=esmock --test", + "test": "node --loader=esmock --test && npm run test:metaresolve" } } diff --git a/tests/tests-nodets/package.json b/tests/tests-nodets/package.json index 0c0723a5..009ab782 100644 --- a/tests/tests-nodets/package.json +++ b/tests/tests-nodets/package.json @@ -14,6 +14,8 @@ "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" }, "scripts": { - "test": "node --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts" + "test-metaresolve": "node --experimental-import-meta-resolve --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts", + "test-nometaresolve": "node --loader=ts-node/esm --loader=esmock --test esmock.node-ts.test.ts esmock.node-ts.importing.test.ts", + "test": "npm run test-metaresolve && npm run test-nometaresolve" } } diff --git a/tests/tests-source-map/package.json b/tests/tests-source-map/package.json index 530c49b2..883bb287 100644 --- a/tests/tests-source-map/package.json +++ b/tests/tests-source-map/package.json @@ -16,7 +16,9 @@ "typescript": "^4.8.3" }, "scripts": { - "test": "rimraf dist && tsc && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava", + "test-metaresolve": "rimraf dist && tsc && cross-env \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=esmock\" NODE_NO_WARNINGS=1 ava", + "test-nometaresolve": "rimraf dist && tsc && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava", + "test": "npm run test-metaresolve && npm run test-nometaresolve", "test-no-maps": "rimraf dist && tsc --sourceMap false && cross-env NODE_OPTIONS=--loader=esmock NODE_NO_WARNINGS=1 ava" }, "ava": { diff --git a/tests/tests-tsm/package.json b/tests/tests-tsm/package.json index 4969a6cf..8600df41 100644 --- a/tests/tests-tsm/package.json +++ b/tests/tests-tsm/package.json @@ -15,6 +15,8 @@ "babelGeneratedDoubleDefault": "file:../local/babelGeneratedDoubleDefault" }, "scripts": { - "test": "node --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts" + "test:metaresolve": "node --experimental-import-meta-resolve --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts", + "test:nometaresolve": "node --loader=tsm --loader=esmock --test esmock.node-tsm.test.ts", + "test": "npm run test:metaresolve && npm run test:nometaresolve" } } From 1679cc0fff49ad83d2c08e7191b8bf377329134d Mon Sep 17 00:00:00 2001 From: chris Date: Tue, 18 Oct 2022 19:58:53 -0700 Subject: [PATCH 2/7] use import.meta.resolve when defined --- src/esmockModule.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index ae8ef1c5..726adb1a 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -13,7 +13,13 @@ import { const isObj = o => typeof o === 'object' && o const isDefaultIn = o => isObj(o) && 'default' in o const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ +const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() +const rootDirSlashRe = /^\// +const protocolFileRe = /^file:/ + +const asFileURL = p => protocolFileRe.test(p) ? p + : `file:///${p.replace(rootDirSlashRe, '')}` const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? Object.assign({}, defLive, def) : def @@ -92,6 +98,14 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { return mockModuleKey } +const esmockMetaResolve = async (id, parent) => { + try { + return decodeURI(await import.meta.resolve(id, asFileURL(parent))) + } catch (e) { + return null + } +} + const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { ids = ids || Object.keys(defs) id = ids[0] @@ -99,7 +113,9 @@ const esmockModuleId = async (parent, treeid, defs, ids, opt, mocks, id) => { if (!id) return mocks - const fileURL = resolvewith(id, parent) + const fileURL = isMetaResolve + ? await esmockMetaResolve(id, parent) + : resolvewith(id, parent) if (!fileURL && opt.isModuleNotFoundError !== false) throw esmockErr.errModuleIdNotFound(id, parent) From 0e5823618d2a0f1411b33da06e680b5d52ee107b Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 09:16:32 -0700 Subject: [PATCH 3/7] remove e from catch(e) expression --- src/esmockModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 726adb1a..426dd0b9 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -101,7 +101,7 @@ const esmockModuleCreate = async (treeid, def, id, fileURL, opt) => { const esmockMetaResolve = async (id, parent) => { try { return decodeURI(await import.meta.resolve(id, asFileURL(parent))) - } catch (e) { + } catch { return null } } From f6e86d64fe3af1eb651aa0d71bcbdff77835b0e2 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 09:43:15 -0700 Subject: [PATCH 4/7] use s.startsWith rather than re.test --- src/esmockModule.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 426dd0b9..257d8ddd 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -16,9 +16,8 @@ const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() const rootDirSlashRe = /^\// -const protocolFileRe = /^file:/ -const asFileURL = p => protocolFileRe.test(p) ? p +const asFileURL = p => p.startsWith('file:') ? p : `file:///${p.replace(rootDirSlashRe, '')}` const esmockModuleMergeDefault = (defLive, def) => From 024e61b39eae3f5c3ecdf8e67d3715e6cb9fabd2 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 09:45:20 -0700 Subject: [PATCH 5/7] add extra forward slashes to be more explicit --- src/esmockModule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 257d8ddd..9020005d 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -17,7 +17,7 @@ const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() const rootDirSlashRe = /^\// -const asFileURL = p => p.startsWith('file:') ? p +const asFileURL = p => p.startsWith('file://') ? p : `file:///${p.replace(rootDirSlashRe, '')}` const esmockModuleMergeDefault = (defLive, def) => From 09e4417729985de6a75cede00bbd9438793c5051 Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 10:48:56 -0700 Subject: [PATCH 6/7] per review, convert path using pathToFileURL --- src/esmockModule.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 9020005d..3eed60d7 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -1,4 +1,5 @@ import fs from 'fs' +import url from 'node:url' import resolvewith from 'resolvewithplus' import esmockErr from './esmockErr.js' @@ -16,9 +17,7 @@ const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() const rootDirSlashRe = /^\// - -const asFileURL = p => p.startsWith('file://') ? p - : `file:///${p.replace(rootDirSlashRe, '')}` +const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) => (isObj(defLive) && isObj(def)) ? Object.assign({}, defLive, def) : def From f326672a771d6f7a7551db97bafe75610ba3f25b Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 19 Oct 2022 11:03:17 -0700 Subject: [PATCH 7/7] remove unused var --- src/esmockModule.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/esmockModule.js b/src/esmockModule.js index 3eed60d7..13a97ae4 100644 --- a/src/esmockModule.js +++ b/src/esmockModule.js @@ -16,7 +16,6 @@ const isDefaultIn = o => isObj(o) && 'default' in o const isDirPathRe = /^\.?\.?([a-zA-Z]:)?(\/|\\)/ const isMetaResolve = typeof import.meta.resolve === 'function' const nextId = ((id = 0) => () => ++id)() -const rootDirSlashRe = /^\// const asFileURL = p => p.startsWith('file://') ? p : url.pathToFileURL(p) const esmockModuleMergeDefault = (defLive, def) =>