javascript - 找不到 TypeScript 自定义定义文件
我正在尝试为我的外部 JS 库创建自定义定义文件。我收到的消息是 Module not found: Error: Can't resolve。我正在使用这个项目 https://github.com/juristr/webpack-typescript-starter。
我想把我的 d.ts 文件保存在根目录下的 custom_typings 文件夹中。项目结构如下:
- 自定义类型
- index.d.ts
- 问候器.ts
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 文件正常工作。