Angular 2 Webpack 2 CSS模块解析错误

时间:2017-05-02 07:35:53

标签: angular webpack webpack-2 css-loader

我开始使用带有webpack 2的Angular 2.在运行webpack时我遇到了以下错误

WARNING in ./~/@angular/core/@angular/core.es5.js
5870:15-36 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@angular/core/@angular/core.es5.js
5886:15-102 Critical dependency: the request of a dependency is an expression

ERROR in ./app/dashboard/hero-detail.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-detail.compo
nent.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| label {
|   display: inline-block;
|   width: 3em;
 @ ./app/dashboard/hero-detail.component.ts 45:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/dashboard.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\dashboard.compone
nt.css Unexpected token (1:6)
You may need an appropriate loader to handle this file type.
| [class*='col-'] {
|   float: left;
|   padding-right: 20px;
 @ ./app/dashboard/dashboard.component.ts 32:17-53
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/heroes.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\heroes.component.
css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .selected {
|   background-color: #CFD8DC !important;
|   color: white;
 @ ./app/dashboard/heroes.component.ts 65:17-50
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts

ERROR in ./app/dashboard/hero-search.component.css
Module parse failed: D:\WorkSpace\Angular2\Login\app\dashboard\hero-search.compo
nent.css Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
| .search-result{
|   border-bottom: 1px solid gray;
|   border-left: 1px solid gray;
 @ ./app/dashboard/hero-search.component.ts 59:17-55
 @ ./app/dashboard/index.ts
 @ ./app/app.module.ts
 @ ./app/main.ts
Child html-webpack-plugin for "index.html":
       [0] ./~/html-webpack-plugin/lib/loader.js!./index.html 341 bytes {0} [built]

以下是我的package.json

  "devDependencies": {
    "@types/core-js": "^0.9.41",
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.45",
    "angular2-template-loader": "^0.6.0",
    "awesome-typescript-loader": "^3.0.4",
    "css-loader": "^0.28.0",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.9.0",
    "html-loader": "^0.4.5",
    "html-webpack-plugin": "^2.16.1",
    "jasmine-core": "^2.4.1",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^2.0.1",
    "null-loader": "^0.1.1",
    "postcss-cssnext": "^2.10.0",
    "postcss-loader": "^1.3.3",
    "postcss-responsive-type": "^0.5.1",
    "postcss-smart-import": "^0.6.12",
    "raw-loader": "^0.5.1",
    "resolve-url-loader": "^2.0.2",
    "rimraf": "^2.5.2",
    "sass-loader": "^6.0.3",
    "style-loader": "^0.13.1",
    "stylus-loader": "^3.0.1",
    "to-string-loader": "^1.1.5",
    "typescript": "^2.0.9",
    "webpack": "2.2.1",
    "webpack-dev-server": "2.4.1",
    "webpack-merge": "^3.0.0"
  }

这是我的webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

module.exports = {
    entry: {
        'app': './app/main.ts',
        'vendor': './vendor.ts'
    },
    output: {
        filename: "[name].js",
        path: path.resolve(__dirname, 'dist')
    },
    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                use: [{
                    loader: 'html-loader',
                    options: {
                        minimize: true
                    }
                }],
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                include: /app(\/|\\)css/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            }
        ]
    },

    plugins: [
        // Workaround for angular/angular#11580
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
            'app', // location of your src
            {} // a map of your routes
        ),

        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),

        new HtmlWebpackPlugin({
            template: './index.html'
        }),
        new ExtractTextPlugin("bundle.css")
    ]
};

如果我删除包含在css中的内置加载器是成功的但是我在遇到URL时遇到了不同的错误

Uncaught Error: Expected 'styles' to be an array of strings.
    at assertArrayOfStrings (http://127.0.0.1:8080/vendor.js:30661:19) [<root>]
    at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (http://127.0.0.1:8080/vendor.js:41563:13) [<root>]
    at CompileMetadataResolver._getEntryComponentMetadata (http://127.0.0.1:8080/vendor.js:42265:45) [<root>]
    at http://127.0.0.1:8080/vendor.js:42251:48 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getEntryComponentsFromProvider (http://127.0.0.1:8080/vendor.js:42250:30) [<root>]
    at http://127.0.0.1:8080/vendor.js:42214:83 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:42180:23 [<root>]
    at Array.forEach (native) [<root>]
    at CompileMetadataResolver._getProvidersMetadata (http://127.0.0.1:8080/vendor.js:42178:19) [<root>]
    at http://127.0.0.1:8080/vendor.js:41753:63 [<root>]
    at Array.forEach (native) [<root>]

似乎很多人都面临这个问题,但他们的解决方案对我有用。任何想法我哪里出错了?

1 个答案:

答案 0 :(得分:3)

要修复“未捕获错误:预期'样式'为字符串数组”,您需要使用样式而不是StyleUrls,因为StyleUrls需要简单的CSS文本。试试这样的

styles: [require('./dashboard.component.css').toString()]

在插件下,您需要根据最新版本的角度更改配置。请参阅以下代码。

plugins: [
           new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
             /angular(\\|\/)core(\\|\/)@angular/,
            helpers.root('./src'), // location of your src
            {} // a map of your routes
        ),

new webpack.LoaderOptionsPlugin({ 
options : { 
          htmlLoader : { 
              minimize : false 
                   } 
    }
  }) 


]