Giter VIP home page Giter VIP logo

assetgraph's Introduction

AssetGraph

NPM version Build Status Coverage Status Dependencies Status

AssetGraph is an extensible, node.js-based framework for manipulating and optimizing web pages and web applications. The main core is a dependency graph model of your entire website, where all assets are treated as first class citizens. It can automatically discover assets based on your declarative code, reducing the configuration needs to a minimum.

If you just want to get started with the basics, read Peter Müller - Getting started with Assetgraph.

If you are looking for a prepackaged build system take a look at Assetgraph-builder.

Tools built with AssetGraph

  • assetgraph-builder - A static web page build system that post-processes your website with extremely little configuration
  • subfont - A tool that supercharges your webfont loading by automatically applying all best practice loading techniques and generating optimal font subsets
  • hyperlink - A link checker tool that will ensure all your internal and external links are intact and up to date
  • seespee - A Content-Security Policy generator. Point it at a webpage and it will tell you what policy you need as a minimum
  • trackingdog - cli for finding the original source location of a line+column in a generated file, utilizing the source map
  • bringhome - cli for downloading a web page and its assets to a local directory.

Assets and relations

All web build tools, even those that target very specific problems, have to get a bunch of boring stuff right just to get started, such as loading files from disc, parsing and serializing them, charsets, inlining, finding references to other files, resolution of and updating urls, etc.

The observation that inspired the project is that most of these tasks can be viewed as graph problems, where the nodes are the assets (HTML, CSS, images, JavaScript...) and the edges are the relations between them, e.g. anchor tags, image tags, favorite icons, css background-image properties and so on.

An example illustration of an asset graph representing a web page

An AssetGraph object is a collection of assets (nodes) and the relations (edges) between them. It's a basic data model that allows you to populate, query, and manipulate the graph at a high level of abstraction. For instance, if you change the url of an asset, all relations pointing at it are automatically updated.

Additionally, each individual asset can be inspected and massaged using a relevant API: jsdom for HTML, PostCSS for CSS, and an Esprima AST for Javascript.

AssetGraph represents inline assets the same way as non-inline ones, so eg. inline scripts, stylesheets, and images specified as data: urls are also first-class nodes in the graph. This means that you don't need to dig into the HTML of the containing asset to manipulate them. An extreme example would be an Html asset with a conditional comment with an inline stylesheet with an inline image, which are modelled as 4 separate assets:

<!DOCTYPE html>
<html>
  <head>
    <!--[if !IE]> -->
    <style type="text/css">
      body {
        background-image: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==);
      }
    </style>
    <!-- <![endif]-->
  </head>
  <body></body>
</html>

These are some of the supported assets and associated relation types:

HTML

<a>, <link rel="stylesheet|shortcut icon|fluid-icon|alternate|serviceworker">, <script>, <style>, <html manifest="..."> <img>, <video>, <audio>, <applet>, <embed>, <esi:include>, <iframe>, <svg>, <meta property="og:...">

SVG

<style>, inline style=... attributes, event handlers, <?xml-stylesheet href=...>, <font-face-src>

CSS

//# sourceMappingURL=..., background-image: url(...), @import url(...), behavior: url(...), filter: AlphaImageLoader(src='...'), @font-face { src: url(...) }

JavaScript

//# sourceMappingURL=..., homegrown 'foo/bar.png'.toString('url') syntax for referencing external files

Web manifest

Icon urls, related_applications, start_url, etc.

Cache manifest (appcache)

Entries in the CACHE, NETWORK and FALLBACK sections

JSON, XML, PNG, GIF, JPEG, ICO

(none)

Features

  • Build an AssetGraph programmatically or load it from disk or a remote server via http.
  • Find explicit dependencies between JavaScript and CSS and roll them out as <script> and <link rel='stylesheet'> tags in your HTML.
  • Bundle and inline CSS and JavaScript.
  • Create a cache manifest with references to all the assets your web app needs to be usable offline.
  • Move all CSS, JavaScript, image assets etc. to a static dir and rename them to md5.extension so the web server can be configured to set a far-future Cache-Control.
  • Help getting your static assets on a CDN by allowing you to easily rewrite all references to them.
  • Use Graphviz to visualize your dependencies at any step.
  • Using the separate assetgraph-sprite transform: Optimize CSS background images by creating sprite images. The spriting is guided by a set of custom CSS properties with a -ag-sprite prefix.

