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

Commit

Permalink
Support hot reloading of LESS and CSS chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
artkravchenko committed Oct 28, 2017
1 parent 2d1f9a7 commit 926ac3e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 46 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"build:client": "webpack --config './res/webpack/client.js'",
"build:server": "webpack --config './res/webpack/server.js'",
"build:tasks": "webpack --config './res/webpack/tasks.js'",
"test": "DB_ENV=test run-s reset-db:test build:client mocha",
"test": "DB_ENV=test run-s reset-db:test cleanup build:client mocha",
"coverage": "DB_ENV=test npm run coverage:run",
"travis": "DB_ENV=travis run-s reset-db:travis build:client travis:test lint travis:flow",
"travis": "DB_ENV=travis run-s reset-db:travis cleanup build:client travis:test lint travis:flow",
"travis:flow": "run-s flow:install-types flow",
"travis:test": "run-s coverage:run coverage:coveralls coverage:clean",
"reset-db:test": "echo \"Preparing test database...\" && babel-node test-helpers/dropDatabase.js && knex --env test migrate:latest",
Expand Down
24 changes: 15 additions & 9 deletions res/webpack/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ const clientConfiguration = merge({}, baseConfiguration, {
filename: __DEV__ ? 'assets/[name].js' : 'assets/[name]-[chunkhash].js',
name: 'manifest',
minChunks: Infinity
}),
new ExtractTextPlugin({
filename: path.join('assets/styles/[name]-[contentHash].css'),
allChunks: true
}),
new ManifestPlugin({
fileName: 'webpack-chunks.json',
writeToFileEmit: true
})
],
target: 'web'
Expand All @@ -91,7 +83,11 @@ if (__DEV__) {
},
plugins: [
new webpack.NamedChunksPlugin(),
new webpack.HotModuleReplacementPlugin()
new webpack.HotModuleReplacementPlugin(),
new ManifestPlugin({
fileName: 'webpack-chunks.json',
writeToFileEmit: true
})
]
});
} else {
Expand Down Expand Up @@ -119,6 +115,16 @@ if (__DEV__) {
},
sourceMap: false
}),
new ExtractTextPlugin({
filename: path.join('assets/styles/[name]-[contentHash].css'),
allChunks: true
}),
// cannot push to __DEV__-independent config
// since the ordering makes sense (after ExtractTextPlugin in this case)
new ManifestPlugin({
fileName: 'webpack-chunks.json',
writeToFileEmit: true
}),
new webpack.optimize.AggressiveMergingPlugin({
minSizeReduce: true
}),
Expand Down
95 changes: 60 additions & 35 deletions res/webpack/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,35 @@ module.exports.clientLibFixes = () => [{
}];

module.exports.css = (__DEV__) => [{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
publicPath: '../../',
use: [
{ loader: 'css-loader',
options: {
autoprefixer: false,
calc: false,
mergeIdents: false,
mergeRules: false,
uniqueSelectors: false
} },
{ loader: 'postcss-loader' }
].map(l => addSourceMap(l, __DEV__))
})
}];
test: /\.css$/
}].map(rule => {
let use;
if (__DEV__) {
use = [
{ loader: 'style-loader?sourceMap' },
{ loader: 'css-loader?sourceMap' },
{ loader: 'postcss-loader?sourceMap' }
];
} else {
use = ExtractTextPlugin.extract({
fallback: 'style-loader',
publicPath: '../../',
use: [
{ loader: 'css-loader',
options: {
autoprefixer: false,
calc: false,
mergeIdents: false,
mergeRules: false,
uniqueSelectors: false
} },
{ loader: 'postcss-loader' }
]
});
}

return Object.assign(rule, { use });
});

module.exports.ejsIndexTemplate = () => [{
test: /\/index\.ejs$/,
Expand Down Expand Up @@ -173,24 +185,37 @@ module.exports.less = (__DEV__, context) => [{
{ test: /\.less$/ },
{ include: path.join(context, 'src/less') },
]
},
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
publicPath: '../../',
use: [
{ loader: 'css-loader',
options: {
autoprefixer: false,
calc: false,
mergeIdents: false,
mergeRules: false,
uniqueSelectors: false
} },
{ loader: 'postcss-loader' },
{ loader: 'less-loader' }
].map(l => addSourceMap(l, __DEV__))
})
}];
}
}].map(rule => {
let use;
if (__DEV__) {
use = [
{ loader: 'style-loader?sourceMap' },
{ loader: 'css-loader?sourceMap' },
{ loader: 'postcss-loader?sourceMap' },
{ loader: 'less-loader?sourceMap' }
];
} else {
use = ExtractTextPlugin.extract({
fallback: 'style-loader',
publicPath: '../../',
use: [
{ loader: 'css-loader',
options: {
autoprefixer: false,
calc: false,
mergeIdents: false,
mergeRules: false,
uniqueSelectors: false
} },
{ loader: 'postcss-loader' },
{ loader: 'less-loader' }
]
});
}

return Object.assign(rule, { use });
});

module.exports.serverJS = (__DEV__, context) => [{
test: /\.js?$/,
Expand Down
4 changes: 4 additions & 0 deletions src/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimal-ui">
<%- metadata.title %>
<% if (paths.webpackChunks['vendor.css']) { %>
<link rel="stylesheet" href="<%- paths.webpackChunks['vendor.css'] %>">
<% } %>
<% if (paths.webpackChunks['app.css']) { %>
<link rel="stylesheet" href="<%- paths.webpackChunks['app.css'] %>">
<% } %>
<link href='https://api.mapbox.com/mapbox.js/v2.2.1/mapbox.css' rel='stylesheet' />
<% if (gtm) { %>
<!-- Google Tag Manager -->
Expand Down

0 comments on commit 926ac3e

Please sign in to comment.