Created
July 2, 2016 19:59
-
-
Save justingreenberg/f1735abffbae3778014f8d14b599a2ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
import { interpolateName } from 'loader-utils' | |
// webpack config | |
const publicPath = 'static' | |
const filenameTemplate = '[name].[hash].[ext]' | |
const extensions = [ | |
'png', 'jpg', 'jpeg', 'gif', | |
'ico', 'svg', 'otf', 'eot', 'svg', | |
'ttf', 'woff', 'woff2' | |
] | |
// patch require to interpolate filename | |
const requireHook = (context, filename) => { | |
const content = fs.readFileSync(filename) | |
const outputName = interpolateName( | |
{ resourcePath: filename }, | |
filenameTemplate, | |
{ content } | |
) | |
context.exports = `${publicPath}${outputName}` | |
} | |
extensions.forEach(hook => { | |
if (require.extensions[`.${hook}`]) | |
return null | |
require.extensions[`.${hook}`] = requireHook | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'require-patch' // only necessary once in entry | |
// elsewhere in component... | |
import logo from './logo.jpg' | |
// becomes... | |
var logo = require('static/logo.49da5201548ddfa0438a3a10d088eaf0.jpg') | |
// which matches webpack output... | |
/* | |
Hash: 9aed90340dc0202435a1 | |
Version: webpack 2.1.0-beta.13 | |
Time: 192ms | |
Asset Size Chunks Chunk Names | |
static/49da5201548ddfa0438a3a10d088eaf0.jpg 247 kB [emitted] | |
... | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to submit this as a PR. It's awesome!