加载程序/src/app/app.component.scss中的错误未返回字符串

时间:2018-07-15 14:50:20

标签: webpack angular6

enter image description here

在将sass-loader配置到我的webpack之后,我遇到了这个问题,我确实为此找到了一些解决方案,但是看起来不起作用。 谁能帮我。谢谢!

这是我的webpack.config.js文件。我从头开始配置自己。

const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ScriptExtPlugin = require('script-ext-html-webpack-plugin');
const { AngularCompilerPlugin } = require('@ngtools/webpack');


module.exports = function () {
  return {
    entry: './src/main.ts',
    output: {
      path: __dirname + '/dist',
      filename: 'app.js'
    },
    resolve: {
      extensions: ['.ts', '.js']
    },
    performance: {
      maxEntrypointSize: 512000,
      maxAssetSize: 512000
    },
    module: {
      rules: [
        { test: /\.ts$/, loaders: ['@ngtools/webpack'] },
        { test: /\.css$/, loader: 'raw-loader' },
        { test: /\.html$/, loader: 'raw-loader' },
        {
          test: /\.scss$/,
          exclude: /node_modules/,
          use: [{
            loader: "css-loader", options: {
              sourceMap: true
            }
          }, {
            loader: "sass-loader", options: {
              sourceMap: true
            }
          }]
        },
        // Ignore warnings about System.import in Angular
        { test: /[\/\\]@angular[\/\\].+\.js$/, parser: { system: true } },
      ]
    },
    plugins: [
      new CopyWebpackPlugin([
        { from: 'src/assets', to: 'assets' }
      ]),
      new HtmlWebpackPlugin({
        template: __dirname + '/src/index.html',
        output: __dirname + '/dist',
        inject: 'head'
      }),

      new ScriptExtPlugin({
        defaultAttribute: 'defer'
      }),
      new AngularCompilerPlugin({
        tsConfigPath: './tsconfig.json',
        entryModule: './src/app/app.module#AppModule',
        sourceMap: true
      })

    ]
  };
}

我认为问题出在我为项目配置sass的方式上,但我不知道它是什么。

1 个答案:

答案 0 :(得分:3)

您需要在css-loader之后添加css-to-string-loader。另外,正如Luillyfe所说,您应该将html-loader用于html。

{
  // For Angular template html
  test: /\.html$/,
  use: 'html-loader'
},
{
  test: /\.scss$/,
  exclude: /node_modules/,
  use: [
    'css-to-string-loader',
    {loader: 'css-loader', options: {sourceMap: true}},
    {loader: 'sass-loader', options: {sourceMap: true}}
  ]
},