Installation

Make sure you have node.js and npm installed, then run:

$ npm install assetgraph

Querying the graph

AssetGraph supports a flexible syntax for finding assets and relations in a populated graph using the findAssets and findRelations methods. Both methods take a query object as the first argument. The query engine uses MongoDB-like queries via the sift module. Please consult that to learn about the advanced querying features. Below are some basic examples.

Get an array containing all assets in the graph:

var allAssets = assetGraph.findAssets();

Find assets by type:

var htmlAssets = assetGraph.findAssets({ type: 'Html' });

Find assets of different named types:

var jsAndCss = assetGraph.findAssets({ type: { $in: ['Css', 'JavaScript' ] });

Find assets by matching a regular expression against the url:

var localImageAssets = assetGraph.findAssets({
  url: { $regex: /^file:.*\.(?:png|gif|jpg)$/ },
});

Find assets by predicate function:

var orphanedJavaScriptAssets = assetGraph.findAssets(function (asset) {
  return (
    asset.type === 'JavaScript' &&
    assetGraph.findRelations({ to: asset }).length === 0
  );
});

Find all HtmlScript (<script src=...> and inline <script>) relations:

var allHtmlScriptRelations = assetGraph.findRelations({ type: 'HtmlScript' });

Query objects have "and" semantics, so all conditions must be met for a multi-criteria query to match:

var textBasedAssetsOnGoogleCom = assetGraph.findAssets({
  isText: true,
  url: { $regex: /^https?:\/\/(?:www\.)google\.com\// },
});

Find assets by existence of incoming relations:

var importedCssAssets = assetGraph.findAssets({
  type: 'Css',
  incomingRelations: { $elemMatch: { type: 'CssImport' } },
});

Relation queries can contain nested asset queries when querying the to and from properties.

Find all HtmlAnchor (<a href=...>) relations pointing at local images:

assetGraph.findRelations({
  type: 'HtmlAnchor',
  to: { isImage: true, url: { $regex: /^file:/ } },
});

Transforms and workflows

AssetGraph comes with a collection of premade "transforms" that you can use as high level building blocks when putting together your build procedure. Most transforms work on a set of assets or relations and usually accept a query object so they can be scoped to work on only a specific subset of the graph.

Usually you'll start by loading some initial assets from disc or via http using the loadAssets transform, then get the related assets added using the populate transform, then do the actual processing. Eventually you'll probably write the resulting assets back to disc.

Thus the skeleton looks something like this:

var AssetGraph = require('assetgraph');

const assetGraph = new AssetGraph({ root: '/the/root/directory/' });

await assetGraph.loadAssets('*.html'); // Load all Html assets in the root dir
await assetGraph.populate({ followRelations: { type: 'HtmlAnchor' } }); // Follow <a href=...>
// More work...
await assetGraph.writeAssetsToDisc({ type: 'Html' }); // Overwrite existing files

// Done!

In the following sections the built-in transforms are documented individually:

assetGraph.addCacheManifest([queryObj])

Add a CacheManifest asset to each Html asset in the graph (or to all Html assets matched by queryObj if provided). The cache manifests will contain relations to all assets reachable by traversing the graph through relations other than HtmlAnchor.

assetGraph.bundleRelations(queryObj[, strategyName])

Bundle the Css and JavaScript assets pointed to by the relations matched by queryObj.

The strategyName (string) parameter can be either:

oneBundlePerIncludingAsset (the default)

Each unique asset pointing to one or more of the assets being bundled will get its own bundle. This can lead to duplication if eg. several Html assets point to the same sets of assets, but guarantees that the number of http requests is kept low.

sharedBundles

Create as many bundles as needed, optimizing for combined byte size of the bundles rather than http requests. Warning: Not as well tested as oneBundlePerIncludingAsset.

Note that a conditional comment within an Html asset conveniently counts as a separate including asset, so in the below example ie.css and all.css won't be bundled together:

<!--[if IE]><link rel="stylesheet" href="ie.css" /><![endif]-->
<link rel="stylesheet" href="all.css" />

The created bundles will be placed at the root of the asset graph with names derived from their unique id (for example file://root/of/graph/124.css) and will replace the original assets.

assetGraph.compressJavaScript([queryObj[, compressorName[, compressorOptions]]])

Compresses all JavaScript assets in the graph (or those specified by queryObj).

The compressorName (string) parameter can be either:

uglifyJs (the default and the fastest)

The excellent UglifyJS compressor. If provided, the compressorOptions object will be passed to UglifyJS' ast_squeeze command.

yuicompressor

Yahoo's YUICompressor though Tim-Smart's node-yuicompressor module. If provided, the compressorOptions object will be passed as the second argument to require('yui-compressor').compile.

closurecompiler

Google's Closure Compiler through Tim-Smart's node-closure module. If provided, the compressorOptions object will be passed as the second argument to require('closure-compiler').compile.

assetGraph.convertCssImportsToHtmlStyles([queryObj])

Finds all Html assets in the graph (or those specified by queryObj), finds all CssImport relations (@import url(...)) in inline and external CSS and converts them to HtmlStyle relations directly from the Html document.

Effectively the inverse of assetGraph.convertHtmlStylesToInlineCssImports.

Example:

<style type="text/css">
  @import url(print.css) print;
  @import url(foo.css);
  body {
    color: red;
  }
</style>

is turned into:

<link rel="stylesheet" href="print.css" media="print" />
<link rel="stylesheet" href="foo.css" />
<style type="text/css">
  body {
    color: red;
  }
</style>

assetGraph.convertHtmlStylesToInlineCssImports([queryObj])

Finds all Html assets in the graph (or those specified by queryObj), finds all outgoing, non-inline HtmlStyle relations (<link rel='stylesheet' href='...'>) and turns them into groups of CssImport relations (@import url(...)) in inline stylesheets. A maximum of 31 CssImports will be created per inline stylesheet.

Example:

<link rel="stylesheet" href="foo.css" />
<link rel="stylesheet" href="bar.css" />

is turned into:

<style type="text/css">
  @import url(foo.css);
  @import url(bar.css);
</style>

This is a workaround for the limit of 31 stylesheets in Internet Explorer <= 8. This transform allows you to have up to 31*31 stylesheets in the development version of your HTML and still have it work in older Internet Explorer versions.

assetGraph.drawGraph(fileName)

Uses the Graphviz dot command to render the current contents of the graph and writes the result to fileName. The image format is automatically derived from the extension and can be any of these. Using .svg is recommended.

Requires Graphviz to be installed, sudo apt-get install graphviz on Debian/Ubuntu.

assetGraph.executeJavaScriptInOrder(queryObj[, context])

Experimental: For each JavaScript asset in the graph (or those matched by queryObj), find all reachable JavaScript assets and execute them in order.

If the context parameter is specified, it will be used as the execution context. Otherwise a new context will be created using vm.createContext.

assetGraph.externalizeRelations([queryObj])

Finds all inline relations in the graph (or those matched by queryObj) and makes them external. The file names will be derived from the unique ids of the assets.

For example:

<script>
  foo = 'bar';
</script>
<style type="text/css">
  body {
    color: maroon;
  }
</style>

could be turned into:

<script src="4.js"></script>
<link rel="stylesheet" href="5.css" />

assetGraph.inlineCssImagesWithLegacyFallback([queryObj[, options]])

Finds all Html assets in the graph (or those matched by queryObj), finds all directly reachable Css assets, and converts the outgoing CssImage relations (background-image etc.) to data: urls, subject to these criteria:

  1. If options.sizeThreshold is specified, images with a greater byte size won't be inlined.

  2. To avoid duplication, images referenced by more than one CssImage relation won't be inlined.

  3. A CssImage relation pointing at an image with an inline GET parameter will always be inlined (eg. background-image: url(foo.png?inline);). This takes precedence over the first two criteria.

  4. If options.minimumIeVersion is specified, the data: url length limitations of that version of Internet Explorer will be honored.

If any image is inlined an Internet Explorer-only version of the stylesheet will be created and referenced from the Html asset in a conditional comment.

For example:

await assetGraph.inlineCssImagesWithLegacyFallback(
  { type: 'Html' },
  { minimumIeVersion: 7, sizeThreshold: 4096 }
);

where assetGraph contains an Html asset with this fragment:

<link rel="stylesheet" href="foo.css" />

and foo.css contains:

body {
  background-image: url(small.gif);
}

will be turned into:

<!--[if IE]><link rel="stylesheet" href="foo.css" /><![endif]-->
<!--[if !IE]>--><link rel="stylesheet" href="1234.css" /><!--<![endif]-->

where 1234.css is a copy of the original foo.css with the images inlined as data: urls:

body {
  background-image: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==);
}

The file name 1234.css is just an example. The actual asset file name will be derived from the unique id of the copy and be placed at the root of the assetgraph.

assetGraph.inlineRelations([queryObj])

Inlines all relations in the graph (or those matched by queryObj). Only works on relation types that support inlining, for example HtmlScript, HtmlStyle, and CssImage.

Example:

await assetGraph.inlineRelations({ type: { $in: ['HtmlStyle', 'CssImage'] } });

where assetGraph contains an Html asset with this fragment:

<link rel="stylesheet" href="foo.css" />

and foo.css contains:

body {
  background-image: url(small.gif);
}

will be turned into:

<style type="text/css">
  body {
    background-image: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==);
  }
</style>

Note that foo.css and the CssImage will still be modelled as separate assets after being inlined, so they can be manipulated the same way as when they were external.

assetGraph.loadAssets(fileName|wildcard|url|Asset[, ...])

Add new assets to the graph and make sure they are loaded, returning a promise that fulfills with an array of the assets that were added. Several syntaxes are supported, for example:

const [aHtml, bCss] = await assetGraph.loadAssets('a.html', 'b.css'); // Relative to assetGraph.root
await assetGraph.loadAssets({
  url: 'http://example.com/index.html',
  text: 'var foo = bar;', // The source is specified, won't be loaded
});

file:// urls support wildcard expansion:

await assetGraph.loadAssets('file:///foo/bar/*.html'); // Wildcard expansion
await assetGraph.loadAssets('*.html'); // assetGraph.root must be file://...

assetGraph.mergeIdenticalAssets([queryObj])

Compute the MD5 sum of every asset in the graph (or those specified by queryObj) and remove duplicates. The relations pointing at the removed assets are updated to point at the copy that is kept.

For example:

await assetGraph.mergeIdenticalAssets({ type: { $in: ['Png', 'Css'] } });

where assetGraph contains an Html asset with this fragment:

<head>
  <style type="text/css">
    body {
      background-image: url(foo.png);
    }
  </style>
</head>
<body>
  <img src="bar.png" />
</body>

will be turned into the following if foo.png and bar.png are identical:

<head>
  <style type="text/css">
    body {
      background-image: url(foo.png);
    }
  </style>
</head>
<body>
  <img src="foo.png" />
</body>

and the bar.png asset will be removed from the graph.

assetGraph.minifyAssets([queryObj])

Minify all assets in the graph, or those specified by queryObj. Only has an effect for asset types that support minification, and what actually happens also varies:

Html and Xml

Pure-whitespace text nodes are removed immediately.

Json, JavaScript, and Css

The asset gets marked as minified (isPretty is set to false), which doesn't affect the in-memory representation (asset.parseTree), but is honored when the asset is serialized. For JavaScript this only governs the amount of whitespace (escodegen's compact parameter); for how to apply variable renaming and other compression techniques see assetGraph.compressJavaScript.

Compare to assetGraph.prettyPrintAssets.

transforms.populate(options)

Add assets to the graph by recursively following "dangling relations". This is the preferred way to load a complete web site or web application into an AssetGraph instance after using assetGraph.loadAssets to add one or more assets to serve as the starting point for the population. The loading of the assets happens in parallel.

The options object can contain these properties:

from: queryObj

Specifies the set assets of assets to start populating from (defaults to all assets in the graph).

followRelations: queryObj

Limits the set of relations that are followed. The default is to follow all relations.

onError: function (err, assetGraph, asset)

If there's an error loading an asset and an onError function is specified, it will be called, and the population will continue. If not specified, the population will stop and pass on the error to its callback. (This is poorly thought out and should be removed or redesigned).

concurrency: Number

The maximum number of assets that can be loading at once (defaults to 100).

Example:

const assetGraph = new AssetGraph();
await assetGraph.loadAssets('a.html');
await assetGraph.populate({
  followRelations: {
    type: 'HtmlAnchor',
    to: { url: { $regex: /\/[bc]\.html$/ } },
  },
});

If a.html links to b.html, and b.html links to c.html (using <a href="...">), all three assets will be in the graph after assetGraph.populate is done. If c.html happens to link to d.html, d.html won't be added.

assetGraph.prettyPrintAssets([queryObj])

Pretty-print all assets in the graph, or those specified by queryObj. Only has an effect for asset types that support pretty printing (JavaScript, Css, Html, Xml, and Json).

The asset gets marked as pretty printed (isPretty is set to true), which doesn't affect the in-memory representation (asset.parseTree), but is honored when the asset is serialized. For Xml, and Html, however, the existing whitespace-only text nodes in the document are removed immediately.

Compare to assetGraph.minifyAssets.

Example:

// Pretty-print all Html and Css assets:
await assetGraph.prettyPrintAssets({ type: { $in: ['Html', 'Css'] } });

assetGraph.removeRelations([queryObj, [options]])

Remove all relations in the graph, or those specified by queryObj.

The options object can contain these properties:

detach: Boolean

Whether to also detach the relations (remove their nodes from the parse tree of the source asset). Only supported for some relation types. Defaults to false.

removeOrphan: Boolean

Whether to also remove assets that become "orphans" as a result of removing their last incoming relation.

assetGraph.setHtmlImageDimensions([queryObj])

Sets the width and height attributes of the img elements underlying all HtmlImage relations, or those matching queryObj. Only works when the image pointed to by the relation is in the graph.

Example:

const AssetGraph = require('assetgraph');

const assetGraph = new AssetGraph();
await assetGraph.loadAssets('hasanimage.html');
await assetGraph.populate();

