Search code examples
typescriptwebpackionic3angular-componentswebpack-style-loader

Error: Integrating terra components into Ionic3 app


I am trying to integrate terra components(third party angular component) into ionic 3. We got the error as below.

To load the scss files we have added we have used the loader in webpack.config.js. The code for webpack.config.js is as follows

const webpack = require('webpack');
const ProvidePlugin = require('webpack/lib/ProvidePlugin');
const OccurrenceOrderPlugin = require('webpack/lib/optimize/OccurrenceOrderPlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const autoprefixer = require('autoprefixer');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const helpers = require('./helpers');
const METADATA = {
baseUrl: './'
};
var path = require('path');
module.exports = function (options) {
//isProd = options.env === 'production';
return {
    entry: {
        'bootstrap': 'bootstrap-loader',
        'polyfills': './src/polyfills.ts',
        'vendor': './src/vendor.ts',
        'app': './src/main.ts'
    },
    output:{
      path: path.join(__dirname,'public/assets/'),
      publicPath:'assets/'
  },
    resolve: {
        descriptionFiles: ['package.json'],
        extensions: ['.ts', '.js', '.css', '.scss', 'json', '.html']
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: [
                    'awesome-typescript-loader',
                    'angular2-template-loader'
                ],
                exclude: [/\.(spec|e2e)\.ts$/]
            },
            {
                test: /\.html$/,
                loader: 'html-loader',
                exclude: [helpers.root('src/index.html')]
            },
            {
                test: /\.css$/,
                use: [
                    'to-string-loader',
                    'style-loader',
                    'css-loader'
                ]
            },
            {
              test: /\.scss$/,
              loader: 'style!css!sass!resolve-url!sass?sourceMap&sourceComments'
            },
            {
                test: /\.json$/,
                loader: 'json-loader'
            },
            {
                test: /\.(jpg|png|gif|svg)$/,
                loader: 'file-loader'
            },
            {
                test: /\.(woff)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url-loader?mimetype=application/font-woff'
            },
            {
                test: /\.(woff2)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'url-loader?mimetype=application/font-woff2'
            },
            {
                test: /\.(ttf|eot)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: "file-loader"
            },
            // Bootstrap 4
            {
                test: /bootstrap\/dist\/js\/umd\//,
                loader: 'imports-loader'
            }
        ]
    },
    plugins: [

        // Workaround for angular/angular#11580
        new webpack.ContextReplacementPlugin(
            /angular(\\|\/)core(\\|\/)@angular/,
            helpers.root('./src'), // location of your src
            {} // a map of your routes
        ),

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

        new HtmlWebpackPlugin({
            template: 'src/index.html',
            inject: true,
            metadata: METADATA
        }),

        new OccurrenceOrderPlugin(true),

        new ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery",
            // Tether: "tether",
            // "window.Tether": "tether",
            Alert: "exports-loader?Alert!bootstrap/js/dist/alert",
            Button: "exports-loader?Button!bootstrap/js/dist/button",
            Carousel: "exports-loader?Carousel!bootstrap/js/dist/carousel",
            Collapse: "exports-loader?Collapse!bootstrap/js/dist/collapse",
            Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
            Modal: "exports-loader?Modal!bootstrap/js/dist/modal",
            Popover: "exports-loader?Popover!bootstrap/js/dist/popover",
            Scrollspy: "exports-loader?Scrollspy!bootstrap/js/dist/scrollspy",
            Tab: "exports-loader?Tab!bootstrap/js/dist/tab",
            Tooltip: "exports-loader?Tooltip!bootstrap/js/dist/tooltip",
            Util: "exports-loader?Util!bootstrap/js/dist/util"
        }),

        new CopyWebpackPlugin([
            {from: 'src/app/assets', to: 'assets'},
            {from: 'node_modules/@plentymarkets/terra-components/app/assets/lang', to: 'assets/lang/terra-components/'}
        ]),

        new LoaderOptionsPlugin({
            debug: true,
            options: {
                context: __dirname,
                output: {path: './'},
                postcss: [autoprefixer],
                tslint: {
                    emitErrors: false,
                    failOnHint: false,
                    resourcePath: helpers.root('./src'),
                    formattersDirectory: "./node_modules/tslint-loader/formatters/"
                }
            }
        })
    ],
    node: {
        global: true,
        process: true,
        Buffer: false,
        crypto: 'empty',
        module: false,
        clearImmediate: false,
        setImmediate: false,
        clearTimeout: true,
        setTimeout: true
    }
}
};

