Skip to content

Commit

Permalink
Fix type importing for some tsc configurations (thanks @tje) (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
boronine authored Jul 26, 2023
1 parent ad48482 commit 8d4c6d9
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 259 deletions.
33 changes: 22 additions & 11 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

set -e

rm -rf dist assets
mkdir -p dist assets
rm -rf dist assets test/tmp
mkdir -p dist assets test/tmp

# Store package version
PACKAGE_VERSION=$(node -p -e 'require("./package.json").version')
echo "${PACKAGE_VERSION}" >assets/VERSION

# First build ESM version that is used for testing
npx tsc src/hsluv.ts --outDir dist/esm --module es6 --target es6
echo '{"type": "module"}' >dist/esm/package.json
npx tsc src/hsluv.ts --outDir dist --module es6 --target es6
mv dist/hsluv.js dist/hsluv.mjs

# Test against snapshot before continuing
node test/test.mjs

# Build CommonJS version
npx tsc src/hsluv.ts --outDir dist/cjs --module commonjs --target es6
echo '{"type": "commonjs"}' >dist/cjs/package.json
npx tsc src/hsluv.ts --outDir dist --module commonjs --target es6
mv dist/hsluv.js dist/hsluv.cjs

# Build d.ts file
npx tsc src/hsluv.ts --outDir dist --declaration --emitDeclarationOnly

# Build hsluv.min.js
echo 'import {Hsluv} from "./esm/hsluv.js";window.Hsluv = Hsluv;' >dist/browser-entry.js
echo 'import {Hsluv} from "./hsluv.mjs";window.Hsluv = Hsluv;' >dist/browser-entry.js
npx esbuild dist/browser-entry.js --bundle --minify --outfile="assets/hsluv-${PACKAGE_VERSION}.min.js"

# Sanity check hsluv.min.js window export
Expand All @@ -37,9 +37,20 @@ node dist/browser-test.js
TARBALL=$(cd assets && npm pack ../)

# Test that both commonjs and esm imports work
rm -rf test/tmp
mkdir test/tmp
echo "{}" >test/tmp/package.json
echo 'import {Hsluv} from "hsluv";' >test/tmp/test.ts
echo 'import {Hsluv} from "hsluv";' >test/tmp/test.mjs
echo 'const {Hsluv} = require("hsluv");' >test/tmp/test.cjs

(cd test/tmp && npm install "../../assets/${TARBALL}")
(cd test/tmp && node --input-type=module -e 'import {Hsluv} from "hsluv"; console.log("ESM OK")')
(cd test/tmp && node --input-type=commonjs -e 'const {Hsluv} = require("hsluv"); console.log("CommonJS OK")')
node test/tmp/test.mjs
echo "ESM import OK"
node test/tmp/test.cjs
echo "CommonJS require OK"

# Test that TypeScript can discover types with various configurations
# Discussion: https://github.com/hsluv/hsluv-javascript/pull/4
for m in "node10" "node16" "bundler"; do
(cd test/tmp && npx tsc --strict true --module es2020 --moduleResolution "$m" test.ts)
echo "tsc (--moduleResolution $m) OK"
done
Loading

0 comments on commit 8d4c6d9

Please sign in to comment.