From f4194cd80a1a4556b4a9c54d6e6d7659c74cd1bd Mon Sep 17 00:00:00 2001 From: Jonas Wagner Date: Wed, 16 Jun 2021 00:07:25 +0200 Subject: [PATCH] Trying to get esm to play nicely with node and ts --- .eslintrc.json | 3 ++ package.json | 4 +- scripts/endToEndTest.sh | 28 ------------ scripts/prepare.sh | 16 ++++++- tests/endToEndTest.sh | 45 ++++++++++++++++++++ tests/node-module-compatibility/commonjs.js | 3 ++ tests/node-module-compatibility/esm.mjs | 2 + tests/node-module-compatibility/package.json | 1 + {scripts => tests}/puppeteer.ts | 0 {scripts => tests}/snapshots/testsuite.json | 0 10 files changed, 71 insertions(+), 31 deletions(-) delete mode 100755 scripts/endToEndTest.sh create mode 100755 tests/endToEndTest.sh create mode 100644 tests/node-module-compatibility/commonjs.js create mode 100644 tests/node-module-compatibility/esm.mjs create mode 100644 tests/node-module-compatibility/package.json rename {scripts => tests}/puppeteer.ts (100%) rename {scripts => tests}/snapshots/testsuite.json (100%) diff --git a/.eslintrc.json b/.eslintrc.json index 1581d47..95ce683 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,6 +15,9 @@ "no-nested-ternary": "off", "import/prefer-default-export": "off" }, + "ignorePatterns": [ + "tests/node-module-compatibility" + ], "overrides": [ { "files": "examples/**/*", diff --git a/package.json b/package.json index 9b1bb23..fb0ea90 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/scripts/endToEndTest.sh b/scripts/endToEndTest.sh deleted file mode 100755 index e586882..0000000 --- a/scripts/endToEndTest.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/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 - -cp -R examples/node-sharp e2e -cd e2e/node-sharp -npm unlink dont-crop -npm install "../$tarball" -echo "testing commonjs" -npm start -# test esm compatibility as well -if [ "$node_major_version" -ge 14 ] - then - echo "testing esm" - node --loader ts-node/esm example.ts -fi -cd ../../ -rm -rf e2e -echo "testing browser" -ts-node scripts/puppeteer.ts \ No newline at end of file diff --git a/scripts/prepare.sh b/scripts/prepare.sh index 983937a..ce923e3 100755 --- a/scripts/prepare.sh +++ b/scripts/prepare.sh @@ -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 \ No newline at end of file +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 diff --git a/tests/endToEndTest.sh b/tests/endToEndTest.sh new file mode 100755 index 0000000..2524b6e --- /dev/null +++ b/tests/endToEndTest.sh @@ -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 \ No newline at end of file diff --git a/tests/node-module-compatibility/commonjs.js b/tests/node-module-compatibility/commonjs.js new file mode 100644 index 0000000..5061603 --- /dev/null +++ b/tests/node-module-compatibility/commonjs.js @@ -0,0 +1,3 @@ +const { fitGradientToImageData } = require('dont-crop'); + +console.log(fitGradientToImageData); diff --git a/tests/node-module-compatibility/esm.mjs b/tests/node-module-compatibility/esm.mjs new file mode 100644 index 0000000..122179d --- /dev/null +++ b/tests/node-module-compatibility/esm.mjs @@ -0,0 +1,2 @@ +import {fitGradientToImageData} from 'dont-crop'; +console.log(fitGradientToImageData); \ No newline at end of file diff --git a/tests/node-module-compatibility/package.json b/tests/node-module-compatibility/package.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/node-module-compatibility/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/scripts/puppeteer.ts b/tests/puppeteer.ts similarity index 100% rename from scripts/puppeteer.ts rename to tests/puppeteer.ts diff --git a/scripts/snapshots/testsuite.json b/tests/snapshots/testsuite.json similarity index 100% rename from scripts/snapshots/testsuite.json rename to tests/snapshots/testsuite.json