13
I’m starting with Webpack. It compiles all javascript and css files into a single file Build.js
, Bundle.js
, whatever it is... In the end it generates a fully minified file using the Node command Production
, but even so it generates a relatively heavy file. For a large project ( Which is mine ), it can reach 10mb easy. Charging 10mb at once is very bad.
How does Webpack control this? What is the advantage of using it if it brings me a minified but huge file? It selects the scripts that will be loaded from import
?
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
'scss': 'vue-style-loader!css-loader!sass-loader',
'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
}
}
},
{
test: /\.css$/,
include: /node_modules/,
loaders: ['style-loader', 'css-loader'],
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
},
{
test: /\.s(a|c)ss$/,
loader: 'style-loader!css-loader!sass-loader'
},
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loader: 'url-loader?limit=100000'
}
]
},
resolve: {
extensions: ['.json', '.min.css', '.min.js', , '.scss' , '.css', '.js' ],
alias: {
'vue$': 'vue/dist/vue.esm.js'
}
},
devServer: {
historyApiFallback: true,
noInfo: true
},
performance: {
hints: false
},
devtool: '#eval-source-map',
plugins:[
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
]
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
Thank you!
It actually does a lot more than that. But you need to learn to control what it’s doing, which takes a while. It seems the case to generate several "Chunks" of separate JS, not a single build. The webpack is made for this, and can know what is in each generated js file and load them bit by bit as needed. Post your webpack configuration on the question that improves the chances of you getting help in your specific case.
– bfavaretto
Thanks for the answer! For what you gave to see I’m still beeem layman in Webpack. I made some changes to the hand as needed. But I basically made these settings from a Vuejs CLI. It at the end generates me a file of about 20 lines, but each file minified in only one line
– Jackson
I personally find the webpack complicated, I’ve been using it for a while (1 year or so), and there’s a lot I don’t know. For example, I know he does what I say, but I don’t know how to do it in his code without rereading the documentation. Let’s see if anyone comes in here who knows how to answer in the can. Or at some point I’ll come back here and answer, 'cause I need to learn how to do that, too :)
– bfavaretto
Yay. He’s really good, right, but I got a kick out of it. There must be some detail I must be forgetting rsrs, anyway, I appreciate the help :)
– Jackson
Does this give a light? https://hackernoon.com/optimising-your-application-bundle-size-with-webpack-e85b00bab579 that question in the English OS also appears to be on the same line https://stackoverflow.com/questions/31040379/webpack-creating-large-file--withsmallproject
– nunks