// assetGraph.findAssets({type: 'Html'})[0].text === '<body><img src="foo.png"></body>'

await assetGraph.setHtmlImageDimensions();

// assetGraph.findAssets({type: 'Html'})[0].text === '<body><img src="foo.png" width="29" height="32"></body>'

assetGraph.writeStatsToStderr([queryObj])

Dumps an ASCII table with some basic stats about all the assets in the graph (or those matching queryObj) in their current state.

Example:

       Ico   1   1.1 KB
       Png  28 196.8 KB
       Gif 145 129.4 KB
      Json   2  60.1 KB
       Css   2 412.6 KB
JavaScript  34   1.5 MB
      Html   1   1.3 KB
    Total: 213   2.2 MB

assetGraph.writeAssetsToDisc(queryObj, outRoot[, root])

Writes the assets matching queryObj to disc. The outRoot parameter must be a file:// url specifying the directory where the files should be output. The optional root parameter specifies the url that you want to correspond to the outRoot directory (defaults to the root property of the AssetGraph instance).

Directories will be created as needed.

Example:

const AssetGraph = require('assetgraph');

const assetGraph = new AssetGraph({root: 'http://example.com/'});
await assetGraph.loadAssets(
  'http://example.com/bar/quux/foo.html',
  'http://example.com/bar/baz.html'
);

// Write the two assets to /my/output/dir/quux/foo.html and /my/output/dir/baz.html:
await assetGraph.writeAssetsToDisc({type: 'Html'} 'file:///my/output/dir/', 'http://example.com/bar/');

assetGraph.writeAssetsToStdout([queryObj])

Writes all assets in the graph (or those specified by queryObj) to stdout. Mostly useful for piping out a single asset.

License

AssetGraph is licensed under a standard 3-clause BSD license -- see the LICENSE-file for details.

assetgraph's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

assetgraph's Issues

protocol relative urls

Assetgraph might want to handle protocol relative urls as well, for example:

"//domain.tld/page"

Slim down the dependency list

Eg. iconv is 28MB, but only needed when working with non-utf-8 assets.

less, stylus, coffee-script, graphviz, closure-compiler and yui-compressor could also be made optional.

Support for "external" templates

Allow external templates to be included, so they can be written in separate files. During development these files should be dynamically loaded, and compiled into the JavaScript when building a production version. Would be nice to make the templating engine configurable as well.

