-
Notifications
You must be signed in to change notification settings - Fork 8
/
node.mk
171 lines (149 loc) · 5.4 KB
/
node.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
REPO_ROOT ?= $(shell git rev-parse --show-toplevel)
-include $(REPO_ROOT)/base.mk
ifneq ("$(wildcard $(REPO_ROOT)/pnpm-lock.yaml)","")
PM :=pnpm
EXEC:=pnpm exec
NPX :=pnpm dlx
else
PM :=npm
EXEC:=npx
NPX :=npx -y
endif
PKG_NAME?=$(shell jq -r '.name' package.json)
PKG_VER ?=$(shell jq -r '.version' package.json)
OUT_NAME?=$(shell jq -r '.name' package.json | tr -d '@' | tr '/' '-')
OSPM ?=$(shell for i in apk brew yum apt-get apt; do command -v $$i > /dev/null && break ; done; echo $$i)
# File Structure
# lib/index.js - ESM build
# dist/ - Bundle & Non-ESM build
# cjs/index.js
# system/index.js
# esm/index.js
info:
@printf $(COLOR_INFO) $(PKG_NAME):
@echo " version:" v$(PKG_VER)
@echo " npm :" $(PM) $(shell $(PM) -v)
@echo " OS :" `uname -mispr`
@echo " OSPM :" $(OSPM)
@printf $(COLOR_INFO) " bundle:"
@echo " Name :" $(OUT_NAME)
@echo " CJS :" $(WANT_CJS)
clean: distclean ## clean up built files and caches
rm -rf .next/* .turbo/* node_modules/.cache/*
distclean: ## clean up built files
rm -rf dist/* lib/*
ifneq ($(wildcard next.config.*),)
# Next.JS
build:
$(EXEC) next build
dev:
$(EXEC) next dev
else ifneq ($(wildcard vite.config.*),)
# Vite
build:
$(EXEC) vite build
dev:
$(EXEC) vite dev
else
# Library
WANT_CJS?=$(shell jq -r '.exports["."]|has("require")' package.json)
ESBUILD_DEVELOPMENT_FLAGS? =--define:process.env.NODE_ENV=\"development\" --define:__DEV__=true --minify-syntax --charset=utf8 --target=chrome90 --sourcemap
ESBUILD_PRODUCTION_FLAGS? =--define:process.env.NODE_ENV=\"production\" --define:__DEV__=false --minify --charset=utf8 --target=chrome90 --sourcemap
SOURCE_FILES?=$(shell ls 2>/dev/null src/**/*.js src/**/*.ts src/**/*.tsx | egrep -v '[.]test[.]tsx?')
# will not minify
ESBUILD_BUILD_FLAGS?=--charset=utf8 --target=chrome90 --sourcemap --platform=neutral
build:
ifneq ($(wildcard rollup.build.*),)
$(EXEC) rollup -c $(wildcard rollup.build.*)
else ifneq ($(wildcard rollup.dev.*),)
$(EXEC) rollup -c $(wildcard rollup.dev.*)
else ifneq ($(wildcard rollup.config.*),)
$(MAKE) rollup
else ifneq ($(wildcard esbuild.build.*),)
$(EXEC) $(wildcard esbuild.build.*)
else
@$(EXEC) esbuild --format=esm --outdir=lib $(SOURCE_FILES) $(ESBUILD_BUILD_FLAGS)
ifeq ($(WANT_CJS),true)
@$(EXEC) esbuild --format=cjs --outdir=dist/cjs/lib $(SOURCE_FILES) $(ESBUILD_BUILD_FLAGS)
endif
endif
build-declaration:
$(EXEC) tsc --outDir lib/esm --emitDeclarationOnly --declaration
libsum:
@printf $(COLOR_INFO) "lib size"
-@du --apparent-size -sh lib
@printf $(COLOR_INFO) "dist size"
-@du --apparent-size -sh dist/**/*.{cjs,mjs,js}
@printf $(COLOR_INFO) "dist bundle node_modules?"
-@grep '^// ' dist/esm/index.development.js | grep node_modules || echo none
@printf $(COLOR_INFO) "checksum"
-@tar --mtime="@0" --owner=0 --group=0 --numeric-owne --sort=name -cf - lib | sha256sum | sed 's/-/lib/'
-@tar --mtime="@0" --owner=0 --group=0 --numeric-owne --sort=name -cf - dist | sha256sum | sed 's/-/dist/'
ESBUILD_DEV_FLAGS?= \
--define:process.env.NODE_ENV=\"development\" --define:__DEV__=true --charset=utf8 --target=chrome90 --sourcemap=external
dev: ESBUILD_BUILD_FLAGS=$(ESBUILD_DEV_FLAGS)
dev: build
ifneq ($(wildcard rollup.dev.js),)
$(EXEC) rollup -c rollup.dev.js --watch
else
@$(EXEC) esbuild --format=esm --outdir=lib --watch $(SOURCE_FILES) $(ESBUILD_DEV_FLAGS)
endif
ESBUILD_BUNDLE_FLAGS?= \
--external:{react,react-dom,prop-types,classnames,@*,markdown-it,prosemirror*} \
--charset=utf8 --target=chrome90 --sourcemap=external --legal-comments=external --bundle
bundle:
ifneq ($(wildcard rollup.bundle.*),)
NODE_ENV=production $(EXEC) rollup -c $(wildcard rollup.bundle.*)
else ifneq ($(wildcard rollup.config.*),)
@printf $(COLOR_INFO) "rollup bundled in build phase"
else ifneq ($(wildcard esbuild.bundle.*),)
$(EXEC) $(wildcard esbuild.bundle.*)
else
@$(EXEC) esbuild --format=esm --outfile=dist/esm/$(OUT_NAME).development.js $(ESBUILD_DEVELOPMENT_FLAGS) $(ESBUILD_BUNDLE_FLAGS) src/index.ts
@$(EXEC) esbuild --format=esm --outfile=dist/esm/$(OUT_NAME).production.js $(ESBUILD_PRODUCTION_FLAGS) $(ESBUILD_BUNDLE_FLAGS) src/index.ts
@$(EXEC) esbuild --format=cjs --outfile=dist/cjs/index.js $(ESBUILD_DEVELOPMENT_FLAGS) $(ESBUILD_BUNDLE_FLAGS) src/index.ts
endif
ifneq ($(wildcard rollup.config.js),)
rollup:
$(EXEC) rollup -c
else ifneq ($(wildcard rollup.config.ts),)
rollup:
$(EXEC) rollup -c --configPlugin @rollup/plugin-typescript
endif
prepublish: distclean build bundle libsum
publish: prepublish
$(PM) publish --registry https://registry.npmjs.org --access public --no-git-checks
sync-mirror:
curl -sf -X PUT "https://registry-direct.npmmirror.com/$(PKG_NAME)/sync?sync_upstream=true" | jq
endif
fmt:
$(EXEC) prettier -w src package.json
fix: LINT_FIX=1
fix: lint
LINT_FIX=
lint:
@printf $(COLOR_INFO) "Linting..."
ifneq ($(wildcard next.config.*),)
$(EXEC) next lint $(if $(filter 1,$(LINT_FIX)),--fix)
else ifneq ($(wildcard .eslintrc.* $(REPO_ROOT)/.eslintrc.*),)
$(EXEC) eslint src $(if $(filter 1,$(LINT_FIX)),--fix)
else ifneq ($(wildcard tsconfig.json),)
$(EXEC) tsc --pretty --noEmit
else
@printf $(COLOR_WARN) "No lint setup"
endif
test:
@printf $(COLOR_INFO) "Testing..."
ifneq ($(wildcard jest.config.*),)
$(EXEC) jest
else ifneq ($(wildcard ava.config.*),)
$(EXEC) ava
else ifeq ($(shell jq 'has("ava")' package.json),true)
$(EXEC) ava
else
@printf $(COLOR_WARN) "No test setup"
endif
install:
@printf $(COLOR_INFO) "Installing..."
npm i -g $(PM)
$(PM) install