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

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

# rollup-plugin-typescript (1.0.1) 默认 include 为 '*.ts+(|x)' / '**/*.ts+(|x)'，
# 该 extglob 写法在本构建环境的 micromatch 4.0.8 + picomatch 4.0.4 下不再匹配任何文件，
# 插件的 transform 因此不生效，rollup 直接解析 TypeScript 源码并报
# "Unexpected token (Note that you need plugins to import files that are not JavaScript)"。
# 这里显式传入等价的普通 glob include，恢复插件对 .ts/.tsx 的转译。
ROLLUP_TS_INCLUDE={"include":["**/*.ts","**/*.tsx"]}
ROLLUP_PLUGINS=-p @rollup/plugin-babel -p 'rollup-plugin-typescript=$(ROLLUP_TS_INCLUDE)' -p rollup-plugin-terser
ES_FILE=$(shell pkgjs-pjson . exports . import)
CJS_FILE=$(shell pkgjs-pjson . exports . require)
ADDON_ES=$(shell pkgjs-pjson . exports ./addons/use-notification-center import)
ADDON_CJS=$(shell pkgjs-pjson . exports ./addons/use-notification-center require)

%:
	dh $@

override_dh_auto_build:
	dh_auto_build --buildsystem=nodejs
	rollup $(ROLLUP_PLUGINS) --external react,clsx -f es -o $(ES_FILE) src/index.ts
	rollup $(ROLLUP_PLUGINS) --external react,clsx -f cjs -o $(CJS_FILE) src/index.ts
	tsc -d
	rollup $(ROLLUP_PLUGINS) --external react,clsx -f es -o $(ADDON_ES) src/addons/use-notification-center/index.ts
	rollup $(ROLLUP_PLUGINS) --external react,clsx -f cjs -o $(ADDON_CJS) src/addons/use-notification-center/index.ts
	ln -s .. node_modules/react-toastify
	tsc -p debian/addon-tsconfig.json --target ES5
	mv addons/addons/use-notification-center/*.d.ts addons/use-notification-center/
	rm -rf addons/addons node_modules/react-toastify
	node-sass scss/main.scss dist/ReactToastify.css
	node-sass scss/minimal.scss dist/ReactToastify.minimal.css
	postcss dist/ReactToastify.css --use autoprefixer -m -o dist/ReactToastify.css
	postcss dist/ReactToastify.minimal.css --use autoprefixer -m -o  dist/_ReactToastify.minimal.css
	yui-compressor dist/_ReactToastify.minimal.css > dist/ReactToastify.minimal.css
	rm -f dist/_ReactToastify.minimal.css dist/_ReactToastify.minimal.css.map
	yui-compressor dist/ReactToastify.css > dist/ReactToastify.min.css

override_dh_installdocs:
	dh_installdocs
	dh_nodejs_autodocs
