Skip to content

Commit

Permalink
Merge pull request #217 from iambumblehead/resolve-failing-hashbang-test
Browse files Browse the repository at this point in the history
do not error when processing sources that include node hashbang
  • Loading branch information
iambumblehead authored Jul 28, 2023
2 parents 9588f85 + 3f78d0a commit a9ac615
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
# node-14's npm must be updated, but no longer updatable due to:
# https://github.com/npm/cli/issues/2663
# because of this, node-14 test strategy is removed :(
node-version: [16.x, 18.x, 20.x]
node-version: [16.x, 18.16.x, 20.x]
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# changelog

* 2.3.3 _Jul.27.2023_
* [do not error when processing node hashbang](https://github.com/iambumblehead/esmock/pull/217) scripts from @tommy-mitchell
* 2.3.2 _Jul.22.2023_
* [restore ava unit-test](https://github.com/iambumblehead/esmock/pull/213) process at node20 test pipeline
* [investigate problems](https://github.com/iambumblehead/esmock/issues/209) using tsx from @tommy-mitchell
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esmock",
"type": "module",
"version": "2.3.2",
"version": "2.3.3",
"license": "ISC",
"readmeFilename": "README.md",
"description": "provides native ESM import and globals mocking for unit tests",
Expand Down
9 changes: 7 additions & 2 deletions src/esmockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const exportNamesRe = /.*exportNames=(.*)/
const withHashRe = /.*#-#/
const isesmRe = /isesm=true/
const isnotfoundRe = /isfound=false/
const hashbangRe = /^(#![^\n]*\n)/

const globalPreload = (({ port }) => (
port.addEventListener('message', ev => (
Expand Down Expand Up @@ -137,12 +138,16 @@ const load = async (url, context, nextLoad) => {
if (treeid) {
const [specifier, importedNames] = parseImportsTree(treeidspec)
if (importedNames && importedNames.length) {
const source = String((await nextLoad(url, context)).source)
const hbang = (source.match(hashbangRe) || [])[0] || ''
const sourcesafe = hbang ? source.replace(hashbangRe, '') : source

return {
format: 'module',
shortCircuit: true,
responseURL: encodeURI(url),
source: `import {${importedNames}} from '${specifier}';`
+ (await nextLoad(url, context)).source
source: hbang + `import {${importedNames}} from '${specifier}';`
+ sourcesafe
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions tests/tests-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
},
"dependencies": {
"esmock": "file:..",
"@ava/typescript": "^4.0.0",
"@tsconfig/node14": "^1.0.3",
"@types/node": "^20.1.5",
"ava": "^5.2.0",
"@ava/typescript": "^4.1.0",
"@tsconfig/node16": "^16.1.0",
"@types/node": "^20.4.5",
"ava": "^5.3.1",
"cross-env": "^7.0.3",
"rimraf": "^3.0.2",
"typescript": "^5.0.4"
"rimraf": "^5.0.1",
"typescript": "^5.1.6"
},
"scripts": {
"test-metaresolve": "rimraf dist && tsc && cross-env \"NODE_OPTIONS=--experimental-import-meta-resolve --loader=esmock\" NODE_NO_WARNINGS=1 ava",
Expand Down
2 changes: 1 addition & 1 deletion tests/tests-source-map/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"module": "Node16",
"moduleResolution": "Node16",
Expand Down

0 comments on commit a9ac615

Please sign in to comment.