Skip to content
This repository has been archived by the owner on Apr 1, 2020. It is now read-only.

Commit

Permalink
decouple asset hashing from webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Apr 21, 2017
1 parent 8627f93 commit 7e4a87a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 43 deletions.
20 changes: 20 additions & 0 deletions build/app/asset-hashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');

module.exports = () => {
const hashes = fs.readdirSync(path.join(process.cwd(), 'public'))
.filter(asset => !/(\.map|about\.json|asset-hashes\.json)$/.test(asset))
.map(name => {
const file = fs.readFileSync(path.join(process.cwd(), 'public', name), 'utf8');
const hash = crypto.createHash('sha1').update(file).digest('hex');
const hashedName = `${hash.substring(0, 8)}/${name}`;
return { name, hashedName };
})
.reduce((previous, current) => {
previous[current.name] = current.hashedName;
previous[current.name + '.map'] = current.hashedName + '.map';
return previous;
}, {});
fs.writeFileSync('./public/asset-hashes.json', JSON.stringify(hashes, null, 2), { encoding: 'UTF8' });
}
2 changes: 2 additions & 0 deletions build/app/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const shellpromise = require('shellpromise');
const shellpipe = require('./shellpipe');
const downloadAssets = require('./download-assets');
const assetHashes = require('./asset-hashes')

const exit = err => {
logger.error(err);
Expand Down Expand Up @@ -50,6 +51,7 @@ program
devAdvice();

shellpipe(`webpack ${options.production ? '--bail' : '--dev'} --config ${webpackConfPath}`)
.then(assetHashes)
.then(aboutJson)
.then(downloadAssets)
.then(() => {
Expand Down
7 changes: 3 additions & 4 deletions build/app/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require('fs');
const join = require('path').join;
const Wrap = require('../lib/addons/wrap');
const headCss = require('../lib/head-css')
const hashedAssets = require('../lib/hashed-assets')

const gitignore = fs.readFileSync(join(process.cwd(), '.gitignore'), 'utf8')
.split('\n');
Expand Down Expand Up @@ -47,9 +46,9 @@ noGitignoreWildcard();
// we no longer build a main.js for the app when generating the standard asset variants
const variants = [
// all entry points excluding main.js generated as normal
hashedAssets(headCss(nWebpack(Object.assign({}, baseConfig, {
headCss(nWebpack(Object.assign({}, baseConfig, {
entry: filterEntryKeys(baseConfig.entry, /main\.js$/, true)
}))))
})))
]

// new entry point for main.js declaring external n-ui
Expand All @@ -69,7 +68,7 @@ mainJs.plugins.push(
)
);

variants.push(hashedAssets(mainJs));
variants.push(mainJs);

if (process.env.NEXT_APP_SHELL === 'local') {
const nWebpackWarning = `
Expand Down
31 changes: 0 additions & 31 deletions build/lib/addons/asset-hashes.js

This file was deleted.

8 changes: 0 additions & 8 deletions build/lib/hashed-assets.js

This file was deleted.

0 comments on commit 7e4a87a

Please sign in to comment.