forked from bazel-contrib/rules_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor_npm.sh
executable file
·285 lines (254 loc) · 7.47 KB
/
vendor_npm.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
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
#!/usr/bin/env bash
# Vendor in files from npm based on /third_party/npm/package.json & /third_party/npm/yarn.lock
set -eu -o pipefail
# -e: exits if a command fails
# -u: errors if an variable is referenced before being set
# -o pipefail: causes a pipeline to produce a failure return code if any command errors
# sedi makes `sed -i` work on both OSX & Linux
# See https://stackoverflow.com/questions/2320564/i-need-my-sed-i-command-for-in-place-editing-to-work-with-both-gnu-sed-and-bsd
sedi () {
case $(uname) in
Darwin*) sedi=('-i' '') ;;
*) sedi='-i' ;;
esac
sed "${sedi[@]}" "$@"
}
readonly RULES_NODEJS_DIR=$(cd $(dirname "$0")/..; pwd)
readonly THIRD_PARTY_DIR=${RULES_NODEJS_DIR}/third_party
readonly DATE=$(date)
readonly PACKAGE_JSON_SHA256=$(cat ${THIRD_PARTY_DIR}/package.json | openssl dgst -sha256)
readonly YARN_LOCK_SHA256=$(cat ${THIRD_PARTY_DIR}/yarn.lock | openssl dgst -sha256)
# runs and adds command run to CMDS for later user
capture_and_run() {
CMDS="${CMDS}# "$(echo "$@" | sed "s#${RULES_NODEJS_DIR}/##g")$'\n'
"$@"
}
# preps for vendoring a package
# args:
# $1 - package name
prep() {
CMDS=""
PKG=$1
SRC_DIR=${THIRD_PARTY_DIR}/node_modules/${PKG}
DST_DIR=${THIRD_PARTY_DIR}/npm/node_modules/${PKG}
printf "\n\nVendoring ${PKG} to ${DST_DIR}\n"
rm -rf ${DST_DIR}
mkdir -p ${DST_DIR}
}
# runs ncc
# args:
# $1 - entry point
# $2 - (optional) additional args
ncc() {
echo "- compiling ${PKG} with ncc"
capture_and_run ${THIRD_PARTY_DIR}/node_modules/.bin/ncc build ${SRC_DIR}/$1 ${2:-} -o ${DST_DIR}
}
# minifies with terser
# args:
# $1 - input file
# $1 - output file
minify() {
echo "- minifying ${PKG} with terser"
capture_and_run ${THIRD_PARTY_DIR}/node_modules/.bin/terser --compress --mangle --comments '/(^!|@license|@preserve|Copyright)/' -- ${DST_DIR}/$1 > ${DST_DIR}/$2
}
# copies a file
# args:
# $1 - input & output file
copy() {
echo "- copy $1"
capture_and_run cp -f ${SRC_DIR}/$1 ${DST_DIR}
}
# copies a file
# args:
# $1 - input file
# $2 - output file
copy_to() {
echo "- copy $1 to $2"
capture_and_run cp -f ${SRC_DIR}/$1 ${DST_DIR}/$2
}
# moves a file
# args:
# $1 - input file
# $2 - output file
rename() {
echo "- rename $1 to $2"
capture_and_run mv ${DST_DIR}/$1 ${DST_DIR}/$2
}
# create a BUILD file and adds the common header portions
# args:
# $1 - license file
build_file_header() {
echo """# Generated by /scripts/vendor_npm.sh
load(\"//internal/js_library:js_library.bzl\", \"js_library\")
package(default_visibility = [\"//visibility:public\"])
licenses([\"notice\"])
# Vendored on ${DATE} from npm with /scripts/vendor_npm.sh
exports_files([\"$1\"])
js_library(
name = \"$(basename ${PKG})\",
srcs = glob([\"**/*.js\"]),
package_name = \"${PKG}\",
)
""" > ${DST_DIR}/BUILD.bazel
}
# adds the common footer to the BUILD file
build_file_footer() {
echo """# shell commands:
${CMDS}
# package.json (SHA256 ${PACKAGE_JSON_SHA256}):
$(cat ${THIRD_PARTY_DIR}/package.json | awk '{print "# "$0}')
# yarn.lock (SHA256 ${YARN_LOCK_SHA256}):
$(cat ${THIRD_PARTY_DIR}/yarn.lock | awk '{print "# "$0}')
""" >> ${DST_DIR}/BUILD.bazel
}
(
cd ${THIRD_PARTY_DIR}
yarn install
##############################################################################
# Vendor browserify
##############################################################################
(
prep browserify
ncc bin/cmd.js
echo "- local mod: revert https://github.com/browserify/browserify/pull/1801"
capture_and_run sedi 's#parent.id !== self._mdeps.top.id#parent.id#' ${DST_DIR}/index.js
echo "- local mod: workaround https://github.com/zeit/ncc/issues/461"
capture_and_run sedi "s#require('process/browser.js')#require('./browser1')#" ${DST_DIR}/main.js
rename index.js index.debug.js
minify index.debug.js index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
# don't include index.debug.js to keep release smaller
srcs = glob([\"**/*.js\"], exclude=[\"index.debug.js\"]) + [
\"advanced.txt\",
\"BUILD.bazel\",
\"LICENSE\",
\"usage.txt\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor ieee754
# (required by browserify)
##############################################################################
(
prep ieee754
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor base64-js
# (required by browserify)
##############################################################################
(
prep base64-js
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor @babel/core
##############################################################################
(
prep @babel/core
ncc lib/index.js
rename index.js index.debug.js
minify index.debug.js index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
# don't include index.debug.js to keep release smaller
srcs = glob([\"**/*.js\"], exclude=[\"index.debug.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor babelify
##############################################################################
(
prep babelify
copy index.js
copy LICENSE
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor named-amd
##############################################################################
(
prep named-amd
copy_to named-amd.js index.js
copy LICENSE.md
build_file_header LICENSE.md
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE.md\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
##############################################################################
# Vendor babelify plugins
##############################################################################
readonly BROWSERIFY_PLUGINS=(
'@babel/plugin-transform-modules-commonjs'
)
for PLUGIN in "${BROWSERIFY_PLUGINS[@]}"; do
(
prep ${PLUGIN}
ncc lib/index.js "-e @babel/core"
echo "- copy babel LICENSE"
capture_and_run cp -f ${THIRD_PARTY_DIR}/node_modules/@babel/core/LICENSE ${DST_DIR}
build_file_header LICENSE
echo """filegroup(
name = \"package_contents\",
srcs = glob([\"**/*.js\"]) + [
\"BUILD.bazel\",
\"LICENSE\",
],
)
""" >> ${DST_DIR}/BUILD.bazel
build_file_footer
)
done
rm -rf node_modules
)