1

I am starting with Angular 2 with webpack 2. while running webpack I am facing below errors

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]

Below is my 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"
  }

This is my 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")
    ]
};

If I remove include in css loader built is successfull but I am facing different error while hitting the 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>]

It seems many have faced this issue but their solution dint worked for me.Any idea where I am going wrong?

1 Answer 1

3

To fix "Uncaught Error: Expected 'styles' to be an array of strings" you need to use styles instead of StyleUrls, as StyleUrls expect plain css text. Try some like this

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

Under Plugins you need to change the configuration based on latest version of angular.See the below code.

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


]
Sign up to request clarification or add additional context in comments.

8 Comments

Try to change css configuration by looking this url: angular.io/docs/ts/latest/guide/webpack.html
If I follow that I am facing this error "Uncaught Error: Expected 'styles' to be an array of strings."
Expected 'styles' to be an array of strings is due to loading css file and scss file in styleurls (or) Use require to load css files EX: require('./custom.css');
Loading css like this @Component({ moduleId: module.id.toString(), selector: 'my-dashboard', templateUrl: 'dashboard.component.html', styleUrls: [ './dashboard.component.css' ] })
Use require to load css files, require('./dashboard.component.css'); @Component({ moduleId: module.id.toString(), selector: 'my-dashboard', templateUrl: 'dashboard.component.html', styleUrls: [ '' ] })
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.