简体   繁体   English

Angular2 / Webpack 2 / Sass设置:TypeError:cssText.replace不是函数

[英]Angular2 / Webpack 2 / Sass setup : TypeError: cssText.replace is not a function

I'm using Angular2 Universal starter to setup my project. 我正在使用Angular2 Universal入门版设置我的项目。 I'm trying to setup the scss files but I am running into an issue when using Extract Text Plugin: 我正在尝试设置scss文件,但是在使用“提取文本插件”时遇到问题:

package.json 的package.json

"webpack": "2.2.1",
"extract-text-webpack-plugin": "^2.0.0-rc.2"

Webpack setup: Webpack设置:

....
module: {
rules: [
  // TypeScript
  { test: /\.ts$/,   use: ['awesome-typescript-loader', 'angular2-template-loader'] },
  { test: /\.html$/, use: 'raw-loader' },
  { test: /\.css$/, use: 'raw-loader' },
  { test: /\.scss$/, use: ExtractTextPlugin.extract({fallbackLoader: "style-loader", loader:'raw-loader!sass-loader'})},
  { test: /\.(woff2?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/, use: "file?name=fonts/[name].[ext]"},
  { test: /\.json$/, use: 'json-loader' }
],
 },

plugins: [
  //Use commonPlugins.
  new ExtractTextPlugin({
    filename: '[name].css',
    allChunks: true,
    disable: false
  })
 ]....

When I build the project, I get this error: 构建项目时,出现以下错误:

TypeError: cssText.replace is not a function

In my App Componet, I load the scss files using "angular2-template-loader": 在我的App Componet中,我使用“ angular2-template-loader”加载scss文件:

...
@Component({
    changeDetection: ChangeDetectionStrategy.Default,
    encapsulation: ViewEncapsulation.Emulated,
    selector: 'app',
    styles: [require('../assets/styles/main.scss')],

In my dist folder, the main.scss is compiled to main.css: 在我的dist文件夹中,main.scss编译为main.css:

/**
 * SCSS entry point
 * IMPORTANT: Do not add any styles here instead import your scss files
 */
/*!
 * Bootstrap v3.3.7 (http://getbootstrap.com)
 * Copyright 2011-2016 Twitter, Inc.
 * Licensed under MIT     (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */
/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */
html {
  font-family: sans-serif;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%; }

body {
  margin: 0; }

article,
aside ...

If anyone can point me in the right direction, that would be greatly appreciated. 如果有人能指出正确的方向,那将不胜感激。 Thank you. 谢谢。

************ UPDATE ********************* ************更新*********************

I found out that 我发现

 require('../assets/styles/main.scss')

doesn't return a String as expected using Extract Text Plugin. 使用提取文本插件未按预期返回字符串。 I then cast the require output to a string: 然后,我将require输出转换为字符串:

require('../assets/styles/main.scss').toString()

which works as expected but when I inspect the Header, I'm seeing: 可以正常工作,但是当我检查标题时,我看到:

<link rel="stylesheet" href="/main.css"> ---> My generate scss file
<style>[object Object]</style>   ---> seems like this is coming from the app component styles property???

{ test: /.scss$/i, use: ['to-string-loader', ...extractCSS.extract({ loader: ['css-loader', 'sass-loader'] })] } {测试:/.scss$/i,使用:['to-string-loader',... extractCSS.extract({loader:['css-loader','sass-loader']})]}

This will extract all css from components to a external css files. 这会将所有CSS从组件提取到外部CSS文件。 This will break angular view encaptulation as angular can't modify css. 这将破坏角度视图的封装,因为角度无法修改CSS。 I'm still trying to figure it out how to make it work with encaptulation. 我仍在尝试弄清楚如何使其与封装结合使用。 I'm not sure at witch stage angular changes the css to add attributes. 我不确定在女巫阶段角度会更改CSS来添加属性。

Also instead of : styles: [require('../assets/styles/main.scss')], you can say: styleUrls: ['../assets/styles/main.scss'] 也可以代替::styles:[require('../ assets / styles / main.scss']],说:styleUrls:['../assets/styles/main.scss']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM