gpt4 book ai didi

webpack - 找不到模块 : Error: Can't resolve 'index.scss'

转载 作者:行者123 更新时间:2023-12-02 00:17:41 24 4
gpt4 key购买 nike

我想在我的项目中使用 scss,但是当我使用 webpack 构建我的项目时,出现以下错误。

ERROR in ./src/public/app1/index.tsx Module not found: Error: Can't resolve 'index.scss' in '/Users/k/portal/src/public/app1' @ ./src/public/app1/index.tsx 10:0-21 @ multi ./src/public/app1/index.tsx

请查看我的 webpack 配置。

const path = require('path');
const nodeExternals = require('webpack-node-externals');
const WebpackShellPlugin = require('webpack-shell-plugin');
const CopyWebpackPlugin = require("copy-webpack-plugin");
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

const {
NODE_ENV = 'development',
} = process.env;

module.exports = [{
entry: {
app1: ["./src/public/app1/index.tsx"],
app2: ["./src/public/app2/index.tsx"],

},

output: {

filename: "[name]/bundle.js",
path: path.resolve(__dirname, 'build/public/'),
},

// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",

resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json", ".css", ".scss"]
},

module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{ test: /\.tsx?$/, loader: "awesome-typescript-loader" },

// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
{
test: /(\.css|.scss)$/,
use: [{
loader: "style-loader" // creates style nodes from JS strings
}, {
loader: "css-loader" // translates CSS into CommonJS
}, {
loader: "sass-loader" // compiles Sass to CSS
}]

},
]
},
"plugins": [
new CleanWebpackPlugin(),
new CopyWebpackPlugin([
{
from: "src/public/app1/index.html",
to: "app1"
},
{
from: "src/public/app2/index.html",
to: "app2"
},
]),
],



// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
// "react": "React",
// "react-dom": "ReactDOM"
}
}]

最佳答案

由于您要在 TypeScript 文件中导入 SCSS 文件,因此您需要向 TypeScript 编译器介绍这些 SCSS 文件:

declare module '*.scss' {
const content: string;
export default content;
}

src 文件夹中创建一个 global.d.ts 文件并添加上述模块声明。

关于webpack - 找不到模块 : Error: Can't resolve 'index.scss' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56564619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com