Answer the question
In order to leave comments, you need to log in
How to bundle multiple separate JS with gulp and webpack-stream?
The project needs several small JS files to connect to different pages.
There is a folder and a js/parts folder. I want each file from js to be separately collected and uploaded to the dev folder with its imports from js/parts.
Now there is such gulp-task for js:
const gulp = require('gulp'),
cfg = require('../package.json').config,
webpackConfig = require('../webpack.config'),
webpackProdConfig = require('../webpack-prod.config'),
webpackStream = require('webpack-stream'),
webpack = require('webpack'),
minify = require('gulp-minifier');
//Build js for development
gulp.task('dev:js', function () {
gulp.src(cfg.src_js + '/*.js')
.pipe(webpackStream(webpackConfig, webpack))
.on('error', function handleError() {
this.emit('end'); // Recover from errors
})
.pipe(gulp.dest(cfg.dev_js))
});
//Build js for production
gulp.task('prod:js', function () {
gulp.src(cfg.src_js + '/entry.js')
.pipe(webpackStream(webpackProdConfig, webpack))
.on('error', function handleError() {
this.emit('end'); // Recover from errors
})
.pipe(minify({
minify: true,
minifyJS: {
sourceMap: false
}
}))
.pipe(gulp.dest(cfg.build_js));
});
// webpack.config.js
var webpack = require('webpack');
module.exports = {
mode: 'development',
entry: './src/js/entry.js',
output: {
filename: 'index.js',
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: [
['env', {modules: false}],
],
},
},
],
},
plugins: [
],
externals: {
jquery: 'jQuery',
},
devtool: 'source-map'
};
Answer the question
In order to leave comments, you need to log in
Of documentation
In entry you pass several entry points and that's it
entry: {
messenger: './messenger',
about: './about',
},
...
entry: {
header: './src/js/parts/header.js',
header: './src/js/parts/footer.js',
// и ещё любое кол-во точек входа
},
output: {
filename: '[name].js',
path: __dirname + cfg.build_js,
},
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question