Skip to content

Commit

Permalink
Use webpack's NormalModuleReplacementPlugin to intercept imports for …
Browse files Browse the repository at this point in the history
…blueprint's icon package and replace it with our own slimmed down module instead, allowing us to not bundle 300+kb of SVG icons we don't actually use.

Ref: palantir/blueprint#2193 (comment)
  • Loading branch information
jumpinjackie committed Nov 7, 2019
1 parent 3c90a80 commit 16775cc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -92,6 +92,8 @@
"@types/react-measure": "2.0.5",
"@types/react-redux": "^7.1.4",
"@types/shortid": "0.0.29",
"babel-loader": "8.0.6",
"babel-plugin-transform-object-assign": "6.22.0",
"bestzip": "2.1.5",
"cache-loader": "^4.1.0",
"copyfiles": "2.1.1",
Expand Down
22 changes: 22 additions & 0 deletions stdassets/bp-icons.js
@@ -0,0 +1,22 @@
/**
* bp-icons.js
*
* A verbatim subset of the Blueprint.js SVG icon set that we actually use
*
* All usages of the BP icon set will be replaced with this via
* NormalModuleReplacementPlugin in the webpack config
*/

export const IconSvgPaths16 = {
"chevron-down": ["M12 5c-.28 0-.53.11-.71.29L8 8.59l-3.29-3.3a1.003 1.003 0 00-1.42 1.42l4 4c.18.18.43.29.71.29s.53-.11.71-.29l4-4A1.003 1.003 0 0012 5z"],
"chevron-up": ["M12.71 9.29l-4-4C8.53 5.11 8.28 5 8 5s-.53.11-.71.29l-4 4a1.003 1.003 0 001.42 1.42L8 7.41l3.29 3.29c.18.19.43.3.71.3a1.003 1.003 0 00.71-1.71z"],
"menu-open": ["M9.99 11.99h-9c-.55 0-1 .45-1 1s.45 1 1 1h9c.55 0 1-.45 1-1s-.45-1-1-1zm0-5h-9c-.55 0-1 .45-1 1s.45 1 1 1h9c.55 0 1-.45 1-1s-.45-1-1-1zm0-5h-9c-.55 0-1 .45-1 1s.45 1 1 1h9c.55 0 1-.45 1-1s-.45-1-1-1zm5.71 5.3l-2-2a1.003 1.003 0 00-1.71.71v4a1.003 1.003 0 001.71.71l2-2c.18-.18.29-.43.29-.71s-.11-.53-.29-.71z"],
"warning-sign": ["M15.84 13.5l.01-.01-7-12-.01.01c-.17-.3-.48-.5-.85-.5s-.67.2-.85.5l-.01-.01-7 12 .01.01c-.09.15-.15.31-.15.5 0 .55.45 1 1 1h14c.55 0 1-.45 1-1 0-.19-.06-.35-.15-.5zm-6.85-.51h-2v-2h2v2zm0-3h-2v-5h2v5z"],
}

export const IconSvgPaths20 = {
"chevron-down": ["M16 6c-.28 0-.53.11-.71.29L10 11.59l-5.29-5.3a1.003 1.003 0 00-1.42 1.42l6 6c.18.18.43.29.71.29s.53-.11.71-.29l6-6A1.003 1.003 0 0016 6z"],
"chevron-up": ["M16.71 12.29l-6-6C10.53 6.11 10.28 6 10 6s-.53.11-.71.29l-6 6a1.003 1.003 0 001.42 1.42L10 8.41l5.29 5.29c.18.19.43.3.71.3a1.003 1.003 0 00.71-1.71z"],
"menu-open": ["M12 9H1c-.55 0-1 .45-1 1s.45 1 1 1h11c.55 0 1-.45 1-1s-.45-1-1-1zm0 5H1c-.55 0-1 .45-1 1s.45 1 1 1h11c.55 0 1-.45 1-1s-.45-1-1-1zm0-10H1c-.55 0-1 .45-1 1s.45 1 1 1h11c.55 0 1-.45 1-1s-.45-1-1-1zm7.71 5.29l-3-3A1.003 1.003 0 0015 7v6a1.003 1.003 0 001.71.71l3-3c.18-.18.29-.43.29-.71 0-.28-.11-.53-.29-.71z"],
"warning-sign": ["M19.86 17.52l.01-.01-9-16-.01.01C10.69 1.21 10.37 1 10 1s-.69.21-.86.52l-.01-.01-9 16 .01.01c-.08.14-.14.3-.14.48 0 .55.45 1 1 1h18c.55 0 1-.45 1-1 0-.18-.06-.34-.14-.48zM11 17H9v-2h2v2zm0-3H9V6h2v8z"],
}
8 changes: 7 additions & 1 deletion webpack.config.js
Expand Up @@ -29,7 +29,13 @@ const basePlugins = [
// both options are optional
filename: process.env.DEBUG_BUILD === '1' ? '[name]-debug.css' : '[name].css',
chunkFilename: "[id].css"
})
}),
// Intercept the BP icon import with our own slimmed down version
// Ref: https://github.com/palantir/blueprint/issues/2193#issuecomment-453326234
new webpack.NormalModuleReplacementPlugin(
/.*\/generated\/iconSvgPaths.*/,
path.resolve(__dirname, 'stdassets/bp-icons.js'),
)
];

const devPlugins = [
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Expand Up @@ -1355,6 +1355,16 @@ babel-jest@^24.9.0:
chalk "^2.4.2"
slash "^2.0.0"

babel-loader@8.0.6:
version "8.0.6"
resolved "https://registry.yarnpkg.com/babel-loader/-/babel-loader-8.0.6.tgz#e33bdb6f362b03f4bb141a0c21ab87c501b70dfb"
integrity sha512-4BmWKtBOBm13uoUwd08UwjZlaw3O9GWf456R9j+5YykFZ6LUIjIKLc0zEZf+hauxPOJs96C8k6FvYD09vWzhYw==
dependencies:
find-cache-dir "^2.0.0"
loader-utils "^1.0.2"
mkdirp "^0.5.1"
pify "^4.0.1"

babel-messages@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-messages/-/babel-messages-6.23.0.tgz#f3cdf4703858035b2a2951c6ec5edf6c62f2630e"
Expand Down Expand Up @@ -1682,6 +1692,13 @@ babel-plugin-transform-for-of-as-array@^1.1.1:
resolved "https://registry.yarnpkg.com/babel-plugin-transform-for-of-as-array/-/babel-plugin-transform-for-of-as-array-1.1.1.tgz#f18fcbcbfa2b8caed1445c3153893d37439a6537"
integrity sha512-eE4hZJhOUKpX0q/X3adR8B4hLox+t8oe4ZqmhANUmv4cds07AbWt6O0rtFXK7PKFPPnW4nz/5mpbkPMkflyGeg==

babel-plugin-transform-object-assign@6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-assign/-/babel-plugin-transform-object-assign-6.22.0.tgz#f99d2f66f1a0b0d498e346c5359684740caa20ba"
integrity sha1-+Z0vZvGgsNSY40bFNZaEdAyqILo=
dependencies:
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
Expand Down Expand Up @@ -3718,7 +3735,7 @@ fill-range@^7.0.1:
dependencies:
to-regex-range "^5.0.1"

find-cache-dir@^2.1.0:
find-cache-dir@^2.0.0, find-cache-dir@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-2.1.0.tgz#8d0f94cd13fe43c6c7c261a0d86115ca918c05f7"
integrity sha512-Tq6PixE0w/VMFfCgbONnkiQIVol/JJL7nRMi20fqzA4NRs9AfeqMGeRdPi3wIhYkxjeBaWh2rxwapn5Tu3IqOQ==
Expand Down

0 comments on commit 16775cc

Please sign in to comment.