Skip to content

Commit 89a7061

Browse files
committed
feat(@bolt/website,@bolt/build-tools): add posibility to add external icons to be rendered by icon t
1 parent 12b3639 commit 89a7061

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

.boltrc.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ const siteConfig = require(path.join(baseBoltDir, '.boltrc'));
2020

2121
// Paths that are relative to `baseBoltDir` must now be relative to this directory (i.e. `__dirname`)
2222
const adjustRelativePath = thePath =>
23-
path.relative(__dirname, path.resolve(baseBoltDir, thePath));
23+
path.relative(__dirname, path.resolve(baseBoltDir, thePath));
2424

2525
// Gather directories for any/all image fixtures and consolidate for the image resizing task
26-
const imageFixtureDirs = globby.sync(path.join(__dirname, './packages/components/**/fixtures/**/*.{jpg,jpeg,png}')).map(file => path.dirname(file));
26+
const imageFixtureDirs = globby
27+
.sync(
28+
path.join(
29+
__dirname,
30+
'./packages/components/**/fixtures/**/*.{jpg,jpeg,png}',
31+
),
32+
)
33+
.map(file => path.dirname(file));
2734
const imageSets = [];
2835

29-
imageFixtureDirs.forEach((fixturePath) => {
36+
imageFixtureDirs.forEach(fixturePath => {
3037
imageSets.push({
3138
base: fixturePath,
3239
glob: '**',
@@ -37,6 +44,7 @@ imageFixtureDirs.forEach((fixturePath) => {
3744
module.exports = {
3845
wwwDir: adjustRelativePath(siteConfig.wwwDir),
3946
buildDir: adjustRelativePath(siteConfig.buildDir),
47+
iconDir: [],
4048
components: {
4149
global: globby
4250
.sync(path.join(__dirname, './packages/components/**/*/package.json'))

docs-site/.boltrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const config = {
1414
srcDir: './src/pages',
1515
buildDir: '../www/build',
1616
dataDir: '../www/build/data',
17+
iconDir: [],
1718
wwwDir: '../www',
1819
startPath: '/',
1920
plConfigFile: './config/config.yml',

packages/build-tools/tasks/icon-tasks.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ const rootDir = path.dirname(
140140
resolve.sync('@bolt/components-icons/package.json'),
141141
);
142142
const buildDir = path.join(rootDir, 'src/icons');
143+
const globPattern = '**/*.svg';
143144

144145
/**
145146
* Transpile Icons
@@ -151,7 +152,6 @@ async function transpileIcons(icons) {
151152

152153
return Promise.all(
153154
icons.map(async icon => {
154-
// icons.forEach(async (i) => {
155155
const svg = await fs.readFile(icon, 'utf-8');
156156
const id = path
157157
.basename(icon, '.svg')
@@ -263,6 +263,13 @@ async function transpileIcons(icons) {
263263
}
264264

265265
function alphabetizeIconList(a, b) {
266+
if (a.id === b.id) {
267+
const iconDir = a.icon.includes('@bolt/components-icons') ? b.icon : a.icon;
268+
269+
throw new Error(`SVG filenames must be unique but '${a.id}' is not.
270+
Please change filename in location: '${iconDir}'`);
271+
}
272+
266273
if (a.id < b.id) return -1;
267274
if (a.id > b.id) return 1;
268275
return 0;
@@ -271,7 +278,13 @@ function alphabetizeIconList(a, b) {
271278
async function build() {
272279
try {
273280
const config = await getConfig();
274-
const iconPaths = await globby(path.join(rootDir, 'src/svgs/**/*.svg'));
281+
282+
const extendedIconDirs = config.iconDir ? config.iconDir : [];
283+
284+
const dirs = [rootDir, ...extendedIconDirs];
285+
const allIcons = dirs.map(dir => path.join(dir, globPattern));
286+
287+
const iconPaths = await globby(allIcons);
275288

276289
iconSpinner = new Ora(
277290
chalk.blue(
@@ -281,6 +294,7 @@ async function build() {
281294

282295
await fs.remove(path.join(rootDir, 'src/icons')); // Clean folder
283296
await fs.outputFile(path.join(rootDir, 'src', 'index.js'), '', 'utf-8');
297+
284298
const icons = await transpileIcons(iconPaths);
285299
const allExports = icons
286300
.sort(alphabetizeIconList) // we alphabetize the list so multiple compiles on same set doesn't result in a change that git notices
@@ -356,14 +370,13 @@ async function watch() {
356370
const config = await getConfig();
357371

358372
// for now, only watch the main @bolt/components-icons folder for .svg file changes.
359-
// @todo: update to include extra folders specified in the .boltrc config
360-
const dirs = [rootDir];
373+
const extendedIconDirs = config.iconDir ? config.iconDir : [];
374+
const dirs = [rootDir, ...extendedIconDirs];
361375

362376
// Used by watches
363377
const debouncedCompile = debounce(build, config.debounceRate);
364378

365-
const globPattern = '**/*.svg';
366-
const watchedFiles = [dirs.map(dir => path.join(dir, globPattern))];
379+
const watchedFiles = dirs.map(dir => path.join(dir, globPattern));
367380

368381
// The watch event ~ same engine gulp uses https://www.npmjs.com/package/chokidar
369382
const watcher = chokidar.watch(watchedFiles, {

packages/build-tools/utils/config.schema.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
type: string
110110
title: Path to server root
111111
description: "Where static files are served from. The wwwDir config specifies the public web distribution directory. This directory is commonly the root directory for a server, where all static files can be served. This directory is built and rebuilt directly from the source files. Note: We recommend this directory is not committed to a repository."
112+
iconDir:
113+
type: array
114+
description: The iconDir config specifies from where Bolt will be taking svg for icons creation.
112115
startPath:
113116
type: string
114117
title: Local server start path

0 commit comments

Comments
 (0)