项目作者: hezedu

项目描述 :
简单的将CommonJS代码转成前端直接能src的代码,支持amd, global加载。
高级语言: JavaScript
项目地址: git://github.com/hezedu/dist-wrap.git
创建时间: 2017-04-06T07:06:21Z
项目社区:https://github.com/hezedu/dist-wrap

开源协议:MIT License

下载


dist-wrap

简单的将CommonJS代码转成amd加载或前端直接能 src 的代码。

只适合单文件。

安装

npm install dist-wrap --save-dev

特性

  • 最少的包裹--只有一层。
  • 头部注释不动。
  • 没有module.exports判定,支持CommonJS为啥还用转呢?是吧。

    API

    wrap(source)

    wrap(source, globalName)

    必须 在源文件中包含一行 module.exports = someModule

比如源source:

  1. /* hello */
  2. var hello = 'hello';
  3. module.exports = hello; //这个必须有

执行

  1. var wrap = require('dist-wrap');
  2. var result = wrap(source);
  3. conosle.log(result.headerNote + result.code);

结果:

  1. /* hello */
  2. (function(){
  3. //dist-wrap top
  4. var hello = 'hello';
  5. //module.exports = hello;
  6. //dist-wrap bottom
  7. if(typeof define === 'function' && define.amd) {
  8. define(function(){
  9. return hello;
  10. });
  11. }else{
  12. this.hello = hello; //this是全局,.hello可以自定义,见下文
  13. }
  14. })();

自定义全局命名

如果想要绑定到全局的名字不一样,你可以用第二个参数:

  1. wrap(source, 'HELLO');