Skip to content
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

Merged
merged 10 commits into from
Aug 26, 2018
Merged

Add prepublish script #942

merged 10 commits into from
Aug 26, 2018

Conversation

birjj
Copy link
Contributor

@birjj birjj commented Aug 25, 2018

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:

  1. Compile 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)
  2. Compile each icon into its own .js file, located in icons/{filename}.js. This can be imported directly, as importing index.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:

simple-icons
├ icons
│ ├ 500px.svg
│ ├ 500px.js
│ ├ about-dot-me.svg
│ ├ about-dot-me.js
│ └ …
├ …
└ index.js

which can be used by doing:

// not tree-shakeable, but now browser usable
const googleplus = require("simple-icons")["Google+"];
// tree-shakeable, also browser usable
const googleplus = require("simple-icons/icons/googleplus");

As the API has not changed, this is not a breaking change.

Closes #941
Closes #751

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
@birjj birjj added the meta Issues or pull requests regarding the project or repository itself label Aug 25, 2018
const indexFile = `${__dirname}/../index.js`;
const iconsDir = `${__dirname}/../icons`;
const data = require(dataFile);
const fs = require('fs');
Copy link
Contributor

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, '')
Copy link
Contributor

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.
Copy link
Contributor

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"
Copy link
Contributor

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?

// write the static .js file for the icon
fs.writeFileSync(
`${iconsDir}/${filename}.js`,
`module.exports = ${JSON.stringify(i)};`
Copy link
Contributor

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?

});

// write our generic index.js
fs.writeFileSync(indexFile, `module.exports = ${JSON.stringify(icons)};`);
Copy link
Contributor

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?

const { titleToFilename } = require("./utils");

const icons = {};
data.icons.forEach(i => {
Copy link
Contributor

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

@birjj birjj merged commit 6ee6ba9 into simple-icons:develop Aug 26, 2018
@birjj
Copy link
Contributor Author

birjj commented Aug 26, 2018

First version with this is now out, and I have confirmed that it works in both node and webpack 😄

@ericcornelissen ericcornelissen added the package Pull requests or issues that affect the NPM package label Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meta Issues or pull requests regarding the project or repository itself package Pull requests or issues that affect the NPM package
Projects
None yet
2 participants