Skip to content

Instantly share code, notes, and snippets.

@aortbals
Created April 5, 2017 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aortbals/ed06179edcabbce7d7f722424286083e to your computer and use it in GitHub Desktop.
Save aortbals/ed06179edcabbce7d7f722424286083e to your computer and use it in GitHub Desktop.
CSS Modules and Global CSS with Webpack 1.x
/*
This file contains non-global CSS modules
*/
@import "css/variables";
@import "css/utilities";
:global html {
color: var(--black);
}
:global html,
:global button,
:global input,
:global textarea {
font-family: "Helvetica Neue", "Helvetica", sans-serif;
}
:global body {
margin: 0;
}
import 'css/vendor.global.css';
import 'css/app.css';
/*
Include globally-scoped CSS imports in this file. This bypasses
locally-scoped CSS modules, so all classes in this file are global.
*/
@import 'basscss';
@import 'basscss-responsive-type-scale';
@import 'quill/dist/quill.snow';
@import 'rome/dist/rome.css';
// Webpack 1.13.0
var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var env = process.env.NODE_ENV || 'development';
var isProduction = env.match(/(production|dev|qa)$/);
var cssModuleIdentName = env.match(/development|local/) ? '[path][name]---[local]---[hash:base64:5]' : '[hash:base64]';
var config = module.exports = {
entry: [
'./src/index',
],
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development'),
},
}),
],
module: {
noParse: /node_modules\/quill\/dist/,
preLoaders: [
{
test: /\.jsx?$/,
loaders: ['eslint'],
exclude: /node_modules/,
}
],
loaders: [
{
test: /\.jsx?$/,
loaders: ['babel'],
include: [
path.join(__dirname, 'src'),
],
},
{
test: /\.jpe?g$|\.gif$|\.png$|\.svg$/,
loader: 'file?name=./static/[name].[hash:8].[ext]',
include: path.join(__dirname, 'src', 'public', 'img'),
}
]
},
postcss: function(webpackInstance) {
return [
require('postcss-import')({
addDependencyTo: webpackInstance,
path: [
path.join(__dirname, 'src'),
'node_modules',
]
}),
require('postcss-cssnext')(),
require('postcss-inline-svg')({
path: path.join(__dirname, 'src'),
}),
require('postcss-svgo'),
];
},
resolve: {
extensions: ['', '.js', '.jsx', '.css'],
modulesDirectories: [
path.join(__dirname, 'src'),
'node_modules',
],
},
};
if (isProduction) {
var ExtractTextPlugin = require('extract-text-webpack-plugin');
config.devtool = 'source-map';
config.output = {
path: path.join(__dirname, 'dist'),
filename: 'static/bundle.js?v=[chunkhash:8]',
publicPath: '/'
};
config.module.loaders.push({
test: /\.css/,
exclude: /\.global\.css$/,
loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=' + cssModuleIdentName + '!postcss')
});
config.module.loaders.push({
test: /\.global\.css$/,
loader: 'style!css!postcss'
});
config.plugins.push(
new HtmlWebpackPlugin({
template: 'index.html',
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
}
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: {
screw_ie8: true,
warnings: false
}
}),
new ExtractTextPlugin('static/styles.css?v=[contenthash:8]'),
new CopyWebpackPlugin([
{ from: 'robots.txt' },
])
);
} else {
config.devtool = 'eval';
config.output = {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/',
};
config.entry.unshift(
'webpack-dev-server/client?http://localhost:3002',
'webpack/hot/only-dev-server',
'react-hot-loader/patch'
);
config.module.loaders.push({
test: /\.css$/,
exclude: /\.global\.css$/,
loader: 'style!css?modules&importLoaders=1&localIdentName=' + cssModuleIdentName + '!postcss'
});
config.module.loaders.push({
test: /\.global\.css$/,
loader: 'style!css!postcss'
});
config.plugins.push(
new HtmlWebpackPlugin({
template: 'index.html'
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment