
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
有一个将JavaScript代码转换成CMD格式
将Javascript转换成cmd格式之前已经有了spmjs的grunt-cmd-transport,那我为什么还要重复造这个轮子呢?原本我也是fork了一份grunt-cmd-transport,但在扩展的时候,总是觉得它包含了太多和spmjs相交融的隐形规则。我觉得这种隐形规则应该从Gruntfile或者gulpfile中去配置。当然,在开发过程中,我大量参考了grunt-cmd-transport之前代码,其中一些逻辑也做了简化。
另外,之前spmjs也提供了合并任务:grunt-cmd-concat。在我这个cmd-nice中直接也包含concat任务。
简单说来,transport就是将js代码,转换成一个理想的cmd格式;而理想的cmd格式,包含id、dependencies等;并且,配合着seajs-text,seajs还可以加载其他任何类型的文件,因此,我们的transport也需要将这些类型的文件进行预编译,即转换成JavaScript文件。
这些文件通常包括:一些模板文件(如handlebars文件)、CSS文件(.css、.less、*.scss等)。
npm install cmd-nice
usage: cmd-nice.js [-h] [-v] [--action ACTION] [--config CONFIG]
[--configFile CONFIGFILE] --input
[INPUTFILES [INPUTFILES ...]]
command line tool for cmd-nice
Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
--action ACTION action type: transport, debug, concat
--config CONFIG config for transport/debug/concat
--configFile CONFIGFILE
config file
--input [INPUTFILES [INPUTFILES ...]]
input files
比如test.js代码如下:
define(function(require, exports, module) {
var $ = require("$");
$("body").css({
color: "red"
});
});
现在我们调用API来讲它转换成CMD格式:
var fs = require("fs");
var CmdNice = require("cmd-nice");
var transportConfig = {
useCache: true,
rootPath: process.cwd(),
paths: [
process.cwd()
],
alias: {
"$": "alinw/jquery/1.8.0/jquery"
},
aliasPaths: {},
handlebars: {
id: 'alinw/handlebars/1.3.0/runtime',
knownHelpers: [
],
knownHelpersOnly: false
},
sassOptions: {},
lessOptions: {},
cssOptions: {}
};
var parser = new CmdNice.Script(transportConfig);
parser.execute({
content: fs.readFileSync("./test.js", "utf-8"),
src: fs.realpathSync("./test.js")
}).then(function(code) {
console.log(code);
});
得到结果:
define("test", ["alinw/jquery/1.8.0/jquery"], function(require, exports, module) {
var $ = require("alinw/jquery/1.8.0/jquery");
$("body").css({
color: "red"
})
});
FAQs
another cmd transport library
We found that cmd-nice demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.