Lee Matthews
Lee Matthews

Reputation: 11

Setup issue with webpack, angular 2, css and sass

[UPDATED FROM ORIGINAL ISSUE] I am trying to setup the latest version of Angular2 with webpack to process sass. I have scss files being processed now. However, the problem is with css now.

I have added the following to the app.component.ts file...

require('../../node_modules/bootstrap/dist/css/bootstrap.css');
require('../../public/styles/styles.scss');

I am getting a build error from npm start...

    ./~/bootstrap/dist/css/bootstrap.css
Module build failed: ReferenceError: window is not defined
bootstrap\dist\css\bootstrap.css doesn't export content

The relevant bits of webpack.common.js...

{
    test: /\.css$/,
    loader: ExtractTextPlugin.extract({ loader: [{ loader: "style-loader" }, { loader: "css-loader" } ] })
  },
  {
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract({ loader: [{ loader: "css-loader" }, { loader: "sass-loader" } ] })
  }

A snippet from my package.json...

"devDependencies": {
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"codelyzer": "~2.0.0-beta.1",
"faker": "3.1.0",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"json-server": "0.9.4",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"node-sass": "^4.4.0",
"protractor": "~4.0.13",
"raw-loader": "^0.5.1",
"sass-loader": "5.0.1",
"ts-node": "1.2.1",
"tslint": "4.3.0",
"typescript": "2.1.6",
"webdriver-manager": "11.1.1",
"angular2-template-loader": "^0.6.0",
"awesome-typescript-loader": "^3.0.0-beta.18",
"css-loader": "^0.26.1",
"extract-text-webpack-plugin": "2.0.0-beta.5",
"html-webpack-plugin": "^2.16.1",
"file-loader": "^0.10.0",
"html-loader": "^0.4.3",
"style-loader": "^0.13.1",
"postcss-loader": "1.2.2",
"webpack": "2.2.1",
"webpack-dev-server": "2.3.0",
"webpack-merge": "^2.6.1"

}

I have been trying to solve this for days. Any help is much appreciated.

Upvotes: 1

Views: 1323

Answers (2)

Lee Matthews
Lee Matthews

Reputation: 11

So, I found the answer. It appears the angular docs are out of date, as well as webpack docs.

I ended up with this as my working config file...

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
  entry: {
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'app': './src/main.ts'
  },

  resolve: {
    extensions: ['.ts', '.js', '.json', '.css', '.scss', '.html']
  },

  module: {
    rules: [
      {
        test: /\.ts$/,
        loaders: [{
          loader: 'awesome-typescript-loader',
          options: { configFileName: helpers.root('src', 'tsconfig.json') }
        },
        'angular2-template-loader']
      },
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      {
        test: /\.(png|jpg|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'file-loader?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({ loader: [{ loader: "css-loader" }, { loader: "postcss-loader" } ]})
      },
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract({ loader: [{ loader: "css-loader" }, { loader: "sass-loader" } ] })
      }
    ]
  },

  plugins: [
    new ExtractTextPlugin({ filename: 'css/[name].css', disable: false, allChunks: true }),
    // Workaround for angular/angular#11580
    new webpack.ContextReplacementPlugin(
      // The (\\|\/) piece accounts for path separators in *nix and Windows
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
      helpers.root('./src'), // location of your src
      {} // a map of your routes
    ),

    new webpack.optimize.CommonsChunkPlugin({
      name: ['app', 'vendor', 'polyfills']
    }),

    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
};

Then I added require statements to the app.component.ts file for the global css requirements, and then use the styles attribute on nested components for component specific styling.

Upvotes: 0

Ruaghain
Ruaghain

Reputation: 135

Here's an example that works for me with regards to the use of scss. It's important to note that I don't compile my resulting css into a separate file. It gets generated as a module in my resulting js file:

  {
    test: /\.(scss)$/,
    loaders: [
      'raw-loader',
      'sass-loader'
    ]
  }

Upvotes: 1

Related Questions