How to include node_modules in a separate browserify vendor bundle

前端 未结 3 694
我寻月下人不归
我寻月下人不归 2021-02-05 03:49

I am trying to convert an AngularJS app to use browserify. I have installed all my bower packages in node_modules using napa. Now I want to browserify them into a separate vendo

3条回答
  •  [愿得一人]
    2021-02-05 04:42

    I also face this issue. Because it was huge file when all vendor library in same file. So browser always stuck with downloading more than 10MB file. File minification also was not solution because it was take more time to compile and that was not helpful for development.

    I have kept vendor.js and app.js for separate node modules and application scripts. It's like this.

    Sample vendor.js (src/app/vendor.js)

    /**
     * Node modules
     */
    require('angular');
    require('angular-cookies');
    require('angular-ui-router');
    require('angular-aria');
    require('angular-animate');
    ....
    

    Sample app.js (src/app/app.js)

    /**
     * App Common Modules
     */
    require('./modules/about/about.routing.js');
    require('./modules/home/home.routing.js');
    
    /**
     * Services
     */
    require('./shared/services/order.js');
    require('./shared/services/product.js');
    
    
    /**
     * Directives
     */
    require('./shared/directives/dropzone.js');
    require('./shared/directives/tab-change.js');
    
    
    angular
        .module('myApp', [
            //------------- Node modules ------------//
            'ui.router',
            'ngMaterial',
    
    
            //------------- App Common modules ------------//
    
            //About
            'cloudDecor.routing.about',
            'cloudDecor.controller.about',
    
    
            //Home
            'cloudDecor.routing.home',
            'cloudDecor.controller.home',
    
    
            //------------- Services --------------//
    
            'cloudDecor.service.order',
    
    
            //------------- Directives ------------//
            'cloudDecor.directive.dropzone',
            'cloudDecor.directive.tab-change'
    
        ]);
    

    gulpfile.js

    var gulp = require('gulp'),   
        browserify = require('browserify'),
        uglify = require('gulp-uglify'),
        buffer = require('vinyl-buffer');
    
    
    //App js
    gulp.task('app_js', function() {
        // Grabs the app.js file
        browserify({
                entries: ['./src/app/app.js'],
                debug: true
            })
            .bundle()
            .pipe(source('main.js'))
            .pipe(buffer())
            .pipe(gulp.dest('./build/'));
    });
    
    //Vendor js
    gulp.task('vendor_js', function() {
        // Grabs the app.js file
        browserify({
            entries: ['./src/app/vendor.js']
        })
        .bundle()
        .pipe(source('vendor.min.js'))
        .pipe(buffer())
        .pipe(uglify({ mangle: false }))
        .pipe(gulp.dest('./build/'));
    });
    
    gulp.task('default', ['app_js', 'vendor_js']);
    

    Make sure include vendor.js before use main.js in index.html

    
    
    
    
    
    
    
    
    
    
    

提交回复
热议问题