-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbabel.sh
executable file
·202 lines (177 loc) · 5.87 KB
/
babel.sh
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
#!/bin/bash
#
# https://github.com/hilbix/tino/blob/master/howto/debian/babel.md
#
# WTF? Is the complete planet off the trolley?
#
# - Babel uses 'require' instead of just including the neccessary code.
# - And all bundlers capable of expanding require() create a module out of top level code.
#
# In combination this is a complete big ugly fail. What we need:
#
# Some script is written in ES99999999999999999999999999999999999999999999999999999999999999999999999999999999
# running fine in Firefox/Chrome version 9999999999999999999999999999999999999999999999999999999999999999999.0 but not below.
# The script is not a module, not something else or anything. Just a normal plain top level window-namespace polluting script. But working!
# And it should be transpiled to ES6 to run on old browsers. WITH NO CHANGES WHATSOEVER TO OTHER COMPONENTS EXCEPT THE SCRIPT!
#
# Hence no modules. No html changes. No nothing not no no nononononooooooooo!
#
# COMPLETE FAIL:
#
# - Most Transpilers are so old (read: 1 feature behind), such they are not capable to properly tranform working code into ES6.
# - The only one working seems to be Babel, but Babel uses require() to include missing parts. WTF?
# - For require() you need bundlers, but those always transform the input into modules. WTF WHY?
# - Hence the result will be unusable, as when the original was no module the result must not be a module either.
#
# Solution:
#
# - Re-invent the wheel by providing my own 'require' expansion wraper.
# - Stay with the fact that we do not use nor occupy 'require' at the top level.
#
# What a shame!
#
# This Works is placed under the terms of the Copyright Less License,
# see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
PRESETS=@babel/preset-env
PLUGINS=@babel/plugin-proposal-optional-chaining,@babel/plugin-proposal-class-properties,@babel/plugin-transform-runtime
BABEL=(babeljs --source-type=script --presets "$PRESETS" --plugins "$PLUGINS" "$1")
SCRIPTSRC="${2:-"$1"}"
STDOUT() { local e=$?; [ 0 = $# ] || printf '%q' "$1"; [ 1 -lt $# ] && printf ' %q' "${@:2}"; printf '\n'; return $e; }
STDERR() { local e=$?; STDOUT "$@" >&2; return $e; }
OOPS() { STDERR OOPS: "$@"; exit 23; }
DEBUG() { local e=$?; return $e; }
x() { DEBUG exec: "$@"; "$@"; DEBUG rc=$?: "$@"; }
o() { x "$@" || OOPS fail: "$@"; }
v() { local -n __v="$1"; local __e; __v="$("${@:2}" && echo x)"; __e=$?; __v="${__v%%x}"; __v="${__v%$'\n'}"; return $__e; }
ov() { o v "$@"; }
xv() { x v "$@"; }
OUT() { "${@:2}" >"$1"; }
ov TMP mktemp
trap 'rm -f "$TMP"' 0
STDERR
o OUT "$TMP" "${BABEL[@]}"
STDERR
STDERR feel free to ignore: caniuse-lite is outdated
STDERR
ov BASE readlink -e .
INCPATH=("$BASE/" /usr/lib/nodejs/ /usr/share/nodejs/)
detect()
{
(
o cd "$1"
case "$2" in
(./*|../*) LIST=('');;
(*) LIST=("${INCPATH[@]}");;
esac
for a in "${LIST[@]}"
do
b="$a$2"
[ -f "$b/package.json" ] &&
xv p python3 -c 'import json; import sys; print(json.load(open(sys.argv[1]))["main"])' "$b/package.json" &&
o readlink -e "$b" && o readlink -e "$b/$p" && exit
[ -f "$b.js" ] && o readlink -m "$b" && o readlink -e "$b.js" && exit
[ -f "$b/index.js" ] && o readlink -e "$b" && o readlink -e "$b/index.js" && exit
done
exit 1
)
}
require()
{
local d f p
ov d readlink -e -- "$1"
ov f readlink -e -- "$2"
while read -ru6 line
do
ov pf detect "$d" "$line"
p="${pf%%$'\n'*}" # 1st line ist the "require" arg
f="${pf##*$'\n'}" # 2nd line is the real file
for a in "${INCPATH[@]}" # remove common prefix from "require" arg
do
p="${p#"$a"}" # only the first matching prefix is removed as $a looks like /path/
done
[ -z "${REQ["$p"]}" ] || continue # include already known
REQ["$p"]="$f"
o require "${f%/*}" "$f" 6<&-
done 6< <(o sed -n "s/^.*\<require(['\"]\([^'\"]*\)['\"]).*$/\1/p" "$f")
}
bundle()
{
out // DEBUG "<script src=\"$SCRIPTSRC\" data-debug></script>"
o cat <<EOF
require = (function(orig,cs, _)
{
var r = [];
var debug = cs && cs.data && cs.data.debug;
function require(x, p)
{
// relative paths
if (x.substr(0,2)=='./' || x.substr(0,3)=='../')
x = p + '/' + x;
// canonicalize path
x = x.split('/');
p = [];
while (x.length)
{
var t = x.shift();
if (t=='' || t=='.') continue;
if (t=='..') p.pop(); else p.push(t);
}
x = p.join('/');
// if not bundled, forward to others
if (!_[x])
{
if (debug)
console.error('require(orig):', x);
if (orig)
return orig(x);
throw 'require(): not found: '+x;
}
// include CommonJS module
var m = { id:x, exports:{} };
p = _[x][0];
_[x][1](m, function(x) { return require(x, p) });
if (debug)
console.error('require(local):', x, p, m.exports);
return m.exports;
}
return function(x) { return r[x] || (r[x]=require(x, '')) };
})(this.window && window.require, this.document && document.currentScript,
EOF
first='{'
for a in "${!REQ[@]}"
do
p="${REQ["$a"]%"$a"*}"
p="${REQ["$a"]#"$p"}"
o printf '%s "%s":["%s", function(module,require) { // %s\n' "$first" "$a" "${p%/*}" "$p"
o cat "${REQ["$a"]}"
o printf '\n}]\n'
first=,
done
printf '}'
o cat <<EOF
);
EOF
}
out()
{
printf '%s' "$1"
[ 1 -ge $# ] || printf ' %-7s' "$2"
[ 2 -ge $# ] || printf ' %s' "${@:3}"
printf '\n'
}
out "'use strict';"
out // Bundled by:
out // script $(git ls-files --full-name "$0")
out // GIT $(git config --get remote.origin.url)
out // branch $(git rev-parse --abbrev-ref HEAD)
#out // SHA $(git rev-parse HEAD) # disabled. We want stable output, so use src instead
out // src $(sha256sum -- "$1")
declare -A REQ
o require . "$TMP"
for a in "${!REQ[@]}"
do
o bundle
break
done
out // BABEL "${BABEL[@]}"
o cat "$TMP"