Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to assetgraph 4 #17

Merged
merged 8 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 60 additions & 56 deletions bin/subfont
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env node

var optimist = require('optimist');
const optimist = require('optimist');

var commandLineOptions = optimist
const commandLineOptions = optimist
.usage('Create optimal font subsets from your actual font usage.\n$0 [options] <htmlFile(s) | url(s)>')
.options('h', {
alias: 'help',
Expand Down Expand Up @@ -58,11 +58,11 @@ var commandLineOptions = optimist
.argv;

if (commandLineOptions.h) {
optimist.showHelp();
process.exit(1);
optimist.showHelp();
process.exit(1);
}

var validFontDisplayValues = [
const validFontDisplayValues = [
'auto',
'block',
'swap',
Expand All @@ -75,15 +75,15 @@ if (validFontDisplayValues.indexOf(commandLineOptions['font-display']) === -1) {
process.exit(1);
}

var urlTools = require('urltools');
var rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true);
var outRoot = commandLineOptions.output && urlTools.urlOrFsPathToUrl(commandLineOptions.output, true);
var inPlace = commandLineOptions['in-place'];
var inlineSubsets = commandLineOptions['inline-fonts'];
var inlineCss = commandLineOptions['inline-css'];
var fontDisplay = commandLineOptions['font-display'];
var debug = commandLineOptions.debug;
var inputUrls;
const urlTools = require('urltools');
let rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true);
const outRoot = commandLineOptions.output && urlTools.urlOrFsPathToUrl(commandLineOptions.output, true);
const inPlace = commandLineOptions['in-place'];
const inlineSubsets = commandLineOptions['inline-fonts'];
const inlineCss = commandLineOptions['inline-css'];
const fontDisplay = commandLineOptions['font-display'];
const debug = commandLineOptions.debug;
let inputUrls;

if (commandLineOptions._.length > 0) {
inputUrls = commandLineOptions._.map(function (urlOrFsPath) {
Expand Down Expand Up @@ -119,64 +119,68 @@ if (!inPlace && !outRoot) {
process.exit(1);
}

var AssetGraph = require('assetgraph');
var query = AssetGraph.query;
const AssetGraph = require('assetgraph');

var assetGraphConfig = {
const assetGraphConfig = {
root: rootUrl
};

if (rootUrl.indexOf('file:') === -1) {
assetGraphConfig.canonicalRoot = rootUrl.replace(/\/?$/, '/'); // Ensure trailing slash
}

var followRelationsQuery = {
const followRelationsQuery = {
crossorigin: false
};

if (!commandLineOptions.recursive) {
followRelationsQuery.type = AssetGraph.query.not(/Anchor$/);
followRelationsQuery.type = { $not: { $regex: /Anchor$/ } };
}

var assetGraph = new AssetGraph(assetGraphConfig);
assetGraph
.logEvents()
.loadAssets(inputUrls)
.populate({
followRelations: followRelationsQuery
})
.subsetFonts({
debug: debug,
inlineSubsets: inlineSubsets,
inlineCss: inlineCss,
fontDisplay: fontDisplay
})
.queue(function omitFunctionCalls(assetGraph) {
assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) {
relation.omitFunctionCall();
});
})
.if(rootUrl.indexOf('file:') === -1)
.queue(function rootRelativeRelations(assetGraph) {
assetGraph.findRelations().forEach(function (relation) {
(async () => {
try {
await assetGraph.logEvents();
await assetGraph.loadAssets(inputUrls);
await assetGraph.populate({
followRelations: followRelationsQuery
});
const { fontInfo } = await assetGraph.subsetFonts({
inlineSubsets,
inlineCss,
fontDisplay
});

// Omit function calls:
assetGraph.findRelations({type: 'JavaScriptStaticUrl', to: {isLoaded: true}}).forEach(function (relation) {
relation.omitFunctionCall();
});

if (!rootUrl.startsWith('file:')) {
// Root-relative relations:

for (const relation of assetGraph.findRelations()) {
if (relation.hrefType === 'protocolRelative' || relation.hrefType === 'absolute') {
relation.hrefType = 'rootRelative';
}
})
})
.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: query.or('', undefined) }, function (asset, assetGraph) {
var nextSuffixToTry = 0;
var url;
do {
url = asset.url.replace(rootUrl, outRoot).replace(/\/?$/, '/') + 'index' + (nextSuffixToTry ? '-' + nextSuffixToTry : '') + asset.defaultExtension;
nextSuffixToTry += 1;
} while (assetGraph.findAssets({ url: url }).length > 0);

return url;
})
.endif()

.writeAssetsToDisc({ isLoaded: true, url: AssetGraph.query.createPrefixMatcher(assetGraph.root) }, outRoot, assetGraph.root)
.run(function (assetGraph) {
}

await assetGraph.moveAssets({ type: 'Html', isLoaded: true, isInline: false, fileName: { $or: ['', undefined] } }, function (asset, assetGraph) {
return asset.url.replace(/\/?$/, '/') + 'index' + asset.defaultExtension;
});
}

await assetGraph.writeAssetsToDisc({
isLoaded: true,
url: url => url.startsWith(assetGraph.root)
}, outRoot, assetGraph.root);

if (debug) {
console.log(require('util').inspect(fontInfo, false, 99));
}
console.log('Output written to', outRoot);
});
} catch (err) {
console.log(err.stack);
process.exit(1);
}
})();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "/~https://github.com/Munter/subfont#readme",
"dependencies": {
"assetgraph": "^3.13.1",
"assetgraph": "^4.0.0",
"optimist": "^0.6.1",
"urltools": "^0.3.5"
}
Expand Down