Skip to content

Commit

Permalink
Add svgs option to build script
Browse files Browse the repository at this point in the history
options.svgs is an array of icon names. If this option is used, only icon names that match options.svgs will be included in Assembly. Closes #782
  • Loading branch information
samanpwbb committed Jun 10, 2017
1 parent 2402276 commit 6e4e2fd
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 418 deletions.
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'),
svgs: options.svgs || 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'));
});
});
Loading

0 comments on commit 6e4e2fd

Please sign in to comment.