Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created October 29, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondale-sc/01b2e38f605bffc745678d015b5027e7 to your computer and use it in GitHub Desktop.
Save rondale-sc/01b2e38f605bffc745678d015b5027e7 to your computer and use it in GitHub Desktop.
Glimmer Inline SVG
// src/utilities/inline-svg/helper.js
// @ts-ignore:
import { svgs } from '../../../utils/svgs';
function dottify(path) {
return (path || '').replace(/^\//g, '').replace(/\//g, '.');
}
export default function inlineSvg([path]) {
let jsonPath = dottify(path);
let svg = svgs[jsonPath];
return {
string: svg,
toHTML() {
return this.string;
},
toString() {
return this.string;
}
};
}
// in-repo addon
/* eslint-env node */
'use strict';
var mergeTrees = require('broccoli-merge-trees');
var Funnel = require('broccoli-funnel');
var flatiron = require('broccoli-flatiron');
var BroccoliDebug = require('broccoli-debug');
module.exports = {
name: 'inline-svg',
isDevelopingAddon() {
return true;
},
preprocessTree(type, tree) {
if (type === 'src') {
let debugTree = BroccoliDebug.buildDebugCallback(`inline-svg-addon`);
let svgTree = new Funnel('svgs', {
include: [new RegExp(/\.svg$/)]
});
let manifest = flatiron(svgTree, {
outputFile: 'src/utils/svgs.ts',
prefix: 'export let svgs = ',
trimExtensions: true
});
manifest = debugTree(manifest, 'manifest');
tree = debugTree(tree, 'srcTree');
let finalOutput = debugTree(new mergeTrees([tree, manifest]), 'src_manifest_merged');
return finalOutput;
} else {
return tree;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment