简体   繁体   English

使用 Svelte + Vite 应用程序加载 babel.config 错误

[英]Loading babel.config error with Svelte + Vite app

I'm building a new app with Svelte and Vite and I would like to use a jest test with it.我正在用 Svelte 和 Vite 构建一个新的应用程序,我想用它做一个笑话测试。 I configured it as usual but I have this information about an error:我像往常一样配置它,但我有以下关于错误的信息:

tests/App.spec.js
  ● Test suite failed to run

    /Users/ewakadziolka/Desktop/svelte/aloes/babel.config.js: Error while loading config - You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.

      at loadCjsOrMjsDefault (node_modules/@babel/core/lib/config/files/module-types.js:82:13)
          at loadCjsOrMjsDefault.next (<anonymous>)
      at readConfigJS (node_modules/@babel/core/lib/config/files/configuration.js:215:47)
          at readConfigJS.next (<anonymous>)
      at Function.<anonymous> (node_modules/@babel/core/lib/gensync-utils/async.js:27:3)
      at evaluateSync (node_modules/gensync/index.js:251:28)
      at Function.sync (node_modules/gensync/index.js:89:14)
      at sync (node_modules/@babel/core/lib/gensync-utils/async.js:78:25)
      at sync (node_modules/gensync/index.js:182:19)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.681 s
Ran all test suites related to changed files.

Watch Usage: Press w to show more.

An App.一个应用程序。 svelte for a moment is very easy:苗条一会儿很容易:

<main>
  <h1>Hello word!</h1>
</main>

my babel.congig.js is like this:我的 babel.congig.js 是这样的:

module.exports = { presets: [["@babel/preset-env", { targets: { node: "current" } }]],  
};

and my package.json like this:我的 package.json 是这样的:

{
  "name": "aloes",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "test": "jest --watch"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.18.9",
    "@sveltejs/vite-plugin-svelte": "^1.0.1",
    "@testing-library/dom": "^8.16.0",
    "@testing-library/svelte": "^3.1.3",
    "@testing-library/user-event": "^14.2.6",
    "babel-jest": "^28.1.3",
    "jest": "^28.1.3",
    "jest-environment-jsdom": "^28.1.3",
    "svelte": "^3.49.0",
    "svelte-jester": "^2.3.2",
    "vite": "^3.0.0"
  },
  "jest": {
    "transform": {
      "^.+\\.svelte$": "svelte-jester",
      "^.+\\.js$": "babel-jest"
    },
    "testEnvironment": "jsdom"
  }
}

This configuration worked very well when I used rollup to build an app.当我使用 rollup 构建应用程序时,此配置运行良好。

I found a solution.我找到了解决方案。 I changed babel.config.js to babel.config.cjs我将 babel.config.js 更改为 babel.config.cjs

module.exports = {
  presets: [
     [
        '@babel/preset-env',
        {
           targets: {
              node: 'current'
           }
        }
     ]
  ]
};

and now it works:现在它可以工作了:

工作笑话测试

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

相关问题 在MERN应用中的Babel配置 - Babel config in a MERN app 在 svelte 应用程序中的 +page.svelte 上没有加载道具 - Prop not loading on +page.svelte in svelte app 使用 vite 在反应应用程序中加载环境变量 - loading env variables in react app using vite 在 Svelte 和 Vite 中,运行“npm run dev”后出现错误“HTMLElement is not defined” - In Svelte and Vite, error "HTMLElement is not defined" shows up after running "npm run dev" Babel加载器未加载config.json文件 - Babel loader not loading form config.json file 尝试在 nextjs 应用程序中添加 `babel.config.js` 时出现`Cannot find module '.next\server\pages-manifest.json'` 错误 - Getting `Cannot find module '.next\server\pages-manifest.json'` error when trying to add `babel.config.js` in nextjs app 带有Babel ES2015的Angular 2未加载且没有错误 - Angular 2 with babel ES2015 not loading and no error 在控制台中构建后出现 Svelte 错误:“未定义应用程序” - Svelte error after build in console: 'App is not defined' 用于 IE11 的带有 Babel 的 Svelte - Svelte with Babel for IE11 使用 svelte、babel 和 webpack 构建时如何解决“没有 new 就无法调用类构造函数”错误 - How to resolve 'class constructor cannot be invoked without new' error when building with svelte, babel, and webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM