CLI and helper methods for asset versioning. Includes support for Webpack chunking.
- Minimum supported Node version is v14
- Minimum supported Webpack version is v4.44.0
npm install --save-dev @hdsydsvenskan/asset-versions
Install Husky hooks using yarn husky install
before pushing changes.
Follow Semantic Versioning and use np and a version like patch | minor | major | prepatch | preminor | premajor | prerelease | 1.2.3
np patch
See a full example, including Webpack support, in example/ folder and/or run the start
npm script (run yarn start
/ npm start
).
- Add an
assets.json
- Run
asset-versions
- Get an
assets-versioned.json
file
const AssetVersions = require('@hdsydsvenskan/asset-versions');
const assets = new AssetVersions({
assetDefinitions: __dirname + '/assets.json'
});
// Returns something like 'assets/rev/main-e508b3af03.css'
assets.getAssetPath('assets/main.css');
Or, in case of code splitted javascript (likely through the use of Webpack):
// Returns something like ['assets/rev/main-e508b3af03.js', 'assets/rev/vendor-abc123.js']
assets.getAssetPathWithDependencies('assets/main.js');
Create a assets.json
file:
{
"sourceDir": "assets",
"targetDir": "assets/rev/",
"files": [
"main.css",
"main-dev.css",
"main.js"
]
}
- versions file name – both the cli (through the
--output
/-o
flag) and the module (through theversionsFileName
option) can be tweaked to use another versions file than the defaultasset-versions.json
- AssetVersions.webpackManifestPluginGenerate – a
generate
method for use with webpack-manifest-plugin to generate manifest with dependencies