B
B
biz532021-09-14 12:36:11
JavaScript
biz53, 2021-09-14 12:36:11

How to import packages into js file using Gulp and Webpack?

Now I can import, I have the main js file script.js and I can import all my scripts there via the export default function. It looks like this:

"use strict";
import mainSliders from './main__sliders.js';

document.addEventListener("DOMContentLoaded", function () {
  mainSliders();
});

But in this scenario, I have to add libraries through an HTML file.
<script src="assets/js/lib/swiper/swiper.min.js"></script>
<script src="assets/js/script.min.js"></script>


And actually the question is: how can I make it so that I can import libraries into js itself and I get one js file, where all the necessary libraries are already included and which I could include in HTML? This is how my gallp task for scripts is configured:

<script src="assets/js/script.min.js"></script>

function scripts(modeName) {
    return gulp.src(src.js)
    .pipe(webpack({
        watch: false,
        mode: modeName,
        module: {
          rules: [
            { test: /\.(js)$/ },
          ],
        },
        
        stats: 'errors-only'
    }))
    .pipe(babel())
    .on('error', console.error.bind(console))
    .pipe(concat(`script.js`))
        .pipe(gulp.dest(build.js))
    .pipe(uglify({
        toplevel: true
    }))
        .pipe(
            rename({
                extname: ".min.js"
            })
        )

    .pipe(gulp.dest(build.js))
    .pipe(browserSync.stream())
}

const buildScripts = () => scripts('production');
const devScripts = () => scripts('development');

gulp.task('build', gulp.series(clean, grid, gulp.parallel(fonts, html, styles, buildScripts, compress, retina, libraries)));

gulp.task('watch', gulp.series(clean, grid, gulp.parallel(fonts, html, styles, devScripts, compress, libraries), watch));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question