Skip to content

Commit

Permalink
Trying to get esm to play nicely with node and ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jwagner committed Jun 15, 2021
1 parent b7ab9de commit f4194cd
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"no-nested-ternary": "off",
"import/prefer-default-export": "off"
},
"ignorePatterns": [
"tests/node-module-compatibility"
],
"overrides": [
{
"files": "examples/**/*",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dont-crop",
"version": "0.0.1",
"version": "0.0.2",
"description": "A library to fit gradients to images and extract it's dominant colors to help you avoid cropping images.",
"main": "./dist/cjs/lib.js",
"module": "./dist/mjs/lib.js",
Expand All @@ -22,7 +22,7 @@
"watch": "jest --watchAll --detectOpenHandles",
"test": "jest",
"prepare": "scripts/prepare.sh",
"endToEndTest": "./scripts/endToEndTest.sh"
"endToEndTest": "./tests/endToEndTest.sh"
},
"author": "Jonas Wagner",
"license": "MIT",
Expand Down
28 changes: 0 additions & 28 deletions scripts/endToEndTest.sh

This file was deleted.

16 changes: 15 additions & 1 deletion scripts/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,18 @@ ts_config="tsconfig-release.json"
rm -rf dist/*
tsc -P "$ts_config" -d
tsc -P "$ts_config" -d -m commonjs --outDir dist/cjs/
find dist/ -name *.d.ts -not -name lib.d.ts -delete
find dist/ -name *.d.ts -not -name lib.d.ts -delete
# Node insists on .js extensions for esm import/from statements,
# ts refuses to add them but tolerates the presence of .js.
# That doesn't seem to work with ts-jest and probably other options.
# So this fragile and soul crushing hack will have to do for now.
for f in dist/mjs/*.js
do
sed -i -E "s/from '([^']+)';$/from '\1.js';/g" "$f"
done
# another hack to make modules play nicely with node
# in order to support commonjs the parent module doesn't have
# type module. But without that imports in the mjs variant
# will be treated as commonjs. This fixes that.
# At least it's more robust than the hack above.
echo '{"type":"module"}' > dist/mjs/package.json
45 changes: 45 additions & 0 deletions tests/endToEndTest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
set -e
node_major_version=$(node --version|sed 's/v\([^.]*\).*/\1/g')
rm -rf e2e
mkdir -p e2e

rm -f dont-crop*.tgz
npm pack
tarball=(dont-crop*.tgz)

mv "$tarball" e2e
cd e2e

# run node sharp example as end to end test for ts-node
cp -R ../examples/node-sharp .
cd node-sharp
npm unlink dont-crop
npm install "../$tarball"
echo "testing ts-node commonjs"
npm start
# test esm compatibility as well
if [ "$node_major_version" -ge 14 ]
then
echo "testing ts-node esm"
node --loader ts-node/esm example.ts
fi
cd ..

# test plain node module compatibility
cp -R ../tests/node-module-compatibility .
cd node-module-compatibility
npm install "../$tarball"
echo "testing node commonjs"
node commonjs.js
if [ "$node_major_version" -ge 14 ]
then
node esm.mjs
fi
cd ..

cd ..
rm -rf e2e
# run testsuite as end to end test using pupeteer
echo "testing browser"
ts-node tests/puppeteer.ts
3 changes: 3 additions & 0 deletions tests/node-module-compatibility/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { fitGradientToImageData } = require('dont-crop');

console.log(fitGradientToImageData);
2 changes: 2 additions & 0 deletions tests/node-module-compatibility/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import {fitGradientToImageData} from 'dont-crop';
console.log(fitGradientToImageData);
1 change: 1 addition & 0 deletions tests/node-module-compatibility/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
File renamed without changes.
File renamed without changes.

0 comments on commit f4194cd

Please sign in to comment.