diff --git a/cjs-stub.js b/cjs-stub.js new file mode 100644 index 0000000..62afbc6 --- /dev/null +++ b/cjs-stub.js @@ -0,0 +1 @@ +require.extensions['.cjs'] = null; diff --git a/index.js b/index.js index 160a055..757c475 100644 --- a/index.js +++ b/index.js @@ -60,6 +60,7 @@ function endsInSwcTsx(filename) { return filename.endsWith('.swc.tsx'); } +var cjsStub = path.join(__dirname, 'cjs-stub'); var mjsStub = path.join(__dirname, 'mjs-stub'); function isNodeModules(file) { @@ -109,6 +110,7 @@ var extensions = { }); }, }, + '.cjs': cjsStub, '.coffee': 'coffeescript/register', '.coffee.md': 'coffeescript/register', '.esbuild.js': { @@ -339,6 +341,7 @@ var jsVariantExtensions = [ '.esbuild.jsx', '.esbuild.ts', '.esbuild.tsx', + '.cjs', '.coffee', '.coffee.md', '.esm.js', diff --git a/test/fixtures/cjs/0/package.json b/test/fixtures/cjs/0/package.json new file mode 100644 index 0000000..3dbc1ca --- /dev/null +++ b/test/fixtures/cjs/0/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/fixtures/cjs/0/rechoir.js b/test/fixtures/cjs/0/rechoir.js new file mode 100644 index 0000000..d5fcbca --- /dev/null +++ b/test/fixtures/cjs/0/rechoir.js @@ -0,0 +1,24 @@ +import assert from 'assert'; +import path from 'path'; +import url from 'url'; + +import rechoir from 'rechoir'; +import { extensions } from '../../../../index.js'; + +var fixture = path.resolve('test.cjs'); + +rechoir.prepare(extensions, fixture); + +const { default: result } = await import(url.pathToFileURL(fixture).href); + +const expected = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; + +assert.deepEqual(result, expected); diff --git a/test/fixtures/cjs/0/test.cjs b/test/fixtures/cjs/0/test.cjs new file mode 100644 index 0000000..77bd74f --- /dev/null +++ b/test/fixtures/cjs/0/test.cjs @@ -0,0 +1,9 @@ +module.exports = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; diff --git a/test/fixtures/cjs/1/package.json b/test/fixtures/cjs/1/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/test/fixtures/cjs/1/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/test/fixtures/cjs/1/rechoir.js b/test/fixtures/cjs/1/rechoir.js new file mode 100644 index 0000000..0dc3616 --- /dev/null +++ b/test/fixtures/cjs/1/rechoir.js @@ -0,0 +1,23 @@ +var assert = require('assert'); +var path = require('path'); + +var rechoir = require('rechoir'); +var { extensions } = require('../../../../index.js'); + +var fixture = path.resolve('test.cjs'); + +rechoir.prepare(extensions, fixture); + +const result = require(fixture); + +const expected = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; + +assert.deepEqual(result, expected); diff --git a/test/fixtures/cjs/1/test.cjs b/test/fixtures/cjs/1/test.cjs new file mode 100644 index 0000000..77bd74f --- /dev/null +++ b/test/fixtures/cjs/1/test.cjs @@ -0,0 +1,9 @@ +module.exports = { + data: { + trueKey: true, + falseKey: false, + subKey: { + subProp: 1, + }, + }, +}; diff --git a/test/index.js b/test/index.js index 5708108..e0d1259 100644 --- a/test/index.js +++ b/test/index.js @@ -4,6 +4,7 @@ var expect = require('expect'); var path = require('path'); var Module = require('module'); +var child = require('child_process'); var shell = require('shelljs'); var rechoir = require('rechoir'); @@ -69,8 +70,8 @@ describe('interpret.extensions', function () { return attempts; } - // We will handle the .mjs tests separately - if (ext === '.mjs') { + // We will handle the .mjs & .cjs tests separately + if (ext === '.mjs' || ext === '.cjs') { return attempts; } @@ -274,4 +275,24 @@ describe('interpret.extensions', function () { expect(require(fixture)).toEqual(expected); done(); }); + + it('does not error with the .cjs extension when inside a type: module package', function (done) { + if (nodeVersion.major < 14) { + this.skip(); + } + + this.timeout(0); + + process.chdir(path.join(__dirname, 'fixtures/cjs/0')); + + child.exec('node rechoir.js', done); + }); + + it('does not error with the .cjs extension when inside a type: commonjs package', function (done) { + this.timeout(0); + + process.chdir(path.join(__dirname, 'fixtures/cjs/1')); + + child.exec('node rechoir.js', done); + }); });