-
Notifications
You must be signed in to change notification settings - Fork 3
/
Makefile
81 lines (66 loc) · 3.32 KB
/
Makefile
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
.SUFFIXES:
SRCS := $(wildcard src/*) $(wildcard src/*/*)
TSSRCS := $(wildcard src/*.ts) $(wildcard src/*/*.ts)
OUTS := public/404.html public/index.html public/style.css public/script.js
HTMLFLAGS ?= --collapse-boolean-attributes --collapse-whitespace \
--decode-entities --remove-attribute-quotes --remove-comments \
--remove-empty-attributes --remove-optional-tags \
--remove-redundant-attributes --remove-script-type-attributes \
--remove-style-link-type-attributes --sort-attributes \
--sort-class-name --use-short-doctype --minify-css true --minify-js true
CSSFLAGS ?= -O2
WPFLAGS ?= --mode development
build/min/script.js: WPFLAGS := --mode production
ENTRY := src/main.ts
TSCSERVERFLAGS := --target es2017 --module CommonJS
TZ := America/Toronto
DATE = $(shell TZ='$(TZ)' date --date="$$(git log -1 --format='%cI')" +'%-e %B %Y')
YEAR = $(lastword $(DATE))
VERSION := $(shell sed -nE 's/^.*"version": "([^"]+)".*$$/\1/p' package.json)
AUTHOR := $(shell sed -nE 's/^.*"author": "([^"]+)".*$$/\1/p' package.json)
HOMEPAGE := $(shell sed -nE 's/^.*"homepage": "([^"]+)".*$$/\1/p' package.json)
HTMLINFOINS = '1i<!--\n Web Draw v$(VERSION) ($(HOMEPAGE))\n Copyright (C) 2020-$(YEAR) $(AUTHOR)\n Licensed under the GNU General Public License v3.0\n-->'
CSSINFOINS = '1i/*\n * Web Draw v$(VERSION) ($(HOMEPAGE))\n * Copyright (C) 2020-$(YEAR) $(AUTHOR)\n * Licensed under the GNU General Public License v3.0\n */'
JSINFOINS = '1i/*\n * Web Draw v$(VERSION) ($(HOMEPAGE))\n * Copyright (C) 2020-$(YEAR) $(AUTHOR)\n * Licensed under the GNU General Public License v3.0\n *\n * Using msgpack-lite (https://github.com/kawanet/msgpack-lite)\n * Copyright (C) 2015-2016 Yusuke Kawasaki\n * Licensed under the MIT License\n */'
BASE64SUB := 's/(.*)\{\{BASE64:([^}]+)\}\}(.*)/printf "%s%s%s" '\''\1'\'' "data:image\/png;base64,$$(base64 -w 0 < '\''\2'\'')" '\''\3'\''/e'
DATESUB = 's/(.*)\{\{DATE\}\}(.*)/\1$(DATE)\2/'
YEARSUB = 's/(.*)\{\{YEAR\}\}(.*)/\1$(YEAR)\2/'
VERSIONSUB = 's/(.*)\{\{VERSION\}\}(.*)/\1v$(VERSION)\2/'
SUBSTITUTE = cp $< $@
build/sub/index.html: SUBSTITUTE = sed -E -e $(BASE64SUB) -e $(DATESUB) -e $(YEARSUB) -e $(VERSIONSUB) $< > $@
build/sub/style.css build/sub/images.ts: SUBSTITUTE = sed -E $(BASE64SUB) $< > $@
.PHONY: all min clean
# Build output without minification
all: $(patsubst public/%,build/sub/%,$(OUTS))
cp -t public $^
# Build and minify output
min: $(patsubst public/%,build/min/%,$(OUTS))
cp -t public $^
# Compile server code
server.js: server.ts src/message.ts
npx tsc $(TSCSERVERFLAGS) $<
rm src/message.js
clean:
rm -rf build
rm -f $(OUTS)
rm -f server.js
# Perform substitutions
build/sub/%: src/%
@mkdir -p $(@D)
$(SUBSTITUTE)
.SECONDARY: $(patsubst src/%,build/sub/%,$(SRCS))
# Compile and bundle TypeScript
build/%/script.js: webpack.config.js tsconfig.json $(patsubst src/%.ts,build/sub/%.ts,$(TSSRCS))
@mkdir -p $(@D)
npx webpack $(WPFLAGS) --config $< --output-path ./$(@D) --output-filename $(@F) --entry ./$(patsubst src/%,build/sub/%,$(ENTRY))
sed -Ei $(JSINFOINS) $@
# Minified output
build/min/%.html: build/sub/%.html
@mkdir -p $(@D)
npx html-minifier $(HTMLFLAGS) -o $@ $<
sed -Ei $(HTMLINFOINS) $@
build/min/%.css: build/sub/%.css
@mkdir -p $(@D)
npx cleancss $(CSSFLAGS) -o $@ $<
sed -Ei $(CSSINFOINS) $@
# JavaScript minified by webpack invocation above in production mode (target build/min/script.js)