Skip to content

Commit

Permalink
Fix build (#1478)
Browse files Browse the repository at this point in the history
* add build script

* fix

* finalize script

* fix the script

* add minifier

* revert changes in yarn.lock
  • Loading branch information
kenns29 authored Jun 7, 2023
1 parent c2faf46 commit b3cdba2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions packages/react-vis/build-browser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
# Browserify doesn't seem to support defining custom entry point for modules in node_modules
# This build script replaces the entry point (the main field of package.json) of d3 libraries to the files
# defined in the unpkg field when building browser bundles.
# The replacement are reverted after the build finishes.
D3_LIB_PATHS=$(ls -d ../../node_modules/d3-*)

get_key_from_d3_path() {
local KEY=$(echo $1 | sed 's/..\/..\/node_modules\/d3-//')
local KEY=$(echo $KEY | sed 's/-/_/')
echo $KEY
}

for D3_LIB_PATH in $D3_LIB_PATHS
do
PACKAGE_JSON=$D3_LIB_PATH/package.json
UNPKG=$(jq -r '.unpkg' $PACKAGE_JSON)
if ! [ -z $UNPKG ] && [ $UNPKG != null ]
then
# save the main field
MAIN=$(jq -r '.main' $PACKAGE_JSON)
KEY=$(get_key_from_d3_path $D3_LIB_PATH)
declare var_$KEY=$MAIN

# modify the main field
MOD=$(jq --arg unpkg $UNPKG '.main = $unpkg' $PACKAGE_JSON)
cat <<< $MOD | jq . > $PACKAGE_JSON
fi
done

browserify src/index.js -t [ babelify --rootMode upward --global ] --standalone reactVis | uglifyjs > dist/dist.min.js

# set the main fields of package.json back to original
for D3_LIB_PATH in $D3_LIB_PATHS
do
PACKAGE_JSON=$D3_LIB_PATH/package.json
KEY=$(get_key_from_d3_path $D3_LIB_PATH)
MAIN="var_$KEY"
if ! [ -z "${!MAIN}" ]
then
MOD=$(jq --arg var "${!MAIN}" '.main = $var' $PACKAGE_JSON)
cat <<< $MOD | jq . > $PACKAGE_JSON
fi
done

2 changes: 1 addition & 1 deletion packages/react-vis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"docs": "./publish-docs.sh",
"clean": "rm -rf dist es bundle.* index.html && mkdir dist es",
"start": "(cd ../showcase && command -v yarn >/dev/null && yarn start)",
"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 {} +",
"build:browser": "./build-browser.sh",
"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",
"lint-styles": "stylelint src/styles/*.scss --syntax scss",
"test:windows": "babel-node --inspect ./tests/index.js",
Expand Down

0 comments on commit b3cdba2

Please sign in to comment.