在webpack.config.js

时间:2018-02-05 12:49:03

标签: angular typescript webpack ionic3 webpack.config.js

我正在尝试将第三方角度组件集成到Ionic 3演示应用程序中。

该角度组件具有HTML,CSS& SCSS里面,所以我修改了我的webpack.config.js以包含加载器。

这给我带来了两个问题:

  1. “对象原型可能只是一个Object或null:undefined”
  2. Sass错误:要导入的文件未找到或不可读:~bootstrap / scss / mixins
  3. enter image description here

    我的webpack.config.js

    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: /\.css$/,
            loader: 'css-loader'
          },
          {
            test: /\.scss$/,
            loaders: [
              'style-loader',
              'css-loader',
              'sass-loader',
              {
                loader: 'sass-resources-loader',
                options: {
                  resources: 'node_modules/@plentymarkets/terra-components/app/assets/styles/_variables.scss'
                }
              }
            ]
          },
          {
            test: /\.html$/,
            loader: 'html-loader'
          }
        ]
      },
    
      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
    }
    

    我的webpack.config.js中的加载器有什么问题吗?

    如果有的话,请告诉我。

    提前致谢。

1 个答案:

答案 0 :(得分:1)

"问题对象原型可能只是一个Object或null:undefined"与不使用兼容的角度版本有关,您可能正在使用角度版本为5的terra组件。将角度版本降级为4并检查错误是否仍然存在。

另一个问题" Sass错误:导入文件未找到或不可读:~bootstrap / scss / mixins"与sass-loader。 sass-loader通常检测〜并在节点模块中搜索相同的内容。在这种情况下,sass-loader无法识别它并且它会抛出错误。将〜更改为node_modules并检查错误是否仍然存在。有关此问题的详细信息,请查看https://github.com/webpack-contrib/sass-loader