Skip to content

Commit f6695f0

Browse files
authored
chore(esm): remove code for Node v12 (#4874)
1 parent 59f6192 commit f6695f0

File tree

4 files changed

+42
-95
lines changed

4 files changed

+42
-95
lines changed

lib/nodejs/esm-utils.js

+40-70
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const formattedImport = async file => {
1010
// the location of the syntax error in the error thrown.
1111
// This is problematic because the user can't see what file has the problem,
1212
// so we add the file location to the error.
13-
// This `if` should be removed once Node.js fixes the problem.
13+
// TODO: remove once Node.js fixes the problem.
1414
if (
1515
err instanceof SyntaxError &&
1616
err.message &&
@@ -30,64 +30,52 @@ const formattedImport = async file => {
3030
return import(file);
3131
};
3232

33-
const hasStableEsmImplementation = (() => {
34-
const [major, minor] = process.version.split('.');
35-
// ESM is stable from v12.22.0 onward
36-
// https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules
37-
const majorNumber = parseInt(major.slice(1), 10);
38-
const minorNumber = parseInt(minor, 10);
39-
return majorNumber > 12 || (majorNumber === 12 && minorNumber >= 22);
40-
})();
41-
42-
exports.requireOrImport = hasStableEsmImplementation
43-
? async file => {
44-
if (path.extname(file) === '.mjs') {
45-
return formattedImport(file);
46-
}
33+
exports.requireOrImport = async file => {
34+
if (path.extname(file) === '.mjs') {
35+
return formattedImport(file);
36+
}
37+
try {
38+
return dealWithExports(await formattedImport(file));
39+
} catch (err) {
40+
if (
41+
err.code === 'ERR_MODULE_NOT_FOUND' ||
42+
err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
43+
err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
44+
) {
4745
try {
48-
return dealWithExports(await formattedImport(file));
49-
} catch (err) {
46+
// Importing a file usually works, but the resolution of `import` is the ESM
47+
// resolution algorithm, and not the CJS resolution algorithm. We may have
48+
// failed because we tried the ESM resolution, so we try to `require` it.
49+
return require(file);
50+
} catch (requireErr) {
5051
if (
51-
err.code === 'ERR_MODULE_NOT_FOUND' ||
52-
err.code === 'ERR_UNKNOWN_FILE_EXTENSION' ||
53-
err.code === 'ERR_UNSUPPORTED_DIR_IMPORT'
52+
requireErr.code === 'ERR_REQUIRE_ESM' ||
53+
(requireErr instanceof SyntaxError &&
54+
requireErr
55+
.toString()
56+
.includes('Cannot use import statement outside a module'))
5457
) {
55-
try {
56-
// Importing a file usually works, but the resolution of `import` is the ESM
57-
// resolution algorithm, and not the CJS resolution algorithm. So in this case
58-
// if we fail, we may have failed because we tried the ESM resolution and failed
59-
// So we try to `require` it
60-
return require(file);
61-
} catch (requireErr) {
62-
if (
63-
requireErr.code === 'ERR_REQUIRE_ESM' ||
64-
(requireErr instanceof SyntaxError &&
65-
requireErr
66-
.toString()
67-
.includes('Cannot use import statement outside a module'))
68-
) {
69-
// ERR_REQUIRE_ESM happens when the test file is a JS file, but via type:module is actually ESM,
70-
// AND has an import to a file that doesn't exist.
71-
// This throws an `ERR_MODULE_NOT_FOUND` error above,
72-
// and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
73-
// What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
74-
// and not the `ERR_REQUIRE_ESM` error, which is a red herring.
75-
//
76-
// SyntaxError happens when in an edge case: when we're using an ESM loader that loads
77-
// a `test.ts` file (i.e. unrecognized extension), and that file includes an unknown
78-
// import (which thows an ERR_MODULE_NOT_FOUND). require-ing it will throw the
79-
// syntax error, because we cannot require a file that has import-s.
80-
throw err;
81-
} else {
82-
throw requireErr;
83-
}
84-
}
85-
} else {
58+
// ERR_REQUIRE_ESM happens when the test file is a JS file, but via type:module is actually ESM,
59+
// AND has an import to a file that doesn't exist.
60+
// This throws an `ERR_MODULE_NOT_FOUND` error above,
61+
// and when we try to `require` it here, it throws an `ERR_REQUIRE_ESM`.
62+
// What we want to do is throw the original error (the `ERR_MODULE_NOT_FOUND`),
63+
// and not the `ERR_REQUIRE_ESM` error, which is a red herring.
64+
//
65+
// SyntaxError happens when in an edge case: when we're using an ESM loader that loads
66+
// a `test.ts` file (i.e. unrecognized extension), and that file includes an unknown
67+
// import (which throws an ERR_MODULE_NOT_FOUND). `require`-ing it will throw the
68+
// syntax error, because we cannot require a file that has `import`-s.
8669
throw err;
70+
} else {
71+
throw requireErr;
8772
}
8873
}
74+
} else {
75+
throw err;
8976
}
90-
: implementationOfRequireOrImportForUnstableEsm;
77+
}
78+
};
9179

9280
function dealWithExports(module) {
9381
if (module.default) {
@@ -104,21 +92,3 @@ exports.loadFilesAsync = async (files, preLoadFunc, postLoadFunc) => {
10492
postLoadFunc(file, result);
10593
}
10694
};
107-
108-
/* istanbul ignore next */
109-
async function implementationOfRequireOrImportForUnstableEsm(file) {
110-
if (path.extname(file) === '.mjs') {
111-
return formattedImport(file);
112-
}
113-
// This is currently the only known way of figuring out whether a file is CJS or ESM in
114-
// Node.js that doesn't necessitate calling `import` first.
115-
try {
116-
return require(file);
117-
} catch (err) {
118-
if (err.code === 'ERR_REQUIRE_ESM') {
119-
return formattedImport(file);
120-
} else {
121-
throw err;
122-
}
123-
}
124-
}

test/integration/diffs.spec.js

-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ describe('diffs', function () {
6969
var diffs, expected;
7070

7171
before(function (done) {
72-
// @TODO: It should be removed when Node.js 10 LTS is not supported.
73-
const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
74-
if (nodeVersion === 10) {
75-
this.skip();
76-
}
77-
7872
run('diffs/diffs.fixture.js', [], function (err, res) {
7973
if (err) {
8074
done(err);

test/integration/glob.spec.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@ var testGlob = {
187187
})
188188
};
189189

190-
var isFlakeyNode = (function () {
191-
var version = process.versions.node.split('.');
192-
return (
193-
version[0] === '0' && version[1] === '10' && process.platform === 'win32'
194-
);
195-
})();
196-
197190
function execMochaWith(validate) {
198191
return function execMocha(glob, assertOn, done) {
199192
exec(
@@ -206,12 +199,8 @@ function execMochaWith(validate) {
206199
function (error, stdout, stderr) {
207200
try {
208201
validate(error, stderr);
209-
if (isFlakeyNode && error && stderr === '') {
210-
execMocha(glob, assertOn, done);
211-
} else {
212-
assertOn({stdout: stdout, stderr: stderr});
213-
done();
214-
}
202+
assertOn({stdout: stdout, stderr: stderr});
203+
done();
215204
} catch (assertion) {
216205
done(assertion);
217206
}

test/integration/no-diff.spec.js

-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ var run = helpers.runMocha;
66
describe('no-diff', function () {
77
describe('when enabled', function () {
88
it('should not display a diff', function (done) {
9-
// @TODO: It should be removed when Node.js 10 LTS is not supported.
10-
const nodeVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
11-
if (nodeVersion === 10) {
12-
this.skip();
13-
}
14-
159
run('no-diff.fixture.js', ['--no-diff'], function (err, res) {
1610
if (err) {
1711
done(err);

0 commit comments

Comments
 (0)