使用Typescript和Webpack时,相对路径相对的外部模块的解析

我想首先说明一下,我一直在努力使它工作几天,已经广泛搜索了答案并尝试了许多事情,但是还没有碰到任何运气。我还将承认自己对Javascript模块解析的了解不是最深刻的,但是尽管如此,我还是觉得在尝试这里之前已经用尽了其他方法。

基本上,我在磁盘上有两个彼此相邻的Typescript项目-一个叫做asahi,这是一个代码库(特别是WebGL游戏引擎),另一个叫做asahi-testbed (实际上是“游戏”),该库的使用者。这些项目具有以下几乎相同的文件夹结构:

dev
└asahi
 └dist
 └src <-- All source .ts files are here
 └tsconfig.json
└asahi-testbed
 └dist
 └src <-- All source .ts files are here
 └tsconfig.json
 └webpack.config.js

两个项目都使用commonjs进行编译。 asahi按预期进行编译,并按预期将文件输出到dist,因为我为此设置了asahi的配置文件。那里没有问题。

如果我使用标准asahi-testbed编译tsc,它也可以工作。当我尝试使用Webpack编译asahi-testbed时出现问题。尝试执行此操作时,出现以下错误:

ERROR in ./src/index.ts
Module not found: Error: Can't resolve '@asahi/Core/Engine' in 'C:\_dev\asahi-testbed\src'
 @ ./src/index.ts 12:27-56

我已经尝试了多种方法来使其与Webpack一起使用(例如各种配置选项,使用tsconfig-paths-webpack-plugin等),但无济于事。大多数搜索都找到了在项目内部 中重新映射相对路径模块的解决方案,但是我无法找到关于项目目录外部的任何内容。我不确定我在这里做错了什么,但是对此的任何帮助将不胜感激。

我还要注意,我正在使用以下版本:

"ts-loader": "^6.2.1","tsconfig-paths-webpack-plugin": "^3.2.0","typescript": "^3.7.2","webpack": "^4.41.2","webpack-cli": "^3.3.10"

如果需要其他信息,请告诉我。

作为参考,以下是tsconfig.json中的asahi-testbed(为简洁起见进行了编辑):

{
    "compilerOptions": {
        "target": "es6","module": "commonjs","strict": false,"baseUrl": "./","paths": {
            "@asahi/*": [
                "../asahi/dist/*"
            ]
        },"esModuleInterop": true,"forceConsistentCasingInFileNames": true
    },"include": [
        "**/*.ts"
    ],/* Project references */
    "references": [
        {
            "path": "../asahi","prepend": false
        }
    ]
}

这是webpack.config.js中的asahi-testbed

const tsConfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const path = require('path');

module.exports = {
    entry: './src/index.ts',module: {
        rules: [
            {
                test: /\.ts?$/,use: {
                    loader: 'ts-loader',options: {
                        configFile: "tsconfig.json",projectReferences: true                       
                    }
                }
            }
        ],},performance: {
        maxAssetSize: 100000,maxEntrypointSize: 1000000
    },devtool: "",mode: "development",resolve: {
        extensions: ['.tsx','.ts',".js"],modules: [ path.resolve(__dirname,"../asahi/dist/"),"node_modules"],plugins: [
            new tsConfigPathsPlugin({ configFile: "tsconfig.json" })
        ]
    },externals: {
        "@asahi": "../asahi/dist"
    },output: {
        filename: 'bundle.js',path: path.resolve(__dirname,'dist'),}
};

最后,来自index.ts的单个asahi-testbed文件引用了asahi项目:

import * as Asahi from "@asahi/Core/Engine"

window.addEventListener( "load",() => {
    let engine = new Asahi.Engine();

} )
lanjiong 回答:使用Typescript和Webpack时,相对路径相对的外部模块的解析

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2897328.html

大家都在问