最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

webpack构建错误TypeError:将循环结构转换为JSON

运维笔记admin10浏览0评论

webpack构建错误TypeError:将循环结构转换为JSON

webpack构建错误TypeError:将循环结构转换为JSON

我在使用Webpack构建和运行dev服务器时遇到错误。我使用Vue.js并使用vue-cli生成项目。为了测试,我使用Jest和npm test运行没有问题。

如果我运行npm run build我得到此错误输出:

ERROR in   TypeError: Converting circular structure to JSON
  
  - JSON.stringify
  
  - index.js:281 HtmlWebpackPlugin.postProcessHtml
    [corlex-photos-theme]/[html-webpack-plugin]/index.js:281:74
  
  - index.js:154 
    [corlex-photos-theme]/[html-webpack-plugin]/index.js:154:25
  
  - util.js:16 tryCatcher
    [corlex-photos-theme]/[bluebird]/js/release/util.js:16:23
  
  - promise.js:512 Promise._settlePromiseFromHandler
    [corlex-photos-theme]/[bluebird]/js/release/promise.js:512:31
  
  - promise.js:569 Promise._settlePromise
    [corlex-photos-theme]/[bluebird]/js/release/promise.js:569:18
  
  - promise.js:614 Promise._settlePromise0
    [corlex-photos-theme]/[bluebird]/js/release/promise.js:614:10
  
  - promise.js:693 Promise._settlePromises
    [corlex-photos-theme]/[bluebird]/js/release/promise.js:693:18
  
  - async.js:133 Async._drainQueue
    [corlex-photos-theme]/[bluebird]/js/release/async.js:133:16
  
  - async.js:143 Async._drainQueues
    [corlex-photos-theme]/[bluebird]/js/release/async.js:143:10
  
  - async.js:17 Immediate.Async.drainQueues
    [corlex-photos-theme]/[bluebird]/js/release/async.js:17:14
回答如下:

好的,解决方案是删除webpack.dev.conf.js和webpack.prod.conf.js文件中的HtmlWebpackPlugin的template选项。

现在文件看起来像这样:

webpack.dev.conf.js:

'use strict'
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder')

const HOST = process.env.HOST
const PORT = process.env.PORT && Number(process.env.PORT)

const devWebpackConfig = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({sourceMap: config.dev.cssSourceMap, usePostCSS: true})
  },
  // cheap-module-eval-source-map is faster for development
  devtool: config.dev.devtool,

  // these devServer options should be customized in /config/index.js
  devServer: {
    clientLogLevel: 'warning',
    historyApiFallback: true,
    hot: true,
    compress: true,
    host: HOST || config.dev.host,
    port: PORT || config.dev.port,
    open: config.dev.autoOpenBrowser,
    overlay: config.dev.errorOverlay
      ? {warnings: false, errors: true}
      : false,
    publicPath: config.dev.assetsPublicPath,
    proxy: config.dev.proxyTable,
    quiet: true, // necessary for FriendlyErrorsPlugin
    watchOptions: {
      poll: config.dev.poll
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': require('../config/dev.env')
    }),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: 'index.html',
      // HERE WAS TEMPLATE OPTION
      inject: true
    })
  ]
})

module.exports = new Promise((resolve, reject) => {
  portfinder.basePort = process.env.PORT || config.dev.port
  portfinder.getPort((err, port) => {
    if (err) {
      reject(err)
    } else {
      // publish the new Port, necessary for e2e tests
      process.env.PORT = port
      // add port to devServer config
      devWebpackConfig.devServer.port = port

      // Add FriendlyErrorsPlugin
      devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
        compilationSuccessInfo: {
          messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`]
        },
        onErrors: config.dev.notifyOnErrors
          ? utils.createNotifierCallback()
          : undefined
      }))

      resolve(devWebpackConfig)
    }
  })
})
发布评论

评论列表(0)

  1. 暂无评论