Using Gulp to build requireJS project - gulp-requirejs

前端 未结 7 506
轮回少年
轮回少年 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:12

    Sorry for my english. This solution works for me. (I used gulp-requirejs at my job)

    I think you've forgotten to set mainConfigFile in your gulpfile.js. So, this code will be work

    gulp.task('requirejsBuild', function() {
        rjs({
            name: 'main',
            mainConfigFile: 'path_to_config/config.js',
            baseUrl: './app',
            out: 'result.js'
        })
        .pipe(gulp.dest('app/dist'));
    
    });
    

    In addition, I think when you run that task in gulp, require can not find its config file and

    0 讨论(0)
  • 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(''));
    });
    
    0 讨论(0)
  • 2020-12-12 20:22

    gulp-requirejs has been blacklisted by the gulp folks. They see the RequireJS optimizer as its own build system, incompatible with gulp. I don't know much about that, but I did find an alternative in amd-optimize that worked for me.

    npm install amd-optimize --save-dev
    

    Then in your gulpfile:

    var amdOptimize = require('amd-optimize');
    var concat = require('gulp-concat');
    
    gulp.task('bundle', function ()
    {
        return gulp.src('**/*.js')
            .pipe(amdOptimize('main'))
            .pipe(concat('main-bundle.js'))
            .pipe(gulp.dest('dist'));
    });
    

    The output of amdOptimize is a stream which contains the dependencies of the primary module (main in the above example) in an order that resolves correctly when loaded. These files are then concatenated together via concat into a single file main-bundle.js before being written into the dist folder.

    You could also minify this file and perform other transformations as needed.


    As an aside, in my case I was compiling TypeScript into AMD modules for bundling. Thinking this through further I realized that when bundling everything I don't need the asynchronous loading provided by AMD/RequireJS. I am going to experiment with having TypeScript compile CommonJS modules instead, then bundling them using webpack or browserify, both of which seem to have good support within gulp.

    0 讨论(0)
  • 2020-12-12 20:22

    Try this code in your gulpfile:

    // Node modules
    var
        fs = require('fs'),
        vm = require('vm'),
        merge = require('deeply');    
    
    // Gulp and plugins
    var
        gulp = require('gulp'),    
        gulprjs= require('gulp-requirejs-bundler');
    
    // Config
    var
        requireJsRuntimeConfig = vm.runInNewContext(fs.readFileSync('app/config.js') + '; require;'),
        requireJsOptimizerConfig = merge(requireJsRuntimeConfig, {
            name: 'main',
            baseUrl: './app',
            out: 'result.js',
            paths: {
                requireLib: 'bower_modules/requirejs/require'
            },
            insertRequire: ['main'],
            // aliases from config.js - libs will be included to result.js
            include: [
                'requireLib',
                "almond",
                "underscore",
                "jquery",
                "backbone",
                "text",
                "book"
            ]
        });
    
    gulp.task('requirejsBuild', ['component-scripts', 'external-scripts'], function (cb) {
        return gulprjs(requireJsOptimizerConfig)            
            .pipe(gulp.dest('app/dist'));
    });
    
    0 讨论(0)
  • 2020-12-12 20:24

    My solution works like this:

    ./client/js/main.js:

    require.config({
        paths: {
            jquery: "../vendor/jquery/dist/jquery",
            ...
        },
        shim: {
            ...
        }
    });
    
    define(["jquery"], function($) {
        console.log($);
    });
    

    ./gulpfile.js:

    var gulp = require('gulp'),
        ....
        amdOptimize = require("amd-optimize"),
        concat = require('gulp-concat'),
        ...
    
    gulp.task('scripts', function(cb) {
    
        var js = gulp.src(path.scripts + '.js')
            .pipe(cached('scripts'))
            .pipe(jshint())
            .pipe(jshint.reporter('default'))
            .pipe(remember('scripts'))
            .pipe(amdOptimize("main",
                {
                    name: "main",
                    configFile: "./client/js/main.js",
                    baseUrl: './client/js'
                }
            ))
            .pipe(concat('main.js'));
    
            .pipe(gulp.dest(path.destScripts));
    }
    ...
    

    This part was important:

    configFile: "./client/js/main.js",
    baseUrl: './client/js'
    

    This allowed me to keep my configuration in one place. Otherwise I was having to duplicate my paths and shims into gulpfile.js.

    0 讨论(0)
  • 2020-12-12 20:31

    This works for me. I seems that one ought to add in uglification etc via gulp if desired. .pipe(uglify()) ...

    Currently I have to duplicate the config in main.js to run asynchronously.

        ....
    
        var amdOptimize = require("amd-optimize");
    
        ...
    
        var js = gulp.src(path.scripts + '.js')
            .pipe(cached('scripts'))
            .pipe(jshint())
            .pipe(jshint.reporter('default'))
            .pipe(remember('scripts'))
            .pipe(amdOptimize("main",
                {
                    name: "main",
                    paths: {
                        jquery: "client/vendor/jquery/dist/jquery",
                        jqueryColor: "client/vendor/jquery-color/jquery.color",
                        bootstrap: "client/vendor/bootstrap/dist/js/bootstrap",
                        underscore: "client/vendor/underscore-amd/underscore"
                    },
                    shim: {
                        jqueryColor : {
                            deps: ["jquery"]
                        },
                        bootstrap: {
                            deps: ["jquery"]
                        },
                        app: {
                            deps: ["bootstrap", "jqueryColor", "jquery"]
                        }
                    }
                }
    
            ))
            .pipe(concat('main.js'));
    
    0 讨论(0)
提交回复
热议问题