Support for Closure library dependency system

It would be nice if Assetgraph could support the Google Closure dependency syntax; goog.provide and goog.require. Since there is no direct relation between a goog.provide statement and file names you'll have to do some preprocessing of the JavaScript files to discover this relationship. You can use my calcdep Node.js port to do this: https://github.com/bramstein/calcdeps.js

There is an example of how to use it in my Closure Loader project: https://github.com/bramstein/closureloader

cache assets in localstorage

Steve Souders talked about google and Bing storing assets in localstorage at the mobilism conference.

The basic concept is based on the realization that caching is not controllable enough though http headers and localstorage is more reliable.
A cookie or something will indicate to the server whether it needs to send these ressources again.

We might want to build that in as a transformation to the graph.

Model .xtemplate (ExtJS) as a proper asset

... And code fragments as inline JavaScript assets.

Purpose: Less awkward support for one.tr in .xtemplate files included via one.getText.

This really belongs in assetgraph-builder or a separate assetgraph-extjs package.

Make assetgraph aware of media queries

At Stephen Hays talk about CSS media queries at the Mobilism 2011 conference the question of optimal syntax for includsion of such media specific styles.

His suggestion is including files individually in the html.
While this may not be optimal (researchers are hopefully working on that), it's the syntactically best way of writing queries.

If assetgraph could understand these, we could always inline the media query part or have it as a part of the css bundle if needed.

Also we might be able to create seperate applications builds tailored specificly to certain device properties. For example an android app doesn't need css for fluid layouts above 1200px screen width if you bundle a phonegap application.

If we do specific builds for specific devices/resolutions, take a look at image resizing during build.

Querying the graph should have a nicer and more compact syntax

Some ugly examples from transforms/addCacheManifestSiteMap.js:

        var existingManifestRelations = assetGraph.findRelations('from', htmlAsset).filter(function (relation) {
            return relation.type === 'HTMLCacheManifest';
        });

        [...]

        if (asset !== manifest && !assetGraph.findRelations('to', asset).some(function (relation) {return relation.from === manifest;})) {
            assetGraph.attachAndAddRelation(new relations.CacheManifestEntry({
                from: manifest,
                to: asset
            }));
        }

reconsider transforms/addCDNPrefix.js

Currently it's sort of a hack that finds all relations to stuff that can be loaded from a CDN and prepends http://cdnhostname.tld/prefix/... to the raw URL. A better concept would be to move or clone all the relevant assets and set the fully qualified CDN URL. That way it would also be possible to save some bytes by using relative paths between assets that are on the CDN.

bundleAssets should be careful about conditional comments

transforms.bundleAssets produces bundles that break the cascade if there's a conditional comment between the relations.

For example: transforms.bundleAssets({type: 'Css', incoming: {type: 'HtmlStyle'}}) applied to this Html:

<link rel='stylesheet' href='foo.css'>
<!--[if IE]><link rel='stylesheet' href='bar.css'><![endif]-->
<link rel='stylesheet' href='quux.css'>

will choose to bundle foo.css and quux.css like this:

<link rel='stylesheet' href='foo+quux.css'>
<!--[if IE]><link rel='stylesheet' href='bar.css'><![endif]-->

thus messing up the order of the CSS rules in IE.

(Nice catch, munter!)

Better error messages

Where should I look in my code to fix this syntax error?

/home/PROJECT/node_modules/assetgraph/node_modules/cssom-papandreou/lib/parse.js:112
                    throw SyntaxError("Missing */");
           ^
SyntaxError: Missing */
    at SyntaxError (unknown source)
    at Object.parse (/home/PROJECT/node_modules/assetgraph/node_modules/cssom-papandreou/lib/parse.js:112:12)
    at Css.parseTree (/home/PROJECT/node_modules/assetgraph/lib/assets/Css.js:68:37)
    at Css.findOutgoingRelationsInParseTree (/home/PROJECT/node_modules/assetgraph/lib/assets/Css.js:100:37)
    at Css.outgoingRelations (/home/PROJECT/node_modules/assetgraph/lib/assets/Asset.js:441:44)
    at Css.populate (/home/PROJECT/node_modules/assetgraph/lib/assets/Asset.js:484:13)
    at AssetGraph.addAsset (/home/PROJECT/node_modules/assetgraph/lib/AssetGraph.js:119:19)
    at Html.<anonymous> (/home/PROJECT/node_modules/assetgraph/lib/assets/Asset.js:496:41)
    at Array.forEach (native)
    at Html.populate (/home/PROJECT/node_modules/assetgraph/lib/assets/Asset.js:484:32)

