GithubHelp home page GithubHelp logo

jorrit / gulp-requirejs Goto Github PK

View Code? Open in Web Editor NEW
60.0 5.0 41.0 455 KB

A small, simple, very easy wrapper around the require.js optimizer to work with gulp.js

License: MIT License

JavaScript 95.18% HTML 4.82%
gulp javascript requirejs

gulp-requirejs's Introduction

gulp-requirejs

gulp-requirejs on npm Build Status Coverage

Information

A small, simple, very easy wrapper around the require.js optimizer to work with gulp.js.

Packagegulp-requirejs
Description uses require.js's r.js optimizer to combine require.js AMD modules into one file
Node Version ≧ 4

Installation

Simply add gulp-requirejs as a dev-dependency in your package.json or run

$ npm install --save-dev gulp-requirejs

Usage

Because the require.js optimizer (r.js) is a kind of build system in itself we can't use the gulp.src([...]) syntax at the moment (I might add this in future), instead this wrapper itself emits a pipable stream, holding a 'virtual' file, in which the result of the r.js build process are saved.

The resulting stream can be treated like a regular gulp.src(...) stream.

NOTE: The built in minification/obfuscation is deactivated by default. It is recommended to use a gulp plugin like gulp-uglify for minification, but you can enable r.js minification by setting the optimize option to uglify to minify using r.js.

var gulp = require('gulp'),
    rjs = require('gulp-requirejs');

gulp.task('requirejsBuild', function() {
    return rjs({
        baseUrl: 'root/directory/of/js/files/',
        out: 'FILENAME_TO_BE_OUTPUTTED',
        name: 'mainfile', // no extension
        shim: {
            // standard require.js shim options
        },
        // ... more require.js options
    })
    .pipe(gulp.dest('./deploy/')); // pipe it to the output DIR
});

If you use instead of out the dir option, you do not need the pipe at all, see this example in Gulp 4 syntax and mocha test:

const rjs = require('gulp-requirejs');

async function requirejsBuild(cb) {
    return rjs({
        dir: 'deploy',
        mainConfigFile: 'config.js',
        path: {
          'config': '../config_init'
        },
        modules: [{
          name: 'FILENAME_TO_BE_OUTPUTTED', // no extension
          include  : [ .. ]
         ...
        }]
    }) ...
};
    
exports.requirejsBuild = requirejsBuild;

Note: In order to let gulp know that the optimization completes, return the rjs stream.

See requirejs.org for more information about the supported parameters.

Error handling

gulp-requirejs will emit errors when you don't pass an options object, if the baseUrl or out properties are undefined or when the requirejs optimizer detects an error.

Source maps

When source maps are enabled via the r.js generateSourceMaps option the file in the stream rjs() contains a sourceMap property with the sourcemap as an object.

Use gulp-sourcemaps to process this object in your gulp configuration.

var gulp = require('gulp'),
    rjs = require('gulp-requirejs')
    sourcemaps = require('gulp-sourcemaps');

gulp.task('requirejsBuild', function() {
    return rjs({
        baseUrl: 'root/directory/of/js/files/',
        out: 'FILENAME_TO_BE_OUTPUTTED',
        name: 'mainfile', // no extension
        generateSourceMaps: true,
        shim: {
            // standard require.js shim options
        },
        // ... more require.js options
    })
    .pipe(sourcemaps.init({loadMaps: true})) // initialize gulp-sourcemaps with the existing map
    .pipe(sourcemaps.write()) // write the source maps
    .pipe(gulp.dest('./deploy/')); // pipe it to the output DIR
});

Options

The options object supports the same parameters as the require.js optimizer.

gulp-requirejs's People

Contributors

docnoe avatar eemeli avatar greenkeeper[bot] avatar greenkeeperio-bot avatar jorrit avatar joshje avatar leog avatar t3easy avatar tomsun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

gulp-requirejs's Issues

ReferenceError: Can't find variable: require

What did I do wrong? Run gulp did not have the error message, but the browser complained "ReferenceError: Can't find variable: require"

gulp.task('requirejs',function(cb){
  return rjs({
    baseUrl: 'js/user.js',
    out: 'user.js',
    shim: {
        // standard require.js shim options
    },
    // ... more require.js options
  })
  .pipe(gulp.dest('js'));
}),

