Skip to content

Commit

Permalink
chore: run node-esmodule package.json test only in latest node LTS (#621
Browse files Browse the repository at this point in the history
)

While ESM support has been stable in current versions of Node.js 14.x and 16.x, support for importing JSON files is still experimental and evolving.

Since we still want to cover the fact that the package is exporting it's package.json file, this test is now extracted to only run with the updated JSON import syntax which requires import assertions and which was introduced in Node.js 16.14.0.

Fixes #619

Co-authored-by: Robert Kieffer <robert@broofa.com>
  • Loading branch information
ctavan and broofa committed Mar 17, 2022
1 parent 3a033f6 commit e6e5412
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
Empty file added a
Empty file.
4 changes: 0 additions & 4 deletions examples/node-esmodules/example.mjs
Expand Up @@ -10,7 +10,6 @@ import {
version as uuidVersion,
} from 'uuid';
import * as uuid from 'uuid';
import pkg from 'uuid/package.json';

console.log('uuidv1()', uuidv1());

Expand Down Expand Up @@ -65,6 +64,3 @@ console.log('uuid.parse()', uuid.parse(MY_NAMESPACE));
console.log('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
console.log('uuid.validate()', uuid.validate(MY_NAMESPACE));
console.log('uuid.version()', uuid.version(MY_NAMESPACE));

// Some tools like react-native need to introspect the package.json file
console.log('pkg.name', pkg.name);
4 changes: 3 additions & 1 deletion examples/node-esmodules/package.json
Expand Up @@ -3,7 +3,9 @@
"version": "0.0.0",
"private": true,
"scripts": {
"test": "node --experimental-modules --experimental-json-modules example.mjs"
"test:package": "( node --version | grep -vq 'v16' ) || ( node --experimental-json-modules package.mjs )",
"test:example": "node example.mjs",
"test": "npm-run-all test:*"
},
"dependencies": {
"uuid": "file:../../.local/uuid"
Expand Down
4 changes: 4 additions & 0 deletions examples/node-esmodules/package.mjs
@@ -0,0 +1,4 @@
import pkg from 'uuid/package.json' assert { type: 'json' };

// Some tools like react-native need to introspect the package.json file
console.log('pkg.name', pkg.name);
8 changes: 7 additions & 1 deletion scripts/testpack.sh
Expand Up @@ -10,6 +10,7 @@ mkdir -p ../test-pack

cp examples/node-commonjs/example.js ../test-pack/commonjs.js
cp examples/node-esmodules/example.mjs ../test-pack/esmodules.mjs
cp examples/node-esmodules/package.mjs ../test-pack/esmodules-package.mjs

cd ../test-pack

Expand All @@ -18,4 +19,9 @@ npm init -y
npm install ../uuid/uuid-*.tgz

node commonjs.js
node --experimental-json-modules esmodules.mjs
node esmodules.mjs

# Support for json esm imports requires import assertions starting in Node.js v16.14.0 which were
# not supported in earlier versions. Therefore we restrict the ESM test to more recent versions of
# Node.js:
( node --version | grep -vq 'v16' ) || ( node --experimental-json-modules esmodules-package.mjs )

0 comments on commit e6e5412

Please sign in to comment.