如何从package.json配置uglifyjs

时间:2022-09-28 07:17:12

Currently, when developing Wordpress themes I use a simple batch file to uglify my js. An example batch file makebundle.bat

目前,在开发Wordpress主题时,我使用一个简单的批处理文件来uglify我的js。一个示例批处理文件makebundle.bat

call uglifyjs^
  src/file1.js^
  src/file2.js^
  -cmo bundle.min.js

I then use watch to build it like this

然后我用手表来构建它

watch makebundle src

All very simple. Now, I'd like to make this a less Windows-specific process. For the reasons outlined here I don't want to use Grunt / Gulp, and was thinking of trying to use npm as a build tool. The only trouble is, I can't find out how to configure uglifyjs from within package.json

一切都很简单。现在,我想使这个特定于Windows的过程更少。由于这里概述的原因,我不想使用Grunt / Gulp,并且正在考虑尝试使用npm作为构建工具。唯一的麻烦是,我无法找到如何从package.json中配置uglifyjs

edit

Here's an example of something I'd like to work in package.json:

这是我想在package.json中工作的一个例子:

{
  "uglifyConfig": [
    {
      "outfile": "bundle.min.js,
      "files": [
        "src/file1.js",
        "src/file2.js"
      ]
      "config": {
        "mangle": true,
        "compress": true
      }
    }
  ]
}

2 个解决方案

#1


3  

If your build script is a node script, you can use Uglify's JavaScript API instead of the command-line API. You can easily require() your package.json, read configuration from it, and pass those values to Uglify.

如果构建脚本是节点脚本,则可以使用Uglify的JavaScript API而不是命令行API。你可以轻松地要求()你的package.json,从中读取配置,并将这些值传递给Uglify。

package.json:

{
  ...
  "scripts": {
    "ugly": "node do-uglify.js"
  }
  ...
}

do-uglify.js:

var uglify = require('uglify');
var package = require('./package.json');
var uglifyConfig = package.uglifyConfig;
// Call the UglifyJS Javascript API, passing config from uglifyConfig

#2


0  

You can put any scripts you want in the "scripts" section of package.json.

您可以将所需的任何脚本放在package.json的“scripts”部分中。

{
  "name": "my-package",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "ugly": "uglify",
    "prepublish" : "uglify"
  },
...

You can give it any arbitrary name and run it with npm run ugly or use one of the predefined hooks such as prepublish

您可以给它任意名称并使用npm run ugly运行它或使用预定义的钩子之一,例如prepublish

#1


3  

If your build script is a node script, you can use Uglify's JavaScript API instead of the command-line API. You can easily require() your package.json, read configuration from it, and pass those values to Uglify.

如果构建脚本是节点脚本,则可以使用Uglify的JavaScript API而不是命令行API。你可以轻松地要求()你的package.json,从中读取配置,并将这些值传递给Uglify。

package.json:

{
  ...
  "scripts": {
    "ugly": "node do-uglify.js"
  }
  ...
}

do-uglify.js:

var uglify = require('uglify');
var package = require('./package.json');
var uglifyConfig = package.uglifyConfig;
// Call the UglifyJS Javascript API, passing config from uglifyConfig

#2


0  

You can put any scripts you want in the "scripts" section of package.json.

您可以将所需的任何脚本放在package.json的“scripts”部分中。

{
  "name": "my-package",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "ugly": "uglify",
    "prepublish" : "uglify"
  },
...

You can give it any arbitrary name and run it with npm run ugly or use one of the predefined hooks such as prepublish

您可以给它任意名称并使用npm run ugly运行它或使用预定义的钩子之一,例如prepublish