找不到模块:错误:无法解析“ index.scss”

时间:2019-06-12 14:30:23

标签: webpack sass

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

  ./src/public/app1/index.tsx中的

ERROR找不到模块:错误:不能   在'/ Users / k / portal / src / public / app1'中解析'index.scss'@   ./src/public/app1/index.tsx 10:0-21 @多   ./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"
    }
}]

2 个答案:

答案 0 :(得分:2)

我对反应的了解应该这样导入

import './index.scss';

使用.//

答案 1 :(得分:0)

由于您要导入SCSS文件中的TypeScript个文件,因此需要向TypeScript编译器介绍以下SCSS个文件:

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

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