#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

PKGS = parse5 parse5-parser-stream parse5-sax-parser \
       parse5-plain-text-conversion-stream parse5-html-rewriting-stream \
       parse5-htmlparser2-tree-adapter

%:
	dh $@

override_dh_auto_build:
	ln -s ../packages/parse5 node_modules/ || true
	# 1. ESM build (the upstream tsconfig)
	set -e; for d in $(PKGS); do \
		(cd packages/$$d && echo "building $$d (ESM)" && tsc -b); \
	done
	# 2. Extra CommonJS build for each sub-package, so consumers that still
	# use `require("parse5")` (e.g. node-jsdom 20) keep working. The CJS
	# entries in each sub-package's `exports` map are added by the quilt
	# patch package-json-cjs-exports.patch.
	set -e; for d in $(PKGS); do \
		(cd packages/$$d && echo "building $$d (CJS)" && \
		 tsc --ignoreConfig \
		     --module commonjs --moduleResolution bundler \
		     --target es2020 --esModuleInterop \
		     --resolveJsonModule --skipLibCheck --types node \
		     --rootDir lib --outDir dist/cjs lib/index.ts && \
		 echo '{"type":"commonjs"}' > dist/cjs/package.json); \
	done

override_dh_installdocs:
	dh_installdocs
	dh_nodejs_autodocs auto_dispatch