gulp.task('minifyjs',function(cb){
  return gulp.src('js/*.js')
  .pipe(process.env.NODE_ENV === 'development' ? sourcemaps.init() : noop()) 
  //.pipe(sourcemaps.init())
  .pipe(stripDebug())
  .pipe(concat('bundle.js'))
  .pipe(uglify())
  .pipe(rename({suffix: '.min'}))
  .pipe(process.env.NODE_ENV === 'development' ? sourcemaps.write() : noop()) 
  //.pipe(sourcemaps.write())
  .pipe(gulp.dest('../node/public/user/js'))  
})

gulp.task('default', function (cb) {
  gulpSequence(
    'clean',
    ['css', 'requirejs','minifyjs', 'minify_script','copyHtml'],
    function () {
      log('success');
    });
})

An in-range update of eslint is breaking the build 🚨

The devDependency eslint was updated from 5.14.0 to 5.14.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v5.14.1
  • 1d6e639 Fix: sort-keys throws Error at SpreadElement (fixes #11402) (#11403) (Krist Wongsuphasawat)
Commits

The new version differs by 3 commits.

  • b2e94d8 5.14.1
  • ce129ed Build: changelog update for 5.14.1
  • 1d6e639 Fix: sort-keys throws Error at SpreadElement (fixes #11402) (#11403)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

gulp-load-plugins

in gulpfile.js :
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
can I use this plugin like this ?
$.requirejs({OPTIONS})

Doesn't seem to work in my environment

This sounds like the vaguest bug report, but I'm scratching my head at why the plugin doesn't seem to do anything for me. I've setup a super basic test with gulp-debug, all the files are in the root of a folder. Here we go:

package.json

{
  "name": "test",
  "version": "1.0.0",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-debug": "^4.0.0",
    "gulp-requirejs": "^1.2.0"
  }
}

gulpfile.js

const gulp = require('gulp');
const debug = require('gulp-debug');
const rjs = require('gulp-requirejs');

gulp.task('requirejsBuild', function() {
    return rjs({
        baseUrl: 'main.js',
        out: 'main.min.js'
    })
    .pipe(debug())
    .pipe(gulp.dest('./deploy/'));
});

main.js

define('multiply', [], function() {
    return function(a, b) {
        return a * b;
    };
});

requirejs(['multiply'], function(multiply) {
    console.log('multiply:', multiply(3, 5));
});

Now, I don't get any output in a ./deploy/ directory, nor does gulp-debug log any files passing in the stream. I can enter any name in baseUrl (absolute paths, relative paths and non-existing paths), but it never seem to throw errors or give any output.

My node version is 10.11.0

Any idea what could be going on here?

Cheers, Thijs

This plugin does not trigger end event correctly

A possible solution is to add end here at about line 40:

    // try {
        optimize(opts, function(text) {
            _s.write(new File({
                path: _fName,
                contents: new Buffer(text)
            }));
            _s.end() // <--- end state
        });
    // } catch (err) {
    //     _s.emit('error', err);
    // }

rewirte config out to function has a bug

code:
// Set .out to a function to catch result text and sourcemap.
opts.out = function(text, sourceMap) {
output = text;
sourceMapOutput = sourceMap;
};

when i use gulp-requirejs and require-css, at require-css/css-builder.js line 130,
code:
siteRoot = path.resolve(config.dir || path.dirname(config.out), config.siteRoot || '.') + '/';

get an error TypeError: Path must be a string, becuse confg.out is a function.

baseURL

My app is hosted on cdn with certain url. For example http://cnd.com/app
in main.js
require.config({
baseUrl: http://cnd.com/app',
...

When i use this plugin i set the baseURL to local files
rjs({
baseUrl: "app",
mainConfigFile: mainPath,
out: "main.built.js",
include: include
})

So, after it, i get rewrited cdn baseURL to local baseURL.

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml
  • The new Node.js version is in-range for the engines in 1 of your package.json files, so that was left alone

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Outputting compiled files outside of gulpfile folder

Hi,

This is not so much a bug but I am trying to compile the files outside of the project folder however a 'mkdir' error appears, i'm assuming this is a precaution to prevent files outside of the project from accidentally being overridden. Is there an option i can use to allow me to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.