【问题标题】:Fix TypeScript warning about imports handled by Webpack loaders?修复关于由 Webpack 加载器处理的导入的 TypeScript 警告?
【发布时间】:2021-04-06 19:39:40
【问题描述】:

我正在使用 Svelte、Webpack 和 TypeScript 设置一个新项目。它基于this template,并在初始设置后运行scripts/setupTypeScript.js 脚本以切换到TypeScript。

当尝试导入 TypeScript 文件的资源时,VSCode 和 TypeScript 会显示有关它们的错误:

import myContent from './some-file.txt';
// -> Cannot find module './some-file.txt' or its corresponding type declarations.ts(2307)

从技术上讲,这是可行的,但是错误很嘈杂,并且似乎阻止了实时重新加载。有没有办法让 TypeScript 忽略这些导入并相信 Webpack 会处理它们?其中一些是二进制文件,所以我也不能将它们复制/粘贴到.ts 文件中。

Webpack 和 TS 配置如下:

webpack.config.js:

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
const sveltePreprocess = require('svelte-preprocess');

const mode = process.env.NODE_ENV || 'development';
const prod = mode === 'production';

module.exports = {
    entry: {
        'build/bundle': ['./src/main.ts']
    },
    resolve: {
        alias: {
            svelte: path.dirname(require.resolve('svelte/package.json'))
        },
        extensions: ['.mjs', '.js', '.ts', '.svelte'],
        mainFields: ['svelte', 'browser', 'module', 'main']
    },
    output: {
        path: path.join(__dirname, '/public'),
        filename: '[name].js',
        chunkFilename: '[name].[id].js'
    },
    module: {
            rules: [
                {
                    test: /\.ts$/,
                    loader: 'ts-loader',
                    exclude: /node_modules/
                },
                {
                    test: /\.svelte$/,
                    use: {
                        loader: 'svelte-loader',
                        options: {
                            compilerOptions: {
                                dev: !prod
                            },
                            emitCss: prod,
                            hotReload: !prod,
                            preprocess: sveltePreprocess({ sourceMap: !prod })
                        }
                    }
                },
                {
                    test: /\.css$/,
                    use: [
                        MiniCssExtractPlugin.loader,
                        'css-loader'
                    ]
                },
                {
                    test: /\.(txt)$/,
                    use: ['raw-loader']
                },
                {
                    test: /\.(ttf|wasm)$/,
                    use: ['file-loader']
                },
                {
                    // required to prevent errors from Svelte on Webpack 5+
                    test: /node_modules\/svelte\/.*\.mjs$/,
                    resolve: {
                        fullySpecified: false
                    }
                }
        ]
    },
    mode,
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].css'
        })
    ],
    devtool: prod ? false : 'source-map',
    devServer: {
        hot: true
    }
};

tsconfig.json:

{
        "extends": "@tsconfig/svelte/tsconfig.json",
        "include": ["src/**/*.ts", "src/node_modules/**/*.ts"],
        "exclude": ["node_modules/*", "__sapper__/*", "static/*"]
}

【问题讨论】:

  • 这能回答你的问题吗? Import html from typescript with webpack
  • use: ['raw-loader'] 是有效的 webpack 配置语法吗?只是问一下,因为我在 webpack.js.org/concepts/loaders 上看不到匹配的示例,但也许它受支持但未推广。
  • 我无法用另一个问题中给出的答案来解决这个问题,尽管它看起来确实相似。将// @ts-ignore cmets 添加到每一行似乎确实可以避免这个问题,尽管我希望有另一种方法。允许['raw-loader'] 语法,是的,一个类型可以有多个加载器。

标签: typescript webpack svelte webpack-file-loader webpack-loader


【解决方案1】:

添加具有以下内容的global.d.ts 文件,并确保它被您的include 拾取:

declare module "*.txt";

这告诉 TS“任何以.txt 结尾的导入都可以,将其视为any”。或者,使用另一种类型导出默认值:

declare module "*.txt" {
    const content: string;
    export default content;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2017-04-25
相关资源
最近更新 更多