Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
337 lines (282 loc) · 11.4 KB
/
Copy pathMakefile
File metadata and controls
337 lines (282 loc) · 11.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# This Makefile is meant to be exported by Patternslib or Mockup packages.
# Directory of this Makefile - the @patternslib/dev package directory.
# Must be set before any other include.
# Use the real path, as pnpm symlinks node_modules/@patternslib/dev into
# node_modules/.pnpm/, where its dependencies are located.
DEV_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
# .evn include, mainly for a GITHUB_TOKEN.
-include .env
export
# If you want to release on GitHub, make sure to have a .env file with a GITHUB_TOKEN.
# Also see:
# https://github.com/settings/tokens
# and https://github.com/release-it/release-it/blob/master/docs/github-releases.md#automated
ESLINT ?= npx eslint
PRETTIER ?= npx prettier
PNPM ?= npx pnpm
BUILDABLE := $(shell node -p "Boolean(require('./package.json').scripts?.build)")
PACKAGE_NAME := $(shell node -p "require('./package.json').name")
BUNDLE_NAME := $(subst @patternslib/,,$(subst @plone/,,$(PACKAGE_NAME)))
# Webpack config for the bundle analysis targets.
# Patternslib keeps it in webpack/webpack.config.js, most add-ons in the
# project root. If webpack/webpack.config.js doesn't exist, this is empty and
# no --config is passed. webpack-cli then uses its own discovery, which finds
# webpack.config.{js,mjs,cjs,ts,...} in the root or in .webpack/.
WEBPACK_CONFIG ?= $(wildcard webpack/webpack.config.js)
# Resolve webpack-cli relative to @patternslib/dev. webpack is a dependency of
# @patternslib/dev and pnpm doesn't link binaries of indirect dependencies
# into the consuming project's node_modules/.bin.
WEBPACK_CLI = node $(shell node -p "require.resolve('webpack-cli/bin/cli.js', {paths: ['$(DEV_DIR)']})")
##
## User targets
#
# Repeat this install target in consuming packages' Makefiles: on a fresh
# checkout, @patternslib/dev and this shared Makefile are not installed yet.
.PHONY: install
pnpm-lock.yaml install: .git/hooks/commit-msg
$(PNPM) install
# Update dependencies
# Update pnpm version and project dependencies individually and create 3 separate commits.
# Don't fail if nothing needs a commit.
.PHONY: update
update:
@if ! git diff --quiet -- package.json || ! git diff --cached --quiet -- package.json; then \
echo "Warning: package.json has uncommitted changes. Commit or stash them before running make update." >&2; \
exit 1; \
fi
@if ! git checkout -b dependencies-update; then \
echo ""; \
echo "Warning: the branch dependencies-update already exists. If it was already merged, you might want to delete if locally with \`git branch -D dependencies-update\`." >&2; \
echo ""; \
exit 1; \
fi
$(PNPM) self-update
-git commit package.json -m"maint: Update pnpm."
$(PNPM) update --latest --interactive
-git commit package.json -m"maint: Update dependencies."
-git commit pnpm-lock.yaml -m"pnpm-lock.yaml: Update." -n
@# Run the upgrades of a possibly updated @patternslib/dev.
@# A sub-make reads its newly installed Makefile, while this make still
@# uses the old one.
$(MAKE) upgrade
.PHONY: clean-dist
clean-dist:
rm -Rf dist/
.PHONY: clean
clean: clean-dist
rm -Rf node_modules/ stats.json stats.html
.PHONY: lint eslint
lint: eslint
eslint: install
$(ESLINT) ./src
.PHONY: format prettier
format: prettier
prettier: install
$(PRETTIER) --write .
# NOTE: Needs a package.json script. Not implemented here.
.PHONY: test check
test: check
check: install eslint
$(PNPM) run test
# NOTE: Needs a package.json script. Not implemented here.
.PHONY: start serve
start: serve
serve: install
$(PNPM) run start
# NOTE: Needs a package.json script. Not implemented here.
.PHONY: watch
watch: install
$(PNPM) run watch
# Compile the bundle.
# NOTE: Needs a package.json script. Not implemented here.
# NOTE: When using the normal workflow - e.g. `make release-minor`, the
# relase-it config runs `make bundle` after the version bump.
.PHONY: build bundle
build: bundle
bundle: clean-dist bundle-pre install
ifeq ($(BUILDABLE),true)
$(PNPM) run build
endif
.PHONY: release-major
release-major:
$(MAKE) LEVEL=major release
.PHONY: release-minor
release-minor:
$(MAKE) LEVEL=minor release
.PHONY: release-patch
release-patch:
$(MAKE) LEVEL=patch release
.PHONY: prerelease-alpha
prerelease-alpha:
$(MAKE) LEVEL=alpha release
.PHONY: prerelease-beta
prerelease-beta:
$(MAKE) LEVEL=beta release
##
## Analyze
#
# Build stats.json. Rebuilt when pnpm-lock.yaml changes.
# Write to a temporary file first, so that a failed build doesn't leave a
# broken but up-to-date stats.json behind.
# With --json, webpack reports compilation errors only within the JSON output.
# On failure, print them before removing the temporary file.
# Packages without a build script (like @patternslib/dev itself) have no
# bundle to analyze. Stop early with a clear message instead of failing
# with a webpack configuration error.
stats.json: pnpm-lock.yaml
ifneq ($(BUILDABLE),true)
@echo "$(PACKAGE_NAME) has no build script in package.json - nothing to analyze." >&2
@exit 1
endif
NODE_ENV=production $(WEBPACK_CLI) $(if $(WEBPACK_CONFIG),--config $(WEBPACK_CONFIG)) --json > $@.tmp \
|| { \
node -e "try { for (const e of JSON.parse(require('fs').readFileSync('$@.tmp')).errors || []) console.error(e.message || e) } catch {}"; \
rm -f $@.tmp; \
exit 1; \
}
mv $@.tmp $@
# Use `pnpm dlx` instead of `npx`: npx might find a package in the dependency
# tree without a linked binary and then fail with "not found".
# https://github.com/d4rkr00t/whybundled
.PHONY: analyze-bundle-dependencies
analyze-bundle-dependencies: stats.json
$(PNPM) dlx whybundled stats.json
# https://github.com/webpack/webpack-bundle-analyzer
.PHONY: analyze-bundle-treemap
analyze-bundle-treemap: stats.json
$(PNPM) dlx webpack-bundle-analyzer stats.json
# https://github.com/jonamat/webpack-visualizer
# Maintained fork of https://github.com/chrisbateman/webpack-visualizer
# The webpack plugin isn't used. Only its prebuilt frontend is downloaded,
# which renders the existing stats.json as stats.html.
VISUALIZER_PACKAGE ?= webpack-visualizer-plugin2@2
VISUALIZER_DIR := node_modules/.cache/webpack-visualizer
$(VISUALIZER_DIR)/package/lib/main.js:
mkdir -p $(VISUALIZER_DIR)
cd $(VISUALIZER_DIR) && tar -xzf "$$(npm pack --silent $(VISUALIZER_PACKAGE))"
stats.html: stats.json $(VISUALIZER_DIR)/package/lib/main.js
node $(DEV_DIR)webpack/stats-sunburst.js $(VISUALIZER_DIR)/package stats.json $@
# Command to open a file in the default browser. Override with e.g.
# `make analyze-bundle-sunburst OPEN=firefox`.
OPEN ?= $(if $(filter Darwin,$(shell uname -s)),open,xdg-open)
.PHONY: analyze-bundle-sunburst
analyze-bundle-sunburst: stats.html
$(OPEN) stats.html || echo "Open stats.html in your browser."
##
## Helper targets
#
.git/hooks/commit-msg:
echo "npx commitlint --edit" > .git/hooks/commit-msg
chmod u+x .git/hooks/commit-msg
.PHONY: bundle-pre
bundle-pre::
@# Override this in your project to add some tasks before the bundle is built.
@# Please use double-colons `::` so that the base bundle-pre is also run.
@# Example: Unlink any linked dependencies.
@# bundle-pre::
@# -pnpm unlink --recursive
@# pnpm install
npx update-browserslist-db@latest
# Create a ZIP file from the bundle which is uploaded to the GitHub release tag.
# NOTE: When using the normal workflow - e.g. `make release-minor`, the
# relase-it config runs `make release-zip` after the version bump and `make bundle`.
.PHONY: release-zip
release-zip:
ifeq ($(BUILDABLE),true)
$(eval PACKAGE_VERSION := $(shell node -p "require('./package.json').version"))
@echo Creating $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION).zip
mkdir -p $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION)
cp -R dist/. $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION)
zip -r $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION).zip $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION)/
rm -Rf $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION)
endif
# Prepare some necessary variables.
.PHONY: prepare-release
prepare-release:
$(eval RELEASE_IT_GITHUB_OPTIONS :=)
ifeq ($(LEVEL),$(filter $(LEVEL), alpha beta))
@# case alpha or beta pre-release
@# Set level argument for release-it.
$(eval RELEASE_IT_LEVEL := "--preRelease=$(LEVEL)")
else
@# case normal major/minor/patch release
@# Set level argument for release-it.
$(eval RELEASE_IT_LEVEL := $(LEVEL))
endif
# Note release-zip is run via the `after:bump` hook from .release-it.js, so
# that the build contains the correct version bump.
.PHONY: release
release: clean install check prepare-release
@# Note: The consuming package must include dist/ in its package.json files list.
@# Get the version to be released.
@# `--release-version` implies CI mode, where release-it cannot do the
@# interactive npm login and would fail with no output if not logged in.
@# Skip the npm checks here - they are done in the real release run below.
$(eval PACKAGE_VERSION := $(shell npx release-it $(RELEASE_IT_LEVEL) --release-version --npm.skipChecks))
$(if $(PACKAGE_VERSION),,$(error Could not determine the release version via release-it))
ifeq ($(BUILDABLE),true)
$(eval RELEASE_IT_GITHUB_OPTIONS += --github.assets=$(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION).zip)
endif
npx release-it \
$(RELEASE_IT_LEVEL) \
--github.release \
--no-github.update \
--no-github.draft \
$(RELEASE_IT_GITHUB_OPTIONS)
@# Remove the bundle from release-zip again.
@# But don't break if it doesn't exist.
-rm $(BUNDLE_NAME)-bundle-$(PACKAGE_VERSION).zip
##
## Upgrades
#
upgrade:\
.git/hooks/commit-msg\
upgrade-remove-husky\
upgrade-eslint\
upgrade-remove-build-stats\
upgrade-gitignore-stats
@# Upgrade target, depends on other upgrades
# The following upgrades only touch files without uncommitted changes, so
# that they never commit unrelated work. They run again on the next upgrade.
# The build:stats script is replaced by the stats.json target.
upgrade-remove-build-stats:
@if node -e "process.exit(require('./package.json').scripts?.['build:stats'] ? 0 : 1)"; then \
if git diff --quiet HEAD -- package.json 2>/dev/null; then \
npm pkg delete "scripts.build:stats" \
&& git commit package.json -m"maint: @patternslib/dev upgrade - remove build:stats script in favor of make stats.json."; \
else \
echo "Skipping removal of the build:stats script: package.json has uncommitted changes." >&2; \
fi; \
fi
# Ignore the generated bundle analysis files.
upgrade-gitignore-stats:
@if [ -f .gitignore ] && ! { grep -qxF stats.json .gitignore && grep -qxF stats.html .gitignore; }; then \
if git diff --quiet HEAD -- .gitignore 2>/dev/null; then \
[ -z "$$(tail -c1 .gitignore)" ] || echo >> .gitignore; \
for f in stats.json stats.html; do \
grep -qxF $$f .gitignore || echo $$f >> .gitignore; \
done \
&& git commit .gitignore -m"maint: @patternslib/dev upgrade - ignore stats.json and stats.html."; \
else \
echo "Skipping the stats.json/stats.html .gitignore update: .gitignore has uncommitted changes." >&2; \
fi; \
fi
upgrade-remove-husky:
test -d .husky\
&& rm -R .husky\
&& git add .husky\
&& git commit .husky -m"maint: @patternslib/dev upgrade - remove .husky directory in favor of git hooks."\
|| :
-git config --unset core.hooksPath
eslint.config.js upgrade-eslint:
test -f "eslint.config.js"\
|| (\
echo 'module.exports = require("@patternslib/dev/eslint.config.js");' > eslint.config.js\
&& git add eslint.config.js\
&& git commit eslint.config.js -m"maint: @patternslib/dev upgrade - create eslint.config.js."\
)
test -f ".eslintrc.js"\
&& rm .eslintrc.js\
&& git add .eslintrc.js\
&& git commit .eslintrc.js -m"maint: @patternslib/dev upgrade - remove old .eslintrc.js."\
|| :