首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack未捕获调用:无法读取未定义的属性“”TypeError“”

Webpack未捕获调用:无法读取未定义的属性“”TypeError“”
EN

Stack Overflow用户
提问于 2018-09-07 18:33:04
回答 2查看 15.6K关注 0票数 6

基本上,我在react-router文件中有一个动态条目块。但是在3中,我无法从这些条目文件中将css提取成单独的块。因此,在webpack条目中包含了相同的组块名称。

现在已经创建了块,提取了没有重复项的css,并将来自多个条目文件的常见scss导入移到了commonChunks条目中,但我开始收到这个错误。可能是因为我现在指定了两次条目块(一次在webpack条目中,另一次在react-router文件中)

所以我升级到了3.10,它解决了这个问题,但现在css块中有重复的css。

所以想知道任何解决方案或替代方案,以提取独立命名区块的css在webpack 3.2或解决重复的css问题在3.10

错误(仅在生产模式下发生)

manifest.a9c406e9412da8263008.js:1 Uncaught TypeError: Cannot read property 'call' of undefined at n (manifest.a9c406e9412da8263008.js:1) at Object../desktop-containers/Index/index.js (VM150 home.1ee4964ea9da62fee1c0.js:1) at n (manifest.a9c406e9412da8263008.js:1) at window.webpackJsonp (manifest.a9c406e9412da8263008.js:1) at VM150 home.1ee4964ea9da62fee1c0.js:1

github issue https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/185#issuecomment-419396442链接

代码语言:javascript
复制
/* Desktop webpack client-side config */
const webpack = require('webpack');
const path = require('path');
const DashboardPlugin = require('webpack-dashboard/plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
const autoprefixer = require('autoprefixer');
const WebpackStableModuleIdAndHash = require('webpack-stable-module-id-and-hash');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const AssetsPlugin = require('assets-webpack-plugin');
const HashOutput = require('webpack-plugin-hash-output');

const nodeEnv = process.env.NODE_ENV;

const isProduction = nodeEnv === 'production';
/** ***********common rules********* */
const rules = [
  {
    test: /\.html$/,
    loader: 'html-loader'
  },
  {
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    use: [
      {
        loader: 'babel-loader'
      }
    ]
  }
];

const plugins = [
  new webpack.DefinePlugin({
    'process.env': {
      NODE_ENV: JSON.stringify(nodeEnv)
    },
    __BROWSER__: true
  }),
  new webpack.NamedModulesPlugin(), // used for scope hoisting in webpack 3
  new webpack.LoaderOptionsPlugin({
    options: {
      postcss: [
        autoprefixer({
          browsers: ['> 1%', 'Firefox >= 20', 'ie >= 9']
        })
      ],
      context: path.resolve(`${__dirname}client`)
    }
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: 'vendor',
    minChunks: Infinity
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: 'main',
    children: true,
    minChunks: 2
  }),
  new webpack.optimize.CommonsChunkPlugin({
    name: 'manifest',
    minChunks: Infinity
  }),
  // optimize moment
  new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  // exclude mobile-specific modules
  new webpack.IgnorePlugin(/react-input-range/),
  new webpack.IgnorePlugin(/react-lazy-load/),
  new webpack.IgnorePlugin(/react-collapse/),
  new webpack.IgnorePlugin(/react-motion/),
  new webpack.IgnorePlugin(/react-scroll/)
];

/** *********end********** */

/** **********rules for non production***** */
if (!isProduction) {
  rules.push({
    test: /\.scss$/,
    exclude: /node_modules/,
    use: [
      'style-loader',
      // Using source maps breaks urls in the CSS loader
      // https://github.com/webpack/css-loader/issues/232
      // This comment solves it, but breaks testing from a local network
      // https://github.com/webpack/css-loader/issues/232#issuecomment-240449998
      // 'css-loader?sourceMap',
      'css-loader',
      'postcss-loader',
      'sass-loader',
      {
        loader: 'stylefmt-loader',
        options: {
          config: '.stylelintrc'
        }
      }
    ]
  });
  plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new BundleAnalyzerPlugin({
      analyzerPort: 9999
    })
  );
}

