1. grunt-commonizor
plugin for building js file to support commonjs
grunt-commonizor
Package: grunt-commonizor
Last modified: Sat, 18 Jun 2022 14:40:18 GMT
Version: 0.1.1
Downloads: 15

Install

npm install grunt-commonizor
yarn add grunt-commonizor

@ali/grunt-commonizor

plugin for building js file to support commonjs

最新版本

0.3.9

Getting Started

安装本插件:

 tnpm install @ali/grunt-commonizor --save-dev

如果没有安装tnpm请参考这里.

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

 grunt.loadNpmTasks('grunt-commonizor');

The "commonizor" task

Overview

本grunt插件根据配置将对应文件(比如env.js)输出为.common.js文件(比如env.common.js),输出后的文件可以通过commonJS规范的require方法引入。配合tnpm publish命令打包发布到tnpm上:

 tnpm publish

发布到tnpm以后可以直接通过以下方式引用该组件了:

 var env = require('@ali/lib-env');

Options

  • namespace: 发布到哪个名字空间(可选,值可以是'@ali'/'@alife'等,默认为'@ali')
  • name: 组件名字(可选,请按照组件命名规范命名,默认从package.json的name字段解析)
  • type: 组件类型(可选,值可以是lib/ctrl,默认从package.json的name字段解析)
  • pkg: 组件的package.json对象(可选,默认为根目录下的package.json)

使用方法

在gruntfile.js的initConfig配置参数中加入以下配置:

 // 方式一:合并多个文件到一个common.js文件
commonizor: {
	js: {
	  options: {
      namespace: '@alife'
	  },
	  files: {
      '<%=distPath%>/helloamfe.common.js': [
          '<%=distPath%>/helloamfe.js',
          '<%=distPath%>/helloamfe_css.js'
        ]
	  }
  }
}

// 方式二:每个文件生成单独的common.js文件
commonizor: {
  js: {
    files: [{
      expand: true,
      cwd: '<%=distPath%>',
      src: ['a.js', 'b.js'],
      dest: '<%=distPath%>',
      ext: '.common.js'
    }]
  }
}

注意:0.3.0之前版本升级到当前版本后,gruntfile配置方式需要改为上面的方式

然后执行commonizor任务:

 grunt.loadNpmTasks('@ali/grunt-commonizor');

grunt.registerTask('common', ['commonizor']);

在命令行里运行下面的命令即可打包出xxx.common.js文件。该文件支持commonjs依赖加载标准。
请注意如果写入权限不够需要在命令前面加上sudo,这样才有权限把依赖写入到package.json的dependencies字段

 sudo grunt common

Release History

0.3.10

  • 修复对type-foo-bar命名规范的支持

0.3.9

  • 修复module.exports组件类型不正确的问题

0.3.7

  • 兼容不包含app/ctrl/lib的组件名

0.3.5

  • bugfix: 兼容名字中带有-或者.的组件名

0.3.2

  • bugfix: 多文件时module.exports只对主文件输出(其他文件通过global方式挂接)

0.3.1

  • 修正combo某些字段不应列入依赖的bug

0.3.0

  • 多个文件打包成一个commonjs文件
  • 支持ctrl组件的_css.js文件打包

0.2.15

  • bugfix: dependencies包版本兼容

0.2.12

  • 支持多文件、多任务

0.2.10

  • 打包.common.js文件
  • 依赖写入package.json文件
  • 自动把main、publishConfig写入package.json文件
  • 自动替换package.json的name为@ali/xx-xx的形式

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code