Last active
September 22, 2017 21:43
-
-
Save cgons/9161c5d05f863dda85fde6f03ef032bc to your computer and use it in GitHub Desktop.
Use Webpack 2 to build JS and SASS files/bundles easily.
This file contains hidden or 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
{ | |
"name": "frontend", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"autoprefixer": "^7.1.4", | |
"babel-core": "^6.26.0", | |
"babel-loader": "^7.1.2", | |
"babel-preset-env": "^1.6.0", | |
"babel-preset-react": "^6.24.1", | |
"css-loader": "^0.28.7", | |
"extract-text-webpack-plugin": "^3.0.0", | |
"postcss-loader": "^2.0.6", | |
"postcss-scss": "^1.0.2", | |
"precss": "^2.0.0", | |
"react": "^15.6.1", | |
"react-dom": "^15.6.1", | |
"webpack": "^3.6.0" | |
} | |
} |
This file contains hidden or 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.exports = { | |
parser: 'postcss-scss', | |
plugins: [ | |
require('precss'), | |
require('autoprefixer') | |
] | |
} |
This file contains hidden or 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
const path = require('path'); | |
const webpack = require('webpack'); | |
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
// JS Config | |
// --------- | |
const jsconfig = { | |
entry: { | |
channels: './src/pages/channels.js', | |
}, | |
output: { | |
path: path.join(__dirname, 'public', 'js'), | |
filename: '[name].js' | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.jsx?$/, | |
exclude: /node_modules/, | |
use: { | |
loader: 'babel-loader', | |
options: { | |
presets: ['env', 'react'] | |
} | |
} | |
} | |
] | |
}, | |
plugins: [ | |
new webpack.optimize.CommonsChunkPlugin({ | |
name: 'vendor' | |
}) | |
] | |
}; | |
// CSS Config | |
// ---------- | |
const cssconfig = { | |
entry: { | |
styles: './src/styles.css', | |
}, | |
output: { | |
path: path.join(__dirname, 'public', 'css'), | |
filename: '[name].css' | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.css$/, | |
use: ExtractTextPlugin.extract({ | |
use: [ | |
{ | |
loader: 'css-loader', | |
options: { | |
importLoaders: 1 | |
} | |
}, | |
'postcss-loader' | |
] | |
}) | |
} | |
] | |
}, | |
plugins: [ | |
new ExtractTextPlugin('styles.css') | |
] | |
}; | |
module.exports = [jsconfig, cssconfig]; |
This file contains hidden or 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
const path = require('path'); | |
const ExtractTextPlugin = require('extract-text-webpack-plugin'); | |
// Note: | |
// ------ | |
// We are using Webpack's "multi-compiler" feature to specify multiple config objects | |
// to export. This is key to making this work easily as it allows us to isolate | |
// options relating to JS transpiling/bundeling from what is done for Sass files. | |
// When using the sass-loader, one must also use the css-loader (first) to ensure that | |
// css is translated correctly. | |
// Sass is compiled using a combination of css-loader, sass-loader + extract-text-webpack-plugin | |
// ------ | |
// Javascript Config | |
// ----------------- | |
const jsConfig = { | |
entry: { | |
website: './apps/Website/static/js/website.js', | |
app: './apps/Backend/static/js/app.js', | |
}, | |
output: { | |
path: path.resolve(__dirname, 'public/js'), | |
filename: '[name]-bundle.js' | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.js$/, | |
exclude: /(node_modules|bower_components)/, | |
use: { | |
loader: 'babel-loader', | |
options: { | |
presets: ['env'] | |
} | |
} | |
} | |
] | |
} | |
}; | |
// Sass Config | |
// ----------- | |
const filename = '[name].css'; | |
const sassConfig = { | |
entry: { | |
website: './apps/Website/static/sass/website.scss', | |
backend: './apps/Backend/static/sass/backend.scss', | |
}, | |
output: { | |
path: path.resolve(__dirname, 'public/css'), | |
filename: filename, | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.scss$/, | |
use: ExtractTextPlugin.extract({ | |
use: ['css-loader', 'sass-loader'] | |
}) | |
} | |
] | |
}, | |
plugins: [ | |
new ExtractTextPlugin(filename), | |
] | |
}; | |
module.exports = [jsConfig, sassConfig]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment