找不到TypeScript自定义定义文件

时间:2019-02-14 11:04:47

标签: javascript angular typescript webpack

我正在尝试为外部JS库创建自定义定义文件。我收到Module not found: Error: Can't resolve的消息。我正在使用这个项目https://github.com/juristr/webpack-typescript-starter

我想将d.ts文件保存在custom_typings的根文件夹中。项目结构如下:

  • custom_typing
    • index.d.ts
  • node_modules
  • src
    • greeter.ts
  • webpack.config.js
  • tsconfig.json

greeter.ts

import * as t from './../custom_typing/index';
export class Greeter {
    constructor(name: string) {
        t.test('dasd');
    }

    greet(): void {
    }
}

webpack.config.js

const path = require('path');
const webpack = require('webpack');

const ROOT = path.resolve( __dirname, 'src' );
const DESTINATION = path.resolve( __dirname, 'dist' );

module.exports = {
    context: ROOT,

    entry: {
        'main': './main.ts'
    },

    output: {
        filename: 'app.bundle.js',
        path: DESTINATION
    },

    resolve: {
        extensions: ['.ts', '.js'],
        modules: [
            ROOT,
            'node_modules'
        ]
    },

    module: {
        rules: [
            /****************
            * PRE-LOADERS
            *****************/
            {
                enforce: 'pre',
                test: /\.js$/,
                use: 'source-map-loader'
            },
            {
                enforce: 'pre',
                test: /\.ts$/,
                exclude: /node_modules/,
                use: 'tslint-loader'
            },

            /****************
            * LOADERS
            *****************/
            {
                test: /\.ts$/,
                exclude: [ /node_modules/ ],
                use: 'awesome-typescript-loader'
            }
        ]
    },

    devtool: 'cheap-module-source-map',
    devServer: {}
};

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "allowJs": true
  }
}

我的index.d.ts示例

export declare function test(test: string): void;

当我运行webpack命令时,我得到

  

./ greeter.ts中的错误   找不到模块:错误:无法解析'./../ custom_typing / index'

编辑。

当我将index.d.ts重命名为index.ts时,它似乎可以工作,但是我仍然需要d.ts文件正常工作。

0 个答案:

没有答案