Skip to content

Commit

Permalink
export an object of functions so we don't need to sanitize names
Browse files Browse the repository at this point in the history
  • Loading branch information
evoactivity committed Jul 8, 2023
1 parent 06dde79 commit 0c4827f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/ember-svg-jar/addon/helpers/svg-jar.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { helper } from '@ember/component/helper';
import makeSVG from 'ember-svg-jar/utils/make-svg';
import * as assets from '../inlined';
import assets from '../inlined';

function getInlineAsset(assetId) {
try {
return assets[assets.makeSafeFunctionName(assetId)]().default;
return assets[assetId]().default;
} catch (err) {
return null;
}
Expand Down
37 changes: 14 additions & 23 deletions packages/ember-svg-jar/lib/inline-packer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const {
toPosixPath,
saveToFile,
relativePathFor,
makeSafeFunctionName,
} = require('./utils');

class InlinePacker extends CachingWriter {
Expand All @@ -46,37 +45,29 @@ class InlinePacker extends CachingWriter {
const assets = [];

this.listFiles().forEach(_filePath => {
let filePath = toPosixPath(_filePath);
let relativePath = relativePathFor(filePath, inputPath);
let modulePath = path.join(outputPath, `${makeAssetID(relativePath)}.js`);
let svgData = svgDataFor(readFile(filePath));
const filePath = toPosixPath(_filePath);
const relativePath = relativePathFor(filePath, inputPath);
const jsName = makeAssetID(relativePath);
const modulePath = path.join(outputPath, `${jsName}.js`);
const svgData = svgDataFor(readFile(filePath));

saveToFile(modulePath, `export default ${JSON.stringify(svgData)}`);

const jsName = makeSafeFunctionName(makeAssetID(relativePath));
const asset = { name: jsName, path: relativePath };

assets.push(asset);
assets.push(jsName);
});

saveToFile(
path.join(outputPath, `index.js`),
`import { importSync } from '@embroider/macros';\n` +
`export ${makeSafeFunctionName.toString()}` +
'\n' +
this.createFunctions(assets).join('\n') +
'\n'
`import { importSync } from '@embroider/macros';
const obj = {
${assets
.map(asset => {
return `['${asset}']: function() { return importSync('./${asset}'); }`;
})
.join(',\n')}
}; export default obj;`
);
}

createFunctions(assets) {
let { makeAssetID } = this.options;
return assets.map(asset => {
return `export function ${
asset.name
}() { return importSync('./${makeAssetID(asset.path)}'); }`;
});
}
}

module.exports = InlinePacker;
14 changes: 0 additions & 14 deletions packages/ember-svg-jar/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ function relativePathFor(filePath, inputPath) {
return filePath.replace(`${inputPath}${path.sep}`, '');
}

function makeSafeFunctionName(str) {
// remove invalid characters
let safeName = str
.split('/')
.pop()
.replace(/[^a-z0-9_$]/gi, '');
// prepend with _ if starts with a number
if (/^[0-9]/.test(safeName)) {
safeName = '_' + safeName;
}
return safeName;
}

function makeIDForPath(relativePath, { idGen, stripPath, prefix }) {
return fp.pipe(
relative => (stripPath ? path.basename(relative) : relative),
Expand Down Expand Up @@ -65,5 +52,4 @@ module.exports = {
readFile,
saveToFile,
toPosixPath,
makeSafeFunctionName,
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('InlinePacker', function () {
'bar.js':
'export default {"content":"<path d=\\"bar\\"/>","attrs":{"height":"10px","viewBox":"0 0 2 2"}}',
'index.js':
"import { importSync } from '@embroider/macros';\nexport function makeSafeFunctionName(str) {\n // remove invalid characters\n let safeName = str\n .split('/')\n .pop()\n .replace(/[^a-z0-9_$]/gi, '');\n // prepend with _ if starts with a number\n if (/^[0-9]/.test(safeName)) {\n safeName = '_' + safeName;\n }\n return safeName;\n}\nexport function bar() { return importSync('./bar'); }\nexport function foo() { return importSync('./foo'); }\n",
"import { importSync } from '@embroider/macros';\n const obj = {\n ['bar']: function() { return importSync('./bar'); },\n['foo']: function() { return importSync('./foo'); }\n }; export default obj;",
},
};

Expand Down

0 comments on commit 0c4827f

Please sign in to comment.