Giter Club home page Giter Club logo

ranma's Introduction

A converter between CommonJS/AMD/CMD/Module/other

ranma取自同名动漫人物乱马,意指同一个事物的不同形式。

NPM version Build Status Coverage Status Dependency Status

为满足所写的代码能同时运行于server环境和web环境,而不需手动修改,所以做了个转换方法,使得几者之间的模块能够互相等价转化。

需要注意的是AMD模块的写法应遵守文件和模块一对一的原则。

INSTALL

npm install ranma

API

  • cjsify(code:String):String

    • 将代码转换为CommonJS
    • 对于AMDCMD,会将define的factory提取,改写return为module.exports并删除define,如果define父语句有if判断也会删除
    • 对于es6 module,会将module和import变为require,export变为exports,export default变为module.exports
    • 对于普通文件,会将全局声明的变量作为exports,全局使用的未声明变量作为require
  • amdify(code:String):String

    • 将代码转换为AMD
    • 在cjsify的基础上进行define包裹
    • 如果代码是CMD不做修改,因为AMD兼容这种写法
  • cmdify(code:String):String

    • 将代码转换为CMD
    • 在cjsify的基础上进行define包裹
    • 如果code是AMD类型,会进行依赖转化——即将factory的参数改为CMD的require, exports, module固定参数,同时依赖变为require变量声明
  • type.isCommonJS(code:String):Boolean

    • code是否是CommonJS
  • type.isAMD(code:String):Boolean

    • code是否是AMD
  • type.isCMD(code:String):Boolean

    • code是否是CMD
  • type.isModule(code:String):Boolean

    • code是否是es6 module

AMD和CMD的区分依据

  • 是否出现define.amd的判断
  • factory的参数是否为固定的require, exports, module

License

[MIT License]

ranma's People

Contributors

army8735 avatar zongwei007 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ranma's Issues

尝试转换zrender的js为CommonJS时,部分文件报错

https://github.com/ecomfe/zrender/blob/master/src/dep/excanvas.js

    if(ast.first() && ast.first().name() == Es6Node.MODULEBODY) {
           ^
TypeError: Object #<Object> has no method 'first'
    at Object.exports.analyse (e:\code\zrender\node_modules\ranma\type.js:94:12)
    at Object.exports.isCommonJS (e:\code\zrender\node_modules\ranma\type.js:119:11)
    at modify.fileModifier (e:\code\zrender\gulpfile.js:10:29)
    at DestroyableTransform._transform (e:\code\zrender\node_modules\gulp-modify\index.js:18:21)
    at DestroyableTransform.Transform._read (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_st
eam_transform.js:184:10)
    at DestroyableTransform.Transform._write (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_s
ream_transform.js:172:12)
    at doWrite (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_str
am_writable.js:194:11)
    at write (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:623:24)
    at flow (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:632:7)
    at DestroyableTransform.pipeOnReadable (e:\code\zrender\node_modules\through2\node_modules\readable-stream\lib\_str
am_readable.js:664:5)

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.