Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Created April 23, 2017 04:10
Show Gist options
  • Save aloha1003/e9d23a361ed6c0e5403613dd3971aad8 to your computer and use it in GitHub Desktop.
Save aloha1003/e9d23a361ed6c0e5403613dd3971aad8 to your computer and use it in GitHub Desktop.
Webpack with auto scan path for multiple entry points and recursive path mapping
'use strict';
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
// Map common libraries with aliases
var providePlugins = new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Bootstrap: 'bootstrap',
'window.Bootstrap': 'bootstrap'
});
// Paths
var public_dir = __dirname + '/web';
var source_dir = __dirname + '/app/Resources/assets';
// Grab all project js file to compile by file mask
var glob = require('glob-fs')({});
var files = glob.readdirSync('./app/Resources/assets/js/**.js');
// Use each entry to build the key and add them to entries
var entries = {};
for (var i = 0, l = files.length; i < l; i++) {
var key = files[i].replace('app/Resources/assets/js/','').replace('.js', '');
entries[key] = source_dir + '/js/' + key;
}
// Finish setup with sass and vendors
entries.app = [source_dir + "/sass/styles.scss"];
entries.vendors = [
'jquery',
'jquery-ui',
//'jquery-tags-input',
'bootstrap-sass',
'datatables',
'bootbox',
//'bootpag',
'select2',
'slug',
];
// Back to webpack
var config = {
entry: entries,
// We use a CommonsChunkPlugin to take all the vendor libraries and create a vendors.js file.
// The first argument matches the key "vendors" entries
plugins: [
providePlugins,
new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.js'),
new ExtractTextPlugin("../../css/styles.css")
],
output: {
path: public_dir + '/js/compiled',
filename: "[name].js"
},
module: {
loaders: [
{test: /\.(scss|css)$/, loader: ExtractTextPlugin.extract('css!sass?indentedSyntax')},
{test: /\.(otf|eot)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file?&name=../fonts/[name].[ext]"},
{
test: /\.(woff|woff2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url?limit=10000&minetype=application/font-woff&name=../fonts/[name].[ext]"
},
{
test: /\.svg?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url?limit=10000&mimetype=image/svg+xml&name=../fonts/[name].[ext]"
},
{
test: /\.ttf?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: "url?limit=10000&mimetype=application/octet-stream&name=../fonts/[name].[ext]"
},
]
}
};
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment