Skip to content

Instantly share code, notes, and snippets.

@paganotoni
Created April 5, 2018 20:51
Show Gist options
  • Save paganotoni/59406abb54cedc2b1f823acaf1e7dc16 to your computer and use it in GitHub Desktop.
Save paganotoni/59406abb54cedc2b1f823acaf1e7dc16 to your computer and use it in GitHub Desktop.
stanislas-webpack
const webpack = require("webpack");
const glob = require("glob");
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const ManifestPlugin = require("webpack-assets-manifest");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");
var MODE = process.env.NODE_ENV || "development";
var entries = {
application: [
'./node_modules/jquery-ujs/src/rails.js',
'./assets/css/application.scss',
],
}
glob.sync("./assets/*/*.*").forEach((entry) => {
if (entry === './assets/css/application.scss') {
return
}
let key = entry.replace(/(\.\/assets\/(js|css|go)\/)|\.(js|s[ac]ss|go)/g, '')
if(key.startsWith("_") || (/(js|s[ac]ss|go)$/i).test(entry) == false) {
return
}
if( entries[key] == null) {
entries[key] = [entry]
return
}
entries[key].push(entry)
})
module.exports = {
mode: MODE,
entry: entries,
output: {
filename: "[name].[hash].js",
path: `${__dirname}/public/assets`
},
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true
}),
new OptimizeCSSAssetsPlugin({})
],
splitChunks: {
chunks: "initial",
name: "vendors"
},
},
plugins: [
new CleanWebpackPlugin([
"public/assets"
], {
verbose: false,
}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new MiniCssExtractPlugin({
filename: "[name].[contenthash].css"
}),
new CopyWebpackPlugin(
[{
from: "./assets",
to: ""
}], {
copyUnmodified: true,
ignore: ["css/**", "js/**"]
}
),
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new ManifestPlugin({
fileName: "manifest.json"
})
],
module: {
rules: [{
test: /\.jsx?$/,
loader: "babel-loader",
exclude: /node_modules/
}, {
test: /\.(png|jpe?g|gif|ico)$/,
use: 'file-loader?name=images/[name].[hash].[ext]'
}, {
// Stylesheets loader
test: /\.s[ac]ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
sourceMap: true
}
},
{
loader: "postcss-loader",
options: {
sourceMap: true
}
},
{
loader: "sass-loader",
options: {
sourceMap: true,
includePaths: [
path.resolve(__dirname, "./node_modules/compass-mixins/lib")
]
}
}
]
},
{ test: /\.(woff|woff2|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/,use: "url-loader"},
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,use: "file-loader" },
{
test: require.resolve("jquery"),
use: "expose-loader?jQuery!expose-loader?$"
},
{
test: /\.go$/,
use: "gopherjs-loader"
}
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment