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

do not error when processing sources that include node hashbang #217

Merged
merged 5 commits into from
Jul 28, 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
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