简体   繁体   English

如何使Gulp在项目的根目录中读取Babel7的babel.config.js?

[英]How to make Gulp read babel.config.js of Babel7 in the root of the project?

This is my project structure: 这是我的项目结构:

Project
|-module1
|  |-package.json
|  |-gulpfile.babel.js
|-module2
|  |-package.json
|  |-gulpfile.babel.js  
|-package.json
|-scripts.js
|-babel.config.js 

As you see in root of the project I have scripts.js that exports some things. 如您在项目根目录中scripts.js ,我有scripts.js可以导出一些东西。 After reading all the information about Babel7 configuration here in the root of the project I created babel.config.js with the following content: 阅读Babel7配置中的所有信息后, 这里在我创建的项目的根babel.config.js具有以下内容:

module.exports = {
  presets: [
    ['@babel/env', {
      targets: {
        node: 'current',
        firefox: '60',
        chrome: '67',
        safari: '11.1',
      },
    }],
  ],
};

As I understand I don't need to create .babelrc in module1 and module2 as I have a babel.config.js in the root. 据我所知,我不需要创建.babelrcmodule1module2 ,因为我有一个babel.config.js根。 However, when I run gulp on module1 I see that gulp doesn't find the configuration for babel as I get: 然而,当我上运行一饮而尽module1我看到一饮而尽没有找到通天塔的配置,因为我得到:

Project/module1/gulpfile.babel.js:26
import gulp from 'gulp';
       ^^^^
SyntaxError: Unexpected identifier

Where is my mistake? 我的错误在哪里?

The only solution I found is the following - we should use gulpfile.js instead of gulpfile.babel.js and config babel manually. 我发现的唯一解决方案如下-我们应该手动使用gulpfile.js而不是gulpfile.babel.js和config babel。 I mean: 我的意思是:

in Project/module1/gulpfile.js Project/module1/gulpfile.js

require('@babel/register')({
    configFile: './../babel.config.js',
    ignore: [/node_modules/]
})
require('./tasks')

and in Project/module1/tasks.js we put all gulp tasks. 然后在Project/module1/tasks.js放入所有Project/module1/tasks.js任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM