Using Gulp to build requireJS project - gulp-requirejs

前端 未结 7 531
轮回少年
轮回少年 2020-12-12 20:05

I am trying to use gulp-requirejs to build a demo project. I expect result to be a single file with all js dependencies and template included. Here is my gulpfile.js

7条回答
  •  感动是毒
    2020-12-12 20:13

    This is not gulp-requirejs fault.

    The reason why only main.js and config.js is in the output is because you're not requiring/defining any other files. Without doing so, the require optimizer wont understand which files to add, the paths in your config-file isn't a way to require them!

    For example you could load a main.js file from your config file and in main define all your files (not optimal but just a an example).

    In the bottom of your config-file:

    // Load the main app module to start the app
    requirejs(["main"]); 
    

    The main.js-file: (just adding jquery to show the technique.

    define(["jquery"], function($) {}); 
    

    I might also recommend gulp-requirejs-optimize instead, mainly because it adds the minification/obfuscation functions gulp-requirejs lacks: https://github.com/jlouns/gulp-requirejs-optimize

    How to implement it:

    var requirejsOptimize = require('gulp-requirejs-optimize');
    
    gulp.task('requirejsoptimize', function () {
      return gulp.src('src/js/require.config.js')
        .pipe(requirejsOptimize(function(file) {
          return {
            baseUrl: "src/js",
            mainConfigFile: 'src/js/require.config.js',
            paths: {
              requireLib: "vendor/require/require"
            },
            include: "requireLib",
            name: "require.config",
            out: "dist/js/bundle2.js"
          };
      })).pipe(gulp.dest(''));
    });
    

提交回复
热议问题