我的 Webpack 配置:
const path = require("path");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const sass = require("sass");
module.exports = {
mode: "development",
devtool: "inline-source-map",
devServer: {
static: "./dist",
hot: true
}
entry: "./src/index.tsx",
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist"),
clean: true
},
module: {
rules: [
{
test: /\.scss$/,
use: [
"style-loader",
"css-loader",
{
loader: "sass-loader",
options: {
implementation: sass,
sassOptions: {
fiber: false
}
}
}
],
exclude: "/node_modules/"
},
{
test: /\.tsx$/,
use: [
{
loader: "ts-loader"
},
{
loader: "babel-loader",
options: {
presets: ["@babel/preset-typescript", "@babel/react", "@babel/env"],
}
}
],
exclude: "/node_modules/"
},
{
test: /\.lang$/,
type: "asset/source",
exclude: "/node_modules/"
}
]
},
resolve: {
extensions: [".js", ".ts", ".tsx"]
},
plugins: [
new HTMLWebpackPlugin({
template: "./src/index.html",
inject: true
})
]
};
我得到的錯誤:
ERROR in ./src/impl/text/language.ts 3:27
Module parse failed: Unexpected token (3:27)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import * as enUS from "lang/en-US.lang";
|
> const parseLanguage = (lang: string) => {
| console.log(lang);
| const values: { [key: string]: string } = {};
@ ./src/components/App.tsx 2:0-52 6:15-26
@ ./src/index.tsx 3:0-39 4:36-39
webpack 5.65.0 compiled with 1 error in 2854 ms
我在讓 Typescript 運行良好時遇到了很多問題,現在我終生無法弄清楚為什么 Webpack 有這個問題。我嘗試匯入的檔案名為en-US.lang. 除了嘗試各種加載器(,...)之外,我還嘗試使用asset/resourceand asset/inline,但我真的很茫然。raw-loaderfile-loader
所有其他檔案型別都可以完美運行,我什至使用類似的格式匯入了 PNG/SVG...那么我在這里做錯了什么?
uj5u.com熱心網友回復:
長話短說,被匯入的檔案在一個 Typescript(不是 tsx)檔案中,我需要包含在 Webpack TS 加載器中
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379573.html
