Skip to content

Commit

Permalink
fix: require doesn't work in esm projects (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamann committed Jan 28, 2024
1 parent f49afee commit 3fb1d4c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"unit:ts-esm": "cross-env TS_NODE_PROJECT=./test/configs/ts-esm.tsconfig.json FASTIFY_AUTOLOAD_TYPESCRIPT=1 node -r ts-node/register --loader ts-node/esm suite-runner.js \"templates/app-ts-esm/test/**/*.test.ts\"",
"unit:suites": "node should-skip-test-suites.js || npm run all-suites",
"all-suites": "npm run unit:cjs && npm run unit:esm && npm run unit:ts-cjs && npm run unit:ts-esm",
"unit:cli-js-esm": "node suite-runner.js \"test/esm/**/*.test.js\"",
"unit:cli-js": "tap \"test/**/*.test.js\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"unit:cli-ts": "cross-env TS_NODE_PROJECT=./test/configs/ts-cjs.tsconfig.json tap \"test/**/*.test.ts\" --no-coverage --timeout 400 --jobs 1 --color -R specy",
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts",
"unit:cli": "npm run unit:cli-js && npm run unit:cli-ts && npm run unit:cli-js-esm",
"test:cli-and-typescript": "npm run unit:cli && npm run test:typescript",
"test:typescript": "tsd templates/plugin -t ./../../index.d.ts && tsc --project templates/app-ts/tsconfig.json --noEmit && tsc --project templates/app-ts-esm/tsconfig.json --noEmit"
},
Expand Down
8 changes: 8 additions & 0 deletions test/esm/data/custom-logger.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = {
name: 'Custom Logger',
customLevels: {
test: 99
}
}
3 changes: 3 additions & 0 deletions test/esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
16 changes: 16 additions & 0 deletions test/esm/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { requireModule } from '../../util.js'
import { resolve, join } from 'node:path'
import t from 'tap'
import * as url from 'url'

const test = t.test

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

test('requiring a commonjs module works in an esm project', (t) => {
t.plan(1)
const module = requireModule(
resolve(join(__dirname, './data/custom-logger.cjs'))
)
t.strictSame(module, { name: 'Custom Logger', customLevels: { test: 99 } })
})
4 changes: 1 addition & 3 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ function exit (message) {
function requireModule (moduleName) {
if (fs.existsSync(moduleName)) {
const moduleFilePath = path.resolve(moduleName)
const moduleFileExtension = path.extname(moduleName)
const modulePath = moduleFilePath.split(moduleFileExtension)[0]
return require(modulePath)
return require(moduleFilePath)
} else {
return require(moduleName)
}
Expand Down

0 comments on commit 3fb1d4c

Please sign in to comment.