/** *********end******** */
/** ** only for proudction rule starts ****** */
if (isProduction) {
  rules.push({
    test: /\.scss$/,
    loader: ExtractTextPlugin.extract({
      fallback: 'style-loader',
      use: 'css-loader!postcss-loader!sass-loader'
    })
  });
  plugins.push(
    new ExtractTextPlugin({
      filename: '[name].[contentHash].css',
      allChunks: true
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    }),
    new webpack.optimize.UglifyJsPlugin(),
    new HashOutput(), // used for MD5 hashing of assets
    new WebpackStableModuleIdAndHash(),
    new AssetsPlugin({
      filename: 'assetsManifestDesktop.json',
      path: path.resolve('./build'),
      prettyPrint: true
    })
  );
}

/** **************end *********** */
module.exports = {
  devtool: isProduction ? false : 'source-map',
  context: path.join(`${__dirname}/client`),
  entry: {
    main: 'app-desktop.js',
    home: 'desktop-containers/Index',
    wizardLandingPage: 'common-containers/WizardLandingPage',
    auth: 'desktop-containers/Auth',
    vendor: [
      'babel-polyfill',
      'fingerprint',
      'isomorphic-fetch',
      'moment',
      'moment-range',
      'react',
      'react-addons-css-transition-group',
      'react-cookie',
      'react-daterange-picker',
      'react-dom',
      'react-helmet',
      'react-redux',
      'react-router',
      'react-waypoint',
      'redux',
      'redux-thunk'
    ]
  },
  output: {
    path: isProduction ? path.join(`${__dirname}/build/desktop`) : path.join(`${__dirname}/dist/desktop`), // webpack cli output directory
    publicPath: '/assets/desktop/', // from where actually assets will be served.
    filename: isProduction ? '[name].[chunkhash].js' : '[name].js',
    chunkFilename: isProduction ? '[name].[chunkhash].js' : '[name].js'
  },
  module: {
    rules
  },
  performance: isProduction && {
    maxAssetSize: 100,
    maxEntrypointSize: 300,
    hints: 'warning'
  },
  resolve: {
    alias: {
      react: 'preact-compat',
      'react-dom': 'preact-compat',
      'create-react-class': 'preact-compat/lib/create-react-class',
      components: path.resolve(__dirname, 'client/desktop-components')
    },
    extensions: ['dev-server.js', '.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
    modules: [path.join(`${__dirname}/client`), path.join(`${__dirname}/node_modules`)]
  },
  plugins,
  devServer: {
    contentBase: './dist/desktop',
    historyApiFallback: {
      index: 'index.html'
    },
    port: 7000,
    compress: isProduction,
    inline: !isProduction,
    hot: !isProduction,
    disableHostCheck: true,
    host: '0.0.0.0'
  }
};
EN

回答 2

Stack Overflow用户

发布于 2018-09-07 19:02:40

以下两种可能的解决方案可能有助于解决此问题。

如果您正在使用,请删除webpack.config.js中的DedupePlugin

代码语言:javascript
复制
//new webpack.optimize.DedupePlugin()

通过添加allChunks: true来修复它

代码语言:javascript
复制
new ExtractTextPlugin({
  filename: cssFileName,
  allChunks: true
})

在您的Webpack配置中导入以下两个

代码语言:javascript
复制
  require('style-loader/lib/addStyles');
  require('css-loader/lib/css-base');

有关更多详细信息,请查看此github issue &github issue1

票数 5
EN

Stack Overflow用户

发布于 2021-06-24 02:38:32

也许你在html中的某个地方使用了<style>标签,因为webpack有一些问题,而你有这个警告。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52220666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档