Figure out a way to do graph "snapshots" that can be serialized

If possible/feasible at all, this will be a major effort.

Would enable a bunch of advanced features such as "incremental" optimization of a graph: When an asset changes, roll back and reapply the necessary transformations.

Probably requires some kind of transaction concept or a higher degree of formalization in the queries and "commands" that transforms can execute on a graph.

Find a way to add error handling to transforms

Seem natural that transforms should get err as their first argument, but I'm not sure. Would be nice to get rid of transforms.escapeToCallback, whose sole purpose is to shift in a null as the first arg to the callback.

"No type provided in assetConfig" error.

I'm trying to use asset-graph on a static site (http://sbhr.dk), but I get odd errors when trying to even load it in asset-graph:

var AssetGraph = require('assetgraph'),
    transforms = AssetGraph.transforms;

new AssetGraph({root: './_build'}).queue(
    transforms.loadAssets('index.html'),
    transforms.populate(),
    transforms.stats(),
    transforms.writeAssetsToDisc()
).run(function () {console.log("DONE");});

Output:

/home/PROJECT/node_modules/assetgraph/lib/assets/index.js:93
        throw new Error("assets.create: No type provided in assetConfig");
              ^
Error: assets.create: No type provided in assetConfig
    at Object.create (/home/PROJECT/node_modules/assetgraph/lib/assets/index.js:93:15)
    at Function.<anonymous> (/home/PROJECT/node_modules/assetgraph/lib/transforms/populate.js:80:72)
    at native
    at /home/PROJECT/node_modules/assetgraph/lib/util/passError.js:6:13
    at /home/PROJECT/node_modules/assetgraph/lib/resolvers/file.js:33:25
    at [object Object].<anonymous> (fs.js:80:5)
    at [object Object].emit (events.js:64:17)
    at fs.js:820:12

(And I get the Missing */ about every third time.)

If I comment out the transforms.populate(), I get this output (but no changed files):

  Html 1 4.1 KB
Total: 1 4.1 KB
'DONE'

I tried looking briefly in the source code, but all arguments seem to be optional. I also tried to copy the {followRelations: {type: 'HtmlAnchor'}} from the example in the README, but to no avail...

Automatic png optimizations

It would be nice to have a transform that automates the png optimizations that you would normally have to use the -one-image-postprocess css property for.

I am thinking that the transform should run pngcrush -rem alla and optipng in order as default on all png images. I can think of no downside to getting optimal compression per default besides build time.

A further optimization might be gained by detecting if the png file in question has a color palette <= 256. In that case it should run pngquant <numColors> on the image before the other optimizations.

The number of colors can be detected unreliably by looking at the png palette chunks, or reliably by counting the number of unique colors in the images histogram. The histogram is expensive, but reliable, so I recommend that approach.

The histogram color count can be done with this command line: pngtopnm png_file | ppmhist -noheader | wc -l

Since this transform will be quite expensive there should be a command line switch to activate or deactivate it in assetgraph-builder. I think the best setting for the normal user would be to have the transform always on and have a switch to disable it for the power user that has done the optimizations already.

Handle relation classes whose outgoing URLs aren't relative to the containing asset

Some relations are to be resolved relative to the ancestor HTML document rather than the asset contaning them. I'm thinking of the ones below:

  • JavaScriptStaticUrl
  • CSSBehavior
  • CSSAlphaImageLoader

This adds complexity and has the potential to cause huge problems when moving assets around in the presence of more than one of the above relations pointing at the same thing.

Working with root-relative urls in these cases would be an easy way out, but I'd really like to keep the pure-relative urls property.

Outgoing relations from Htc assets are broken

Seems like relative urls in the Htc asset itself are also interpreted as relative to the Html document, d'oh. Will need to have separate HtcScript, HtcStyle etc. relations that are identical to their Html* counterparts except for the baseAssetQuery.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.