如何在GitHub Action中使Babel工作?

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

How To Get Babel Working In A Github Action?

问题

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

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

  1. Run npx babel ./src/localization --out-dir ./.github/workflows/babel-output
  2. 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,错误如下:

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

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

workflow yml文件如下:

  1. name: Compare Localization Keys
  2. on:
  3. pull_request:
  4. branches:
  5. - localization_keys_comparison2
  6. push:
  7. branches:
  8. - localization_keys_comparison2
  9. jobs:
  10. compare_keys:
  11. name: Compare Localization Keys
  12. runs-on: ubuntu-latest
  13. steps:
  14. - name: Get Code
  15. uses: actions/checkout@v3
  16. - name: Setup Node
  17. uses: actions/setup-node@v3
  18. with:
  19. node-version: "18"
  20. - name: Add dependency
  21. run: yarn add @actions/core
  22. - name: Add dependency
  23. run: yarn add flat
  24. #它需要babel ^7 - 检测到6.xx
  25. - name: Uninstall Wrong Babel Version
  26. run: npm uninstall -g babel
  27. #- name: Install Babel 7
  28. #run: yarn add babel@^7.0.0
  29. #run: npm install -g --legacy-peer-deps babel@^7.0.0-0
  30. - name: Install Babel Cli
  31. #run: yarn add --global babel-cli
  32. run: npm install --legacy-peer-deps --global babel-cli
  33. - name: Transpile JavaScript with Babel
  34. run: npx babel ./src/localization --out-dir ./.github/workflows/babel-output
  35. - name: Run comparison javascript file
  36. run: node --trace-warnings --experimental-modules ./.github/compare_localization_keys.js

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

.babelrc文件如下:

  1. {
  2. "presets": ["@babel/preset-env"]
  3. }

en.js文件如下:

  1. export const en = {
  2. common: {
  3. loading: "Loading...",
  4. //.. 其他对象属性
  5. },
  6. };

总体目标是创建一个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:

  1. Run npx babel ./src/localization --out-dir ./.github/workflows/babel-output
  2. Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If you are sure you have a
  3. 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:

  1. Run npm install -g --legacy-peer-deps babel@^7.0.0-0
  2. npm ERR! code ETARGET
  3. npm ERR! notarget No matching version found for babel@^7.0.0-0.
  4. 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:

  1. name: Compare Localization Keys
  2. on:
  3. pull_request:
  4. branches:
  5. - localization_keys_comparison2
  6. push:
  7. branches:
  8. - localization_keys_comparison2
  9. jobs:
  10. compare_keys:
  11. name: Compare Localization Keys
  12. runs-on: ubuntu-latest
  13. steps:
  14. - name: Get Code
  15. uses: actions/checkout@v3
  16. - name: Setup Node
  17. uses: actions/setup-node@v3
  18. with:
  19. node-version: "18"
  20. - name: Add dependency
  21. run: yarn add @actions/core
  22. - name: Add dependency
  23. run: yarn add flat
  24. #it needs babel ^7 - 6.xx detected
  25. - name: Uninstall Wrong Babel Version
  26. run: npm uninstall -g babel
  27. #- name: Install Babel 7
  28. #run: yarn add babel@^7.0.0
  29. #run: npm install -g --legacy-peer-deps babel@^7.0.0-0
  30. - name: Install Babel Cli
  31. #run: yarn add --global babel-cli
  32. run: npm install --legacy-peer-deps --global babel-cli
  33. - name: Transpile JavaScript with Babel
  34. run: npx babel ./src/localization --out-dir ./.github/workflows/babel-output
  35. - name: Run comparison javascript file
  36. 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:

  1. {
  2. "presets": ["@babel/preset-env"]
  3. }

the en.js file is:

  1. export const en = {
  2. common: {
  3. loading: "Loading...",
  4. //.. other object properties
  5. },
  6. };

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
  • action
  • babeljs
  • github
  • javascript

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

确定