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 icons option to build script #794

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Assembly.buildUserAssets('path/to/my/outdir', myOptions)
- **`mediaQueries`**: An object whose properties will override and add to `src/mediaQueries.json`. Use this option to change or add media queries.
These media queries are accessible in any stylesheets you append via the CSS custom media query syntax, e.g. `@media --media-query-name`.
- **`colorVariants`**: An object or array specifying the color variants you would like added to `assembly.css`. This is documented in detail below.
- **`icons`**: An array of icons names to include in Assembly. Names correspond to file names in `src/svgs/`. Use this option to decrease the size of assembly.js by only including the icons you need.
- **`quiet`**: Suppress logs.

### `colorVariants` option
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"execall": "^1.0.0",
"gzip-size": "^3.0.0",
"highlight.js": "^9.8.0",
"jest": "^19.0.0",
"jest": "^20.0.0",
"lodash": "^4.17.2",
"mime": "^1.3.4",
"nodemon": "^1.11.0",
Expand Down
4 changes: 3 additions & 1 deletion scripts/build-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const jsFiles = [
* @param {Object} [options]
* @param {string} [options.outfile] - Path to which built JS should be written.
* @param {Object} [options.quiet] - Suppress logs.
* @param {Array<string>} [options.icons] - Array of icon names to include in the
* loader. icon names correspond to SVG file names excluding the `.svg` suffix.
* @return {Promise<void>}
*/
function buildJs(options) {
Expand All @@ -32,7 +34,7 @@ function buildJs(options) {
const outfile = options.outfile || path.join(__dirname, '../dist/assembly.js');

return Promise.all([
buildSvgLoader(),
buildSvgLoader(options.icons || []),
concatJs()
])
.then((data) => {
Expand Down
26 changes: 24 additions & 2 deletions scripts/build-svg-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,33 @@ function processSvgFile(filename) {
});
}

function buildSvgLoader() {
/**
* Build Svg loader, which includes SVG data for all icons.
*
* @param {Array<string>} [icons] - Array of icon names to include in the
* loader. Icon names correspond to SVG file names excluding the `.svg` suffix.
*/
function buildSvgLoader(icons) {
const sprite = svgstore();

return pify(fs.readdir)(svgDir)
.then((filenames) => {
return Promise.all(filenames.map((filename) => {

// Error if user tries to include icons that don't exist
icons.forEach((svg) => {
if (!filenames.includes(svg + '.svg')) {
throw new Error(`an icon matching ${svg} does not exist`);
}
});

const files = (icons.length !== 0)
? filenames.filter((f) => icons.includes(f.split('.svg')[0]))
: filenames;

return Promise.all(files.map((filename) => {
return processSvgFile(path.join(svgDir, filename), sprite);
}));

})
.then(() => {
// This sorting is necessary to get a detemrinistic
Expand All @@ -94,3 +113,6 @@ function buildSvgLoader() {
}

module.exports = buildSvgLoader;
if (require.main === module) {
buildSvgLoader().catch((err) => console.error(err.stack));
}
1 change: 1 addition & 0 deletions scripts/build-user-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function buildUserAssets(outdir, options) {

const buildJsOptions = {
outfile: path.join(outdir, 'assembly.js'),
icons: options.icons || false,
quiet: options.quiet || false
};

Expand Down
6 changes: 6 additions & 0 deletions test/__snapshots__/build-js.jest.js.snap

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions test/build-js.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,25 @@ describe('buildJs', () => {
})
.then(() => cleanup(tmp));
});

test('with valid svg option', () => {
const tmp = `${getTmp()}/test.js`;
return buildJs({
outfile: tmp,
icons: ['airplane', 'alert']
})
.then(() => pify(fs.readFile)(tmp, 'utf8'))
.then((js) => {
expect(js).toMatchSnapshot();
})
.then(() => cleanup(tmp));
});

test('fails with invalid icon option', () => {
const tmp = `${getTmp()}/test.js`;
return expect(buildJs({
outfile: tmp,
icons: ['airplane', 'pizza']
})).rejects.toEqual(Error('an icon matching pizza does not exist'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know about this rejects thing!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, I didn't either. Had trouble testing a rejecting promise case, so ended up looking into it a little and came across this. It's new as of Jest 0.20.0.

});
});
9 changes: 6 additions & 3 deletions test/build-user-assets.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe('buildUserAssets', () => {
expect(buildJs).toHaveBeenCalledTimes(1);
expect(buildJs).toHaveBeenCalledWith({
outfile: '/path/to/outdir/assembly.js',
quiet: false
quiet: false,
icons: false
});
expect(copyFonts).toHaveBeenCalledTimes(1);
expect(copyFonts).toHaveBeenCalledWith({
Expand Down Expand Up @@ -51,7 +52,8 @@ describe('buildUserAssets', () => {
colorVariants: {
'range': ['blue-faint']
},
quiet: true
quiet: true,
icons: ['airplane']
};

return buildUserAssets('/path/to/another/outdir', options)
Expand All @@ -63,7 +65,8 @@ describe('buildUserAssets', () => {
expect(buildJs).toHaveBeenCalledTimes(1);
expect(buildJs).toHaveBeenCalledWith({
outfile: '/path/to/another/outdir/assembly.js',
quiet: true
quiet: true,
icons: ['airplane']
});
expect(copyFonts).toHaveBeenCalledTimes(1);
expect(copyFonts).toHaveBeenCalledWith({
Expand Down
Loading