在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):wbyoung/gulp-define-module开源软件地址(OpenSource Url):https://github.com/wbyoung/gulp-define-module开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-define-moduleThe gulp define module plugin produces modules from an input source. It allows other plugins to offload module definition to a separate plugin. For consistency, the input source should contain a single JavaScript expression and should not contain a trailing semicolon. An example input file to create a callable module: {
start: function() {},
end: function() {},
version: "1.0"
} Transformed to CommonJS/Node ( module.exports = {
start: function() {},
end: function() {},
version: "1.0"
}; Transformed to AMD ( define([], function() {
return {
start: function() {},
end: function() {},
version: "1.0"
};
}); Transformed to ES6 ( export default {
start: function() {},
end: function() {},
version: "1.0"
}; Transformed to Hybrid ( (function(definition) {
if (typeof exports === 'object') { module.exports = definition(); } // CommonJS
else if (typeof define === 'function' && define.amd) { define([], definition); } // AMD
else { definition(); } // Browser
})(function() {
return {
start: function() {},
end: function() {},
version: "1.0"
};
}); Transformed to Plain ( {
start: function() {},
end: function() {},
version: "1.0"
}; To use the module simply include it in your gulp pipeline: var emberEmblem = require('gulp-ember-emblem');
var defineModule = require('gulp-define-module');
gulp.task('templates', function(){
gulp.src(['client/templates/*.hbs'])
.pipe(emberEmblem())
.pipe(defineModule('commonjs'))
.pipe(gulp.dest('build/templates/'));
}); APIdefineModule(type, [options])typeType: The desired output type. One of the following:
options.requireType: An object containing dependencies that should be imported for this module. This option is only
supported for The property name in the object should be the value of the variable that the dependency will be accessed from, and the property value should be the name of the dependency. For instance, CommonJS/Node var Library = Library || require('library');
module.exports = {}; AMD define(['library'], function(Library) {
return {};
}); ES6 import Library from "library"; Hybrid (function(definition) {
if (typeof exports === 'object') { module.exports = definition(require('library')); } // CommonJS
else if (typeof define === 'function' && define.amd) { define(['library'], definition); } // AMD
else { definition(Library); } // Browser
})(function(Library) {
return {};
}); options.wrapperType: Wrapper in which to wrap input modules. This wrapper will be processed through lodash.template with the following context: gulp-handlebars, for instance, sets a wrapper of options.contextType: Extend the context that's used to process the wrapper. If you pass an object, it will simply be merged with the default context. A function argument should have the signature defineModule('plain', {
wrapper: 'MyApp.templates["<%= templateName %>"] = <%= contents %>',
context: function(context) {
var file = context.file;
var name = path.relative(file.cwd, file.path)
.slice(0, -path.extname(file.path).length)
.split(path.sep).join('.');
return { templateName: name };
}
}) This will result in a template file, options.nameType: This option only works with This function receives the file path as argument and should return a
name for the defineModule('amd', {
name: function(filePath) { return "moduleName"; }
}) If no naming function is present, an
anonymous This can be used with Hogan, for example: gulp.src('client/templates/**/*.mustache')
.pipe(hoganCompiler())
.pipe(rename(function(filePath) { filePath.extname = '.mustache.js' })
.pipe(defineModule('amd', {
require: {
Hogan: 'hogan'
},
name: function(filePath) {
return filePath.split(process.cwd() + '/')[1].replace('.js', '')
}
}))
.pipe(gulp.dest('build/templates/')); This will result in the following template file: define("build/templates/path/to/template.mustache", ["hogan"], function(Hogan) { ... }) For gulp plugin developersPlugin developers can pass options on to this plugin so that users don't have to define values that may be the most common setup for modules. To do so set the For an example, see gulp-ember-emblem. LicenseThis project is distributed under the MIT license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论