Angular 2 Webpack 2 CSS 模块解析错误

Angular 2 Webpack 2 CSS module parse error

我从 Angular 2 和 webpack 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 中的 include 构建加载程序是成功的,但我在点击 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>]

似乎很多人都遇到过这个问题,但他们的解决方案对 me.Any 我错在哪里的想法有效?

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

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

在插件下,您需要根据以下代码的最新版本 angular.See 更改配置。

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 
                   } 
    }
  }) 


]