Skip to content

Commit

Permalink
Speed up requiring index.js file (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Aug 1, 2023
1 parent 99c2d74 commit c759d8f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
38 changes: 27 additions & 11 deletions packages/ember-svg-jar/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

const fs = require('fs');
const _ = require('lodash');
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const SVGOptimizer = require('broccoli-svg-optimizer');
const broccoliReplace = require('broccoli-string-replace');
const Symbolizer = require('./symbolizer/symbolizer');
const InlinePacker = require('./inline-packer');
const ViewerAssetsBuilder = require('./viewer-assets-builder');
const ViewerBuilder = require('./viewer-builder');
const buildOptions = require('./build-options');
const { makeIDForPath } = require('./utils');
const prepareSymbolLoaderScript = require('./prepare-symbol-loader-script');

function mergeTreesIfNeeded(trees, options) {
if (trees.length === 1) {
return trees[0];
}

let mergedOptions = _.assign({ overwrite: true }, options);
return trees.length === 1 ? trees[0] : new MergeTrees(trees, mergedOptions);

const MergeTrees = require('broccoli-merge-trees');

return new MergeTrees(trees, mergedOptions);
}

module.exports = {
Expand All @@ -28,6 +25,9 @@ module.exports = {

included(app) {
this._super.included.apply(this, arguments);

const buildOptions = require('./build-options');

this.svgJarOptions = buildOptions(app);
},

Expand All @@ -41,6 +41,8 @@ module.exports = {
// Add viewer assets to public dir
let svgJarPublicTree = this._super.treeForPublic.apply(this, arguments);

const broccoliReplace = require('broccoli-string-replace');

svgJarPublicTree = broccoliReplace(svgJarPublicTree, {
files: ['**/index.html'],
pattern: {
Expand Down Expand Up @@ -78,6 +80,8 @@ module.exports = {
const outputFile = this.optionFor('symbol', 'outputFile');
const isTestEnv = app.environment === 'test';

const prepareSymbolLoaderScript = require('./prepare-symbol-loader-script');

return prepareSymbolLoaderScript(rootURL, outputFile, isTestEnv);
}

Expand All @@ -103,6 +107,8 @@ module.exports = {
originalSvgsFor(strategy) {
let sourceDirs = this.sourceDirsFor(strategy);

const Funnel = require('broccoli-funnel');

return new Funnel(mergeTreesIfNeeded(sourceDirs), {
include: ['**/*.svg'],
});
Expand All @@ -111,6 +117,8 @@ module.exports = {
optimizedSvgsFor(strategy, originalSvgs) {
let optimizerConfig = this.optionFor(strategy, 'optimizer');

const SVGOptimizer = require('broccoli-svg-optimizer');

return new SVGOptimizer(originalSvgs, {
svgoConfig: _.omit(optimizerConfig, 'svgoModule'),
svgoModule: optimizerConfig.svgoModule,
Expand All @@ -132,6 +140,8 @@ module.exports = {
let stripPath = this.optionFor(strategy, 'stripPath');
let prefix = this.optionFor(strategy, 'prefix');

const ViewerAssetsBuilder = require('./viewer-assets-builder');

return new ViewerAssetsBuilder(this.svgsFor(strategy), {
strategy,
validationConfig: this.svgJarOptions.validations,
Expand All @@ -143,6 +153,8 @@ module.exports = {
});
});

const ViewerBuilder = require('./viewer-builder');

return new ViewerBuilder(mergeTreesIfNeeded(viewerBuilderTrees), {
outputFile: 'svg-jar-viewer.json',
});
Expand All @@ -152,6 +164,8 @@ module.exports = {
let idGen = this.optionFor('inline', 'idGen');
let stripPath = this.optionFor('inline', 'stripPath');

const InlinePacker = require('./inline-packer');

return new InlinePacker(this.svgsFor('inline'), {
makeAssetID(relativePath) {
return makeIDForPath(relativePath, { idGen, stripPath });
Expand All @@ -164,6 +178,8 @@ module.exports = {
let stripPath = this.optionFor('symbol', 'stripPath');
let prefix = this.optionFor('symbol', 'prefix');

const Symbolizer = require('./symbolizer/symbolizer');

return new Symbolizer(this.svgsFor('symbol'), {
outputFile: this.optionFor('symbol', 'outputFile'),
svgAttrs: this.optionFor('symbol', 'containerAttrs'),
Expand Down
3 changes: 2 additions & 1 deletion packages/ember-svg-jar/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const fs = require('fs');
const cheerio = require('cheerio').default;
const path = require('path').posix;
const osPathSep = require('path').sep;
const _ = require('lodash');
Expand Down Expand Up @@ -30,6 +29,8 @@ function makeIDForPath(relativePath, { idGen, stripPath, prefix }) {
}

function svgDataFor(svgContent) {
const cheerio = require('cheerio').default;

let $svg = cheerio.load(svgContent, { xmlMode: true })('svg');

return {
Expand Down

0 comments on commit c759d8f

Please sign in to comment.