最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

amd - How to use gulp to build JavaScript bundles? - Stack Overflow

matteradmin3PV0评论

I want to use gulp to build bundles of JavaScript files.

For example I have the following structure in my project:

  1. /vendor/vendor1/vendor1.js
  2. /vendor/vendor2/vendor2.js
  3. /js/includes/include1.js
  4. /js/includes/include2.js
  5. /js/bundle1.js
  6. /js/bundle2.js

There are vendor includes (1-2), local includes (3-4), and bundle files (5-6).

Vendor includes are just third-party JavaScript libraries installed with bower or poser. They can be CommonJS, AMD or just a plain-old jQuery plugins.

I want to specify dependencies inside of a bundle files like this:

/js/bundle1.js

(function() {

    // Vendor includes.
    include('vendor1');
    include('vendor2');

    // Local includes.
    include('includes/include1.js');
    include('includes/include2.js');

    // Some code here.

})();

I want gulp to process this source file and create a final distribution file (bundle) ensuring that all dependencies (includes) are merged together in a single file. So I can include foo.js from my HTML and all dependencies will be available to it.

I want to have a clear and robust system to manage all dependencies inside of a project and build distribution files.

  • How can I achieve this?
  • What format should I use for my own scripts (AMD, CommonJS, other)?
  • How do I specify dependencies in my source bundle files?
  • How do I build distribution?

I want to use gulp to build bundles of JavaScript files.

For example I have the following structure in my project:

  1. /vendor/vendor1/vendor1.js
  2. /vendor/vendor2/vendor2.js
  3. /js/includes/include1.js
  4. /js/includes/include2.js
  5. /js/bundle1.js
  6. /js/bundle2.js

There are vendor includes (1-2), local includes (3-4), and bundle files (5-6).

Vendor includes are just third-party JavaScript libraries installed with bower or poser. They can be CommonJS, AMD or just a plain-old jQuery plugins.

I want to specify dependencies inside of a bundle files like this:

/js/bundle1.js

(function() {

    // Vendor includes.
    include('vendor1');
    include('vendor2');

    // Local includes.
    include('includes/include1.js');
    include('includes/include2.js');

    // Some code here.

})();

I want gulp to process this source file and create a final distribution file (bundle) ensuring that all dependencies (includes) are merged together in a single file. So I can include foo.js from my HTML and all dependencies will be available to it.

I want to have a clear and robust system to manage all dependencies inside of a project and build distribution files.

  • How can I achieve this?
  • What format should I use for my own scripts (AMD, CommonJS, other)?
  • How do I specify dependencies in my source bundle files?
  • How do I build distribution?
Share Improve this question edited Oct 14, 2015 at 5:17 Slava Fomin II asked Apr 4, 2014 at 23:57 Slava Fomin IISlava Fomin II 28.7k35 gold badges134 silver badges208 bronze badges 1
  • Bonus addendum's to the question for 2020: What about nowadays with native Chrome support for ES6 modules? What about Firefox/Edge? What about Chrome or Firefox plugins / injection scripts? Do our needs change if we're targeting just the newest version of Chrome vs. also supporting legacy browsers? – Josh Desmond Commented Jul 31, 2020 at 5:28
Add a ment  | 

2 Answers 2

Reset to default 6

Your question is posed as if there's a single answer, but there isn't. The problem you're trying to solve is one that many people have solved in many different ways, and you've identified two of the major options: AMD and CommonJS. There are other ways, but given that you might be new to Javascript dependency management as well as gulp, I'd remend going with something that's relatively straightforward (even though this subject is inherently not straightforward).

I think the easiest route for you might be:

  • use CommonJS to express the dependencies
  • use browserify to resolve them into bundles
  • in browserify, use the "UMD" method so that you get a single bundle that will work for apps that use either AMD or CommonJS or are not using either of these dependency management systems

The statement in gulp to run browserify as such might look something like:

var browserify = require('gulp-browserify');
gulp.src('bundles/bundle1.js', {read: false})
  .pipe(browserify({
    'standalone': true
  })
  .pipe(rename('bundle1Output.js'))
  .pipe(gulp.dest('dist'));

That should give you a dist/bundle1Output.js file.

There is a gulp plugin for this:

https://www.npmjs./package/gulp-include

It should do what you want, except that in your bundle file instead of this:

    (function() {

        // Vendor includes.
        include('vendor1');
        include('vendor2');

        // Local includes.
        include('includes/include1.js');
        include('includes/include2.js');

        // Some code here.

    })();

You would have to write:

    //=require vendor1/**/*.js
    //=require vendor2/**/*.js

    //=require includes/include1.js
    //=require includes/include2.js

    // Some code here
Post a comment

comment list (0)

  1. No comments so far