node.js - Babel 不会忽略 node_modules 目录,尽管它在 "ignore"配置中

标签 node.js compression minify babeljs

由于某些原因 babel 不会忽略 node_modules 目录,尽管我在 .babelrc 的 "ignore" 字段中指定了它 文件。为什么会这样?如何让 babel 像预期的那样运行?

我的目标是在我将我的应用程序推送到远程仓库然后再推送到服务器之前压缩和破坏我的 ExpressJS 应用程序中的所有 .js 文件(特别是我的所有后端代码)。所以我使用 babelbabili

这是我的 .babelrc 配置:

{
    "presets": [
        ["latest", {
            "modules": false
        }]
    ],
    "env": {
        "development": {
            "presets": ["stage-0", "react", "babili"]
        },
        "production": {
            "presets": ["stage-0", "react", "babili"]
        }
    },
    "ignore": [
        "node_modules",
        "assets",
        "view",
        "public",
        "test",
        "spec",
        "logs",
        "lib/jasmine_examples",
        "db"
    ]
}

然后我像这样从命令行运行 babel:

./node_modules/.bin/babel . -d ~/app_compressed/

babel开始压缩node_modules目录:

node_modules\apache-crypt\gensrc\index.js -> C:\Users\user\app_compressed\node_modules\apache-crypt\gensrc\index.js
node_modules\apache-md5\gensrc\index.js -> C:\Users\user\app_compressed\node_modules\apache-md5\gensrc\index.js
node_modules\babel-preset-env\data\built-in-features.js -> C:\Users\user\app_compressed\node_modules\babel-preset-env\data\built-in-features.js
node_modules\babel-preset-env\data\plugin-features.js -> C:\Users\user\app_compressed\node_modules\babel-preset-env\data\plugin-features.js
node_modules\babel-preset-env\lib\default-includes.js -> C:\Users\user\app_compressed\node_modules\babel-preset-env\lib\default-includes.js
node_modules\babel-preset-env\lib\index.js -> C:\Users\user\app_compressed\node_modules\babel-preset-env\lib\index.js

从字面上看是错误的行为。如何解决?如何让 babel 忽略配置中指定的文件夹?

最佳答案

忽略获取正则表达式数组,所以尝试这样

ignore: [
 /node_modules/,
 ...,
]

或者你可以像这样传递一个回调函数

ignore: [
   /node_modules/,
   function(filepath) {
      return filepath !== "/path/to/es6-file.js";
   },
]

关于node.js - Babel 不会忽略 node_modules 目录,尽管它在 "ignore"配置中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42980116/

相关文章:

c++ - 如何高效解压哈夫曼编码文件

javascript - 压缩/缩小 javascript 文件的方法

javascript - 通过压缩 css 和 js 编译 Visual Studio 元素

javascript - 即时压缩 css 和 js

android - 使用 algolia 在 firestore 中进行全文搜索

algorithm - 他们使用什么技术来压缩 URL?

mysql - 升级 Knex 后出现 "Timeout acquiring a connection"

video - H.264 编码视频中的显示顺序语法

javascript - 使用Object.create和toString.call()返回 '[object Rectangle]'

javascript - 如何使用 node.js 向 Google Drive API 发出 3000 个请求而不超过用户速率限制?