如何在GitHub Action中使Babel工作?

huangapple go评论112阅读模式
英文:

How To Get Babel Working In A Github Action?

问题

我试图运行一个GitHub Action,该Action运行JavaScript ES6代码。以前,我在目标JavaScript文件(en.js)中的'export'关键字上遇到了一个'unexpected token'错误,所以我尝试让Babel首先将其转译。

在工作流中成功加载了babel-cli后,出现了以下错误:

Run npx babel ./src/localization --out-dir ./.github/workflows/babel-output
Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version.

如果我尝试安装Babel 7,错误如下:

Run npm install -g --legacy-peer-deps babel@^7.0.0-0
npm ERR! code ETARGET
npm ERR! notarget No matching version found for babel@^7.0.0-0.
npm ERR! notarget In most cases you or one of your dependencies are requesting a package version that doesn't exist.

有人可以提出继续的方法吗?

workflow yml文件如下:

name: Compare Localization Keys
on:
  pull_request:
    branches:
      - localization_keys_comparison2
  push:
    branches:
      - localization_keys_comparison2

jobs:
  compare_keys:
    name: Compare Localization Keys
    runs-on: ubuntu-latest
    steps:
      - name: Get Code
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: "18"

      - name: Add dependency
        run: yarn add @actions/core

      - name: Add dependency
        run: yarn add flat

        #它需要babel ^7 - 检测到6.xx
      - name: Uninstall Wrong Babel Version
        run: npm uninstall -g babel

      #- name: Install Babel 7
      #run: yarn add babel@^7.0.0
      #run: npm install -g --legacy-peer-deps babel@^7.0.0-0

      - name: Install Babel Cli
        #run: yarn add --global babel-cli
        run: npm install --legacy-peer-deps --global babel-cli

      - name: Transpile JavaScript with Babel
        run: npx babel ./src/localization --out-dir ./.github/workflows/babel-output

      - name: Run comparison javascript file
        run: node --trace-warnings --experimental-modules ./.github/compare_localization_keys.js

compare_localization_keys.js文件(上面)引用了babel-output文件夹中预期的已转译的en.js文件,但我们还没有在工作流中成功完成这一步。

.babelrc文件如下:

{
  "presets": ["@babel/preset-env"]
}

en.js文件如下:

export const en = {
  common: {
    loading: "Loading...",
    //.. 其他对象属性
  },
};

总体目标是创建一个GitHub Action,用于比较两个JavaScript文件中的两个对象。

如有评论或建议,将不胜感激。

英文:

I'm trying to run a GitHub Action which runs javascript ES6 code. Previously, I
was getting an 'unexpected token' error on the 'export' keyword in the target javascript file (en.js), so I tried getting Babel to transpile it first.

With babel-cli successfully loaded in the workflow, the Action error is:

Run npx babel ./src/localization --out-dir ./.github/workflows/babel-output
Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a 
a compatible version of @babel/core, it is likely that something in your build process is loading the wrong version.

and if I try to install Babel 7, the error is:

Run npm install -g --legacy-peer-deps babel@^7.0.0-0
npm ERR! code ETARGET
npm ERR! notarget No matching version found for babel@^7.0.0-0.
npm ERR! notarget In most cases you or one of your dependencies are requesting a package version that doesn't exist.

Can anyone suggest a way to proceed?

the workflow yml file is:

name: Compare Localization Keys
on:
  pull_request:
    branches:
      - localization_keys_comparison2
  push:
    branches:
      - localization_keys_comparison2

jobs:
  compare_keys:
    name: Compare Localization Keys
    runs-on: ubuntu-latest
    steps:
      - name: Get Code
        uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: "18"

      - name: Add dependency
        run: yarn add @actions/core

      - name: Add dependency
        run: yarn add flat

        #it needs babel ^7 - 6.xx detected
      - name: Uninstall Wrong Babel Version
        run: npm uninstall -g babel

      #- name: Install Babel 7
      #run: yarn add babel@^7.0.0
      #run: npm install -g --legacy-peer-deps babel@^7.0.0-0

      - name: Install Babel Cli
        #run: yarn add --global babel-cli
        run: npm install --legacy-peer-deps --global babel-cli

      - name: Transpile JavaScript with Babel
        run: npx babel ./src/localization --out-dir ./.github/workflows/babel-output

      - name: Run comparison javascript file
        run: node --trace-warnings --experimental-modules ./.github/compare_localization_keys.js

The compare_localization_keys.js file (above) references the intended transpiled en.js file in the babel-output folder, but we haven't successfully gotten that far in the workflow yet.

the .babelrc file is:

{
  "presets": ["@babel/preset-env"]
} 

the en.js file is:

export const en = {
  common: {
    loading: "Loading...",
//.. other object properties
  },
};

The overall aim is to create a GitHub Action that compares two objects from JavaScript files.

Any comments or suggestions would be very gratefully received.

答案1

得分: 0

如果你在 package.json 中设置了 type: module 并且没有使用任何非标准语法,就不需要使用 Babel。

如果你确实想要使用 Babel,babel 包已被弃用,没有 v7 版本。请使用 @babel/cli@babel/core@babel/preset-env@babel/cli 提供了命令(npx babel src ...等等)。尽量确保它们都是相似版本(例如 npm i @babel/cli@latest @babel/core@latest @babel/preset-env@latest,或者 @babel/cli@7.22.10 等等)。

英文:

There's no need for Babel if you set type: module in your package.json and aren't using any non-standard syntax.

If you do want to babelify, the babel package is deprecated, there is no v7. Use @babel/cli, @babel/core, and @babel/preset-env. @babel/cli provides the command (npx babel src ...etc). Aim to have them all on a similar version (for example npm i @babel/cli@latest @babel/core@latest @babel/preset-env@latest, or @babel/cli@7.22.10, and so on).

huangapple
  • 本文由 发表于 2023年8月10日 12:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定