我是新的打字和WebPACK编程,所以请不要理所当然地过多的背景…另外,我在堆栈溢出中看到了许多类似的问题,直到现在都没有解决我的问题的方法:
我用的是vscode。我的构建会产生以下警告:

WARNING in ./src/amxcanvas.ts 3:24-31
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

WARNING in ./src/amxmisc.ts 3:24-31
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./src/amxcanvas.ts

在执行生成的JavaScript代码时,当我得到错误(模块未找到)时,我认为我应该首先消除这些警告。
我使用以下webpack.config.js:
var path = require("path");

module.exports = {
  entry: path.join(__dirname, '/src', '/amxcanvas.ts'),
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ]
  },
  output: {
    filename: 'amxcanvas.bundle.js',
    path: path.resolve(__dirname, 'dist'),
    libraryTarget: 'var',
    library: 'AmxCvs'
  }
};

amxcanvas.ts文件的开头如下:
import * as drawable from "./amxdrawables";
import * as misc from "./amxmisc";

export function amxCanvasInit(canvasId:string, shapes:drawable.IDrawable[]) {
    var s = new CanvasState(document.getElementById(canvasId), shapes);
}

amxmisc.ts文件的开头如下:
export function generateUUID(): string {
    return "";
}

var gripSize:number = 7;

我已经挣扎了将近一个星期了,有人知道我做错了什么吗?

最佳答案

好的,在发布之后,我找到了解决方案:我还不完全确定它的含义,但是通过改变:

"module": "umd",


"module": "commonjs",

在TSCONT.JSON中,警告消失了。我觉得umd在require上做了一些奇怪的事情,阻止了webpack的分析。

关于typescript - Webpack和Typescript的“关键依赖项:require函数以无法静态提取依赖项的方式使用”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57944098/

10-12 01:12
查看更多