-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add prepublish script #942
Conversation
Compiles icons into both static js files (icons/{filename}.js) and into a static index.js. The latter is not tree-shakeable. Closes simple-icons#941, closes simple-icons#751
scripts/prepublish.js
Outdated
const indexFile = `${__dirname}/../index.js`; | ||
const iconsDir = `${__dirname}/../icons`; | ||
const data = require(dataFile); | ||
const fs = require('fs'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but other then this you're always using double quotes
scripts/utils.js
Outdated
.replace(/^\./, "dot-") | ||
.replace(/\.$/, "-dot") | ||
.replace(/\./g, "-dot-") | ||
.replace(/[ !’]/g, '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, but other then this you're always using double quotes
README.md
Outdated
@@ -47,6 +47,23 @@ console.log(simpleIcons['Google+']); | |||
*/ | |||
``` | |||
|
|||
Alternatively you can import the needed icons individually. | |||
This is useful if you are e.g. compiling your code with webpack and therefore have to be mindful of your package size. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
webpack
could be a link, just like "JSDelivr" and "Unpkg"
package.json
Outdated
"svglint": "svglint icons/* --ci" | ||
"svglint": "svglint icons/* --ci", | ||
"prepublishOnly": "node scripts/prepublish.js", | ||
"postpublish": "rm icons/*.js && rm index.js" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe rm icons/*.js index.js
also works, why did you write it like this?
scripts/prepublish.js
Outdated
// write the static .js file for the icon | ||
fs.writeFileSync( | ||
`${iconsDir}/${filename}.js`, | ||
`module.exports = ${JSON.stringify(i)};` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change this to module.exports=${JSON.stringify(i)};
(removing the space) to reduce the output size?
scripts/prepublish.js
Outdated
}); | ||
|
||
// write our generic index.js | ||
fs.writeFileSync(indexFile, `module.exports = ${JSON.stringify(icons)};`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, change this to module.exports=${JSON.stringify(icons)};
(removing the space) to reduce the output size?
scripts/prepublish.js
Outdated
const { titleToFilename } = require("./utils"); | ||
|
||
const icons = {}; | ||
data.icons.forEach(i => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename the variable i
to icon
for readability
First version with this is now out, and I have confirmed that it works in both node and webpack 😄 |
Compiles icons into both static js files (icons/{filename}.js) and into a static index.js. The latter is not tree-shakeable.
This will run
scripts/prepublish.js
before the package is published to NPM. Said script will:index.js
- it will from now on only contain a static JS object, and can therefore be used in browsers (Webpack throwing errors when bundling Simple Icons in project #751, Make simple-icons npm package compatible with browsers #941).js
file, located inicons/{filename}.js
. This can be imported directly, as importingindex.js
puts the entire JS object into your compiled package. By importing the icons directly, only the used icons are included.The published project structure will be:
which can be used by doing:
As the API has not changed, this is not a breaking change.
Closes #941
Closes #751