【问题标题】:Module not found: Error: Can't resolve 'index.scss'找不到模块:错误:无法解析“index.scss”
【发布时间】:2019-10-27 02:39:25
【问题描述】:

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

./src/public/app1/index.tsx 中的错误找不到模块:错误:不能 解决 '/Users/k/portal/src/public/app1' 中的 'index.scss' @ ./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"
    }
}]

【问题讨论】:

  • 您是否将 SCSS 文件声明为 TypeScript 模块?

标签: webpack sass


【解决方案1】:

我所知道的 react 你应该这样导入

import './index.scss';

使用.//

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 2020-12-15
      • 2017-12-29
      • 2019-12-01
      • 2023-03-22
      • 2017-07-10
      相关资源
      最近更新 更多