Still I am getting the same error. As a new bee to webpack I am not able to understand what wrong I am really doing. What could be the error. Let us know if any details are required.

Thanks in advance


Solution

  • Problem is with the loaders in your webpack.config.js.

    Try replacing with your loaders with mine, that should work for you.

    You can replace the whole code though.

    var path = require('path');
    var webpack = require('webpack');
    var ionicWebpackFactory = require(process.env.IONIC_WEBPACK_FACTORY);
    
    var ModuleConcatPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
    var PurifyPlugin = require('@angular-devkit/build-optimizer').PurifyPlugin;
    
    var optimizedProdLoaders = [
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      {
        test: /\.js$/,
        loader: [
          {
            loader: process.env.IONIC_CACHE_LOADER
          },
    
          {
            loader: '@angular-devkit/build-optimizer/webpack-loader',
            options: {
              sourceMap: true
            }
          },
        ]
      },
      {
        test: /\.ts$/,
        loader: [
          {
            loader: process.env.IONIC_CACHE_LOADER
          },
    
          {
            loader: '@angular-devkit/build-optimizer/webpack-loader',
            options: {
              sourceMap: true
            }
          },
    
          {
            loader: process.env.IONIC_WEBPACK_LOADER
          }
        ]
      }
    ];
    
    function getProdLoaders() {
      if (process.env.IONIC_OPTIMIZE_JS === 'true') {
        return optimizedProdLoaders;
      }
      return devConfig.module.loaders;
    }
    
    var devConfig = {
      entry: process.env.IONIC_APP_ENTRY_POINT,
      output: {
        path: '{{BUILD}}',
        publicPath: 'build/',
        filename: '[name].js',
        devtoolModuleFilenameTemplate: ionicWebpackFactory.getSourceMapperFunction(),
      },
      devtool: process.env.IONIC_SOURCE_MAP_TYPE,
    
      resolve: {
        extensions: ['.ts', '.js', '.json'],
        modules: [path.resolve('node_modules')]
      },
    
      module: {
        loaders: [
          {
            test: /\.json$/,
            loader: 'json-loader'
          },
          {
            test: /\.ts$/,
            loader: process.env.IONIC_WEBPACK_LOADER
          },
          {
            test: /\.html$/,
            loader: 'html-loader'
          },
          {
            test: /\.css$/,
            loader: ['to-string-loader','css-loader']
          },
          {
            test: /\.scss$/,
            loaders: [
              'to-string-loader',
              'style-loader',
              'css-loader',
              'sass-loader',
              {
                loader: 'sass-resources-loader',
                options: {
                  resources: 'node_modules/@plentymarkets/terra-components/app/assets/styles/_variables.scss'
                }
              }
            ]
          }
        ]
      },
    
      plugins: [
        ionicWebpackFactory.getIonicEnvironmentPlugin(),
        ionicWebpackFactory.getCommonChunksPlugin()
      ],
    
      // Some libraries import Node modules but don't use them in the browser.
      // Tell Webpack to provide empty mocks for them so importing them works.
      node: {
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    };
    
    var prodConfig = {
      entry: process.env.IONIC_APP_ENTRY_POINT,
      output: {
        path: '{{BUILD}}',
        publicPath: 'build/',
        filename: '[name].js',
        devtoolModuleFilenameTemplate: ionicWebpackFactory.getSourceMapperFunction(),
      },
      devtool: process.env.IONIC_SOURCE_MAP_TYPE,
    
      resolve: {
        extensions: ['.ts', '.js', '.json'],
        modules: [path.resolve('node_modules')]
      },
    
      module: {
        loaders: getProdLoaders()
      },
    
      plugins: [
        ionicWebpackFactory.getIonicEnvironmentPlugin(),
        ionicWebpackFactory.getCommonChunksPlugin(),
        new ModuleConcatPlugin(),
        new PurifyPlugin()
      ],
    
      // Some libraries import Node modules but don't use them in the browser.
      // Tell Webpack to provide empty mocks for them so importing them works.
      node: {
        fs: 'empty',
        net: 'empty',
        tls: 'empty'
      }
    };
    
    
    module.exports = {
      dev: devConfig,
      prod: prodConfig
    }