• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jlouns/gulp-requirejs-optimize: Wrapper for the requirejs optimizer for use in g ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

jlouns/gulp-requirejs-optimize

开源软件地址(OpenSource Url):

https://github.com/jlouns/gulp-requirejs-optimize

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

gulp-requirejs-optimize

Build Status npm version Coverage Status Dependency Status

Optimize AMD modules in javascript files using the requirejs optimizer.

Install

$ npm install --save-dev gulp-requirejs-optimize

Usage

Simple

var gulp = require('gulp');
var requirejsOptimize = require('gulp-requirejs-optimize');

gulp.task('scripts', function () {
	return gulp.src('src/main.js')
		.pipe(requirejsOptimize())
		.pipe(gulp.dest('dist'));
});

Custom options

gulp-requirejs-optimize accepts almost all of the same options as r.js optimize (see below).

var gulp = require('gulp');
var requirejsOptimize = require('gulp-requirejs-optimize');

gulp.task('scripts', function () {
	return gulp.src('src/main.js')
		.pipe(requirejsOptimize({
			optimize: 'none',
			insertRequire: ['foo/bar/bop'],
		}))
		.pipe(gulp.dest('dist'));
});

Multiple Modules

Each file passed to the plugin is optimized as a separate module.

var gulp = require('gulp');
var requirejsOptimize = require('gulp-requirejs-optimize');

gulp.task('scripts', function () {
	return gulp.src('src/modules/*.js')
		.pipe(requirejsOptimize())
		.pipe(gulp.dest('dist'));
});

Options generating function

Options can also be specified in the form of an options-generating function to generate custom options for each file passed. This can be used to apply custom logic while optimizing multiple bundles or modules in an app.

var gulp = require('gulp');
var requirejsOptimize = require('gulp-requirejs-optimize');

gulp.task('scripts', function () {
	return gulp.src('src/modules/*.js')
		.pipe(requirejsOptimize(function(file) {
			return {
				name: '../vendor/bower/almond/almond',
				optimize: 'none',
				useStrict: true,
				baseUrl: 'path/to/base',
				include: 'subdir/' + file.relative
			};
		}))
		.pipe(gulp.dest('dist'));
});

Sourcemaps support

The plugin supports gulp-sourcemaps only if preserveLicenseComments is set to false, as described in the r.js docs. If preserveLicenseComments is not specified and the gulp-sourcemaps plugin is detected, the plugin will automatically set preserveLicenseComments to false.

var gulp = require('gulp');
var requirejsOptimize = require('gulp-requirejs-optimize');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('scripts', function () {
	return gulp.src('src/main.js')
		.pipe(sourcemaps.init())
		.pipe(requirejsOptimize())
		.pipe(sourcemaps.write())
		.pipe(gulp.dest('dist'));
});

API

requirejsOptimize(options)

options

Options are the same as what is supported by the r.js optimizer except for out, modules, and dir.

The options parameter can be specified as a static object or an options-generating function. Options-generating functions are passed a file object and are expected to generate an options object.

Differences From r.js

out

r.js supports out as a string describing a path or a function which processes the output. Since we need to pass a virtual file as output, we only support the string version of out.

modules and dir

r.js supports an array of modules to optimize multiple modules at once, using the dir parameter for the output directory. The same thing can be accomplished with this plugin by passing the main file of each module as input to the plugin. gulp.dest can be used to specify the output directory.

This means an r.js config file for optimizing multiple modules that looks like this:

{
	"baseUrl": "src/modules",
	"dir": "dist",
	"modules": [{
		"name": "one"
	}, {
		"name": "two"
	}]
}

Would look like this as a gulp task with this plugin:

gulp.task('scripts', function () {
	return gulp.src('src/modules/*.js')
		.pipe(requirejsOptimize())
		.pipe(gulp.dest('dist'));
});

License

MIT © Jonathan Lounsbury




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap