javascript - 模块未在 node_modules/core-js/internals/global.js 中定义

标签 javascript typescript babeljs rollupjs core-js

我正在用 TypeScript 构建一个库,并且正在尝试测试它。由于它涉及浏览器 API,我正在使用 @web/test-runnerchai 对其进行测试。

我正在使用 TypeScript,一些库是 commonjs 模块,因此我使用 Rollup 将所有这些捆绑到浏览器可以使用的东西中。

我的汇总配置如下:

import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import sourceMaps from 'rollup-plugin-sourcemaps';
import { babel } from '@rollup/plugin-babel';
import nodePolyfills from 'rollup-plugin-node-polyfills';

const pkg = require('./package.json');

const libraryName = 'AGreatLibrary';

export default {
  input: 'src/index.ts',
  output: [
    { file: pkg.main, name: libraryName, format: 'umd', sourcemap: true },
    { file: pkg.module, format: 'es', sourcemap: true },
  ],
  plugins: [
    json(),
    nodePolyfills(),
    typescript(),
    commonjs(),
    nodeResolve({ browser: true, preferBuiltins: false }),
    sourceMaps(),
    babel({ babelHelpers: 'bundled', exclude: [/\bcore-js\b/, /\bwebpack\/buildin\b/] }),
  ],
};

我的巴贝尔配置

{
  "presets":
  [
      [
          "@babel/env",
          {
              "targets":
              {
                  "edge": "17",
                  "firefox": "60",
                  "chrome": "67",
                  "safari": "11.1"
              },
              "useBuiltIns": "usage",
              "corejs": "3"
          }

      ]
  ],
  "minified": true
}

我的测试文件

import { Client } from "../lib/index.es.js";
import { expect } from '@esm-bundle/chai';

it('Should create a Client', () => {
  let client = new Client("123", true);
  expect(client.accessToken).to.equal("123");
});

it('Should auth', async () => {
  let client = new Client("123", true);
  let resp = await client.tryAuth();
  expect(resp.ok).to.be.true;
});

但是当我运行测试时 (web-test-runner "test/**/*.test.ts"--node-resolve)

我收到以下错误

🚧 Browser logs:
      ReferenceError: module is not defined
        at node_modules/core-js/internals/global.js:6:0

我不知道该怎么办。

最佳答案

我修好了!

plugins: [
    commonjs(),
    json(),
    nodePolyfills(),
    typescript(),
    nodeResolve({ browser: true, preferBuiltins: false }),
    sourceMaps(),
    babel({ babelHelpers: 'bundled', exclude: [/\bcore-js\b/, /\bwebpack\/buildin\b/] }),
  ],

commonjs 应该位于数组顶部。

关于javascript - 模块未在 node_modules/core-js/internals/global.js 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68350824/

相关文章:

php - 将数据从选择菜单传递到另一个选择菜单 AJAX Codeigniter

javascript - 有没有办法为 Javascript/Typescript 模块自动声明外部导出?

javascript - 将测试文件放在与源文件相同的文件夹中或单独放在 test/下的优缺点

javascript - TSLint `import-name` 提示 import React from 'react' 并将其变为小写

javascript - 使用babel-node调试ES6 Node.js应用程序

javascript - 在 Visual Studio 2010 中处理常见的 JavaScript 文件

javascript - 在添加新标记和折线之前删除旧标记和折线 - Mapbox

javascript - 隐藏的div没有正确加载javascript

javascript - __zone_symbol__currentTask 错误

typescript - 无法使用选项 'noEmit' 指定选项 'incremental'