Created
June 4, 2016 00:15
-
-
Save b2whats/9f567b3b66859bd652ba920ab9a3a74f 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
// server/index.js | |
require('babel-register') | |
const universalHook = require('./universal-hook').default | |
const webpackDev = require('../../webpack/webpack.config.dev').default | |
const config = require('../../webpack/config').default | |
universalHook(webpackDev, config) | |
require('./server') |
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' | |
const ext = [ | |
'3gp', 'a', 'ai', 'aif', 'aiff', 'asc', 'asf', 'asm', 'asx', 'atom', 'au', | |
'avi', 'bat', 'bin', 'bmp', 'bz2', 'c', 'cab', 'cc', 'chm', 'class', 'com', | |
'conf', 'cpp', 'crt', 'css', 'csv', 'cxx', 'deb', 'der', 'diff', 'djv', 'djvu', | |
'dll', 'dmg', 'doc', 'dot', 'dtd', 'dvi', 'ear', 'eml', 'eps', 'exe', 'f', | |
'f77', 'f90', 'flv', 'for', 'gem', 'gemspec', 'gif', 'gz', 'h', 'hh', 'htm', | |
'html', 'ico', 'ics', 'ifb', 'iso', 'jar', 'java', 'jnlp', 'jpeg', 'jpg', 'js', | |
'json', 'log', 'm3u', 'm4v', 'man', 'mathml', 'mbox', 'mdoc', 'me', 'mid', | |
'midi', 'mime', 'mml', 'mng', 'mov', 'mp3', 'mp4', 'mp4v', 'mpeg', 'mpg', 'ms', | |
'msi', 'odp', 'ods', 'odt', 'ogg', 'p', 'pas', 'pbm', 'pdf', 'pem', 'pgm', | |
'pgp', 'pkg', 'pl', 'pm', 'png', 'pnm', 'ppm', 'pps', 'ppt', 'ps', 'psd', 'py', | |
'qt', 'ra', 'rake', 'ram', 'rar', 'rb', 'rdf', 'roff', 'rpm', 'rss', 'rtf', | |
'ru', 's', 'sgm', 'sgml', 'sh', 'sig', 'snd', 'so', 'svg', 'svgz', 'swf', 't', | |
'tar', 'tbz', 'tcl', 'tex', 'texi', 'texinfo', 'text', 'tif', 'tiff', 'torrent', | |
'tr', 'txt', 'vcf', 'vcs', 'vrml', 'war', 'wav', 'wma', 'wmv', 'wmx', 'wrl', | |
'wsdl', 'xbm', 'xhtml', 'xls', 'xml', 'xpm', 'xsl', 'xslt', 'yaml', 'yml', 'zip' | |
] | |
function getLoaderExt(originRegexp, extList) { | |
const extToString = extList.join(' ') | |
const stripTestRegexp = originRegexp.source.replace(/(\\\.)|(\$)/g, '') | |
const regexpMatchExt = new RegExp(`\\b${stripTestRegexp}\\b`, 'ig') | |
return extToString.match(regexpMatchExt) | |
} | |
export default function universalHook(webpackConfig, params = {}) { | |
if (process.env.NODE_ENV === 'production') return null | |
const options = { | |
stylesName: '[name]_[local]__[hash:base64:5]', | |
...params, | |
} | |
const loaders = webpackConfig.module.loaders | |
const hooks = loaders.filter(loader => loader.hook) | |
if (hooks.length === 0) return null | |
hooks.forEach(loader => { | |
if(loader.hook === 'file') { | |
if (process.env.NODE_ENV !== 'production' && loader.loader !== 'file') { | |
throw new Error( | |
`In development mode include only one file-loader` | |
) | |
} | |
const loaderExt = getLoaderExt(loader.test, ext) | |
loaderExt.forEach(ext => { | |
if (require.extensions[`.${ext}`]) return null | |
require.extensions[`.${ext}`] = function (require, filename) { | |
const content = fs.readFileSync(filename) | |
const webpackName = interpolateName({resourcePath: filename}, '[hash].[ext]', {content}); | |
require.exports = webpackConfig.output.publicPath + webpackName | |
} | |
}) | |
} | |
if(loader.hook === 'css') { | |
const hook = require('css-modules-require-hook') | |
hook({ | |
generateScopedName: options.stylesName, | |
}) | |
} | |
}) | |
} |
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
module: { | |
loaders: [ | |
{ test: /\.js$/, loaders: ['babel'], include: config.src }, | |
{ test: /\.css$/, loaders: ['style', `css?modules&localIdentName=${config.stylesName}`], hook: 'css'}, | |
{ test: /\.(jpe?g|png|gif|svg)$/, loader: 'file', hook: 'file'}, | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment