Skip to content

Commit b3cdba2

Browse files
authored
Fix build (#1478)
* add build script * fix * finalize script * fix the script * add minifier * revert changes in yarn.lock
1 parent c2faf46 commit b3cdba2

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/react-vis/build-browser.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# Browserify doesn't seem to support defining custom entry point for modules in node_modules
3+
# This build script replaces the entry point (the main field of package.json) of d3 libraries to the files
4+
# defined in the unpkg field when building browser bundles.
5+
# The replacement are reverted after the build finishes.
6+
D3_LIB_PATHS=$(ls -d ../../node_modules/d3-*)
7+
8+
get_key_from_d3_path() {
9+
local KEY=$(echo $1 | sed 's/..\/..\/node_modules\/d3-//')
10+
local KEY=$(echo $KEY | sed 's/-/_/')
11+
echo $KEY
12+
}
13+
14+
for D3_LIB_PATH in $D3_LIB_PATHS
15+
do
16+
PACKAGE_JSON=$D3_LIB_PATH/package.json
17+
UNPKG=$(jq -r '.unpkg' $PACKAGE_JSON)
18+
if ! [ -z $UNPKG ] && [ $UNPKG != null ]
19+
then
20+
# save the main field
21+
MAIN=$(jq -r '.main' $PACKAGE_JSON)
22+
KEY=$(get_key_from_d3_path $D3_LIB_PATH)
23+
declare var_$KEY=$MAIN
24+
25+
# modify the main field
26+
MOD=$(jq --arg unpkg $UNPKG '.main = $unpkg' $PACKAGE_JSON)
27+
cat <<< $MOD | jq . > $PACKAGE_JSON
28+
fi
29+
done
30+
31+
browserify src/index.js -t [ babelify --rootMode upward --global ] --standalone reactVis | uglifyjs > dist/dist.min.js
32+
33+
# set the main fields of package.json back to original
34+
for D3_LIB_PATH in $D3_LIB_PATHS
35+
do
36+
PACKAGE_JSON=$D3_LIB_PATH/package.json
37+
KEY=$(get_key_from_d3_path $D3_LIB_PATH)
38+
MAIN="var_$KEY"
39+
if ! [ -z "${!MAIN}" ]
40+
then
41+
MOD=$(jq --arg var "${!MAIN}" '.main = $var' $PACKAGE_JSON)
42+
cat <<< $MOD | jq . > $PACKAGE_JSON
43+
fi
44+
done
45+

packages/react-vis/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"docs": "./publish-docs.sh",
2424
"clean": "rm -rf dist es bundle.* index.html && mkdir dist es",
2525
"start": "(cd ../showcase && command -v yarn >/dev/null && yarn start)",
26-
"build:browser": "browserify src/index.js -t [ babelify --rootMode upward --global ] --standalone reactVis | uglifyjs > dist/dist.min.js && find dist -maxdepth 1 -not \\( -name 'dist.min.js' -or -name 'style.css' -or -name 'main.scss' -or -name 'styles' -or -name 'dist' \\) -exec rm -rf {} +",
26+
"build:browser": "./build-browser.sh",
2727
"build": "yarn run clean && babel --root-mode upward src -d dist --copy-files && BABEL_ENV=es babel --root-mode upward src -d es --copy-files && node-sass src/main.scss dist/style.css --output-style compressed && yarn run build:browser",
2828
"lint-styles": "stylelint src/styles/*.scss --syntax scss",
2929
"test:windows": "babel-node --inspect ./tests/index.js",

0 commit comments

Comments
 (0)