0
votes

I want to use scss in my project however when i build my project using webpack i get the following error.

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

Please see my webpack configuration.

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
Did you declare SCSS files as a TypeScript module? - Harshal Patil

2 Answers

2
votes

What I know about react you should import like that

import './index.scss';

Use ./ or /

1
votes

Since you are importing SCSS files in TypeScript files, you need to teach TypeScript compiler about these SCSS files:

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

Create a global.d.ts file in your src folder and add the above module declaration.