Webpack不包括ProvidePlugins [英] Webpack not including ProvidePlugins

查看:143
本文介绍了Webpack不包括ProvidePlugins的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用vue webpack模板的小型试用版Web应用程序( https://github.com/vuejs-templates/webpack ).我是webpack的新手,所以我假设我可以将new webpack.ProvidePlugin添加到插件中,并且可以在全球范围内使用,但是当我执行npm run dev时,会出现以下错误:

I have a small trial web application that I'm working on that uses the vue webpack template (https://github.com/vuejs-templates/webpack). I'm pretty new to webpack so I was assuming that I could add in to plugins a new webpack.ProvidePlugin and it would be available globally but when I do a npm run dev I get the following error:

/var/www/public/leadsStatsDashboard/liveleadstats/src/components/Hello.vue
  18:17  error  'd3' is not defined  no-undef

在我看来,它找不到d3参考.我不确定是否跳过了某些配置,还是有什么帮助,我们将不胜感激.这是我的文件的来源

Which sounds like to me that it can't find the d3 reference. I'm no sure if there's some configuration I skipped over or what but any help would be appreciated. Here is the source for my files

Webpack.dev.conf.js:

Webpack.dev.conf.js:

var path = require('path')
var config = require('../config')
var utils = require('./utils')
var webpack = require('webpack')
var projectRoot = path.resolve(__dirname, '../')

module.exports = {
  plugins: [
    new webpack.ProvidePlugin({
      d3: 'd3',
      crossfilter: 'crossfilter',
      dc: 'dc'
    })
  ],
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    publicPath: config.build.assetsPublicPath,
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.vue'],
    fallback: [path.join(__dirname, '../node_modules')],
    alias: {
      'src': path.resolve(__dirname, '../src'),
      'assets': path.resolve(__dirname, '../src/assets'),
      'components': path.resolve(__dirname, '../src/components'),
      'd3': path.resolve(__dirname, '../bower_components/d3/d3.min.js'),
      'crossfilter': path.resolve(__dirname, '../bower_components/crossfilter/crossfilter.min.js'),
      'dc': path.resolve(__dirname, '../bower_components/dcjs/dc.js')
    }
  },
  resolveLoader: {
    fallback: [path.join(__dirname, '../node_modules')]
  },
  module: {
    preLoaders: [
      {
        test: /\.vue$/,
        loader: 'eslint',
        include: projectRoot,
        exclude: /node_modules/
      },
      {
        test: /\.js$/,
        loader: 'eslint',
        include: projectRoot,
        exclude: /node_modules/
      }
    ],
    loaders: [
      {
        test: /\.vue$/,
        loader: 'vue'
      },
      {
        test: /\.js$/,
        loader: 'babel',
        include: projectRoot,
        exclude: /node_modules/
      },
      {
        test: /\.json$/,
        loader: 'json'
      },
      {
        test: /\.html$/,
        loader: 'vue-html'
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url',
        query: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      }
    ]
  },
  eslint: {
    formatter: require('eslint-friendly-formatter')
  },
  vue: {
    loaders: utils.cssLoaders()
  }
}

Hello.vue

Hello.vue

<template>
<div id="pieChartContainer">
</div>
</template>

<script>
    export default {
        data () {
           return {
           // note: changing this line won't causes changes
           // with hot-reload because the reloaded component
           // preserves its current state and we are modifying
           // its initial state.
           msg: 'Hello World! This is a test'
        }
     },
     ready () {
     console.log(d3.version)
     }
  }
 </script>

 <!-- Add "scoped" attribute to limit CSS to this component only -->
 <style scoped>
   h1 {
    color: #42b983;
   }
 </style>

推荐答案

您的错误不是从webpack发出的,而是从 eslint发出的.
实际上,我认为webpack部分可以正常工作!

Your error isn't emitted from webpack, but from eslint.
I think the webpack part works as it should, in fact!

no-undef抱怨您使用的是全局d3,而没有在任何地方导入或定义它.

no-undef complains that you are using the global d3 without importing or defining it somewhere.

好消息是,这很容易解决.使用以下三种可能性中的任何一种:

The good news is, that's easy to fix. Use any of the following three possibilities:

  • 只需将以下代码段添加到您的.eslintrc.js:

"globals": {
  "d3": true
}

  • ...或在隐式需要d3的文件中使用eslint注释(但这没有多大意义,因为您使其在全球范围内都可以使用,并且您需要在每个文件中都需要这样做使用全局变量):

  • ...or use eslint comments within the file that requires d3 implicitly (but that doesn't make much sense as you made it available globally and you would need to do this in every file you wish to use the global var):

    /* eslint-disable no-undef */
    

  • ...或者您可以在.eslintrc.js配置中放宽eslint规则:

  • ...or you could relax the eslint rule in your .eslintrc.js config:

    'rules': {
        // all other rules...
        'no-undef': 0
    }
    

  • 其他链接:

    • Direct link to the template's eslintrc file
    • The eslint 'standard' file the template extends
    • Further reading on eslint's no-undef rule

    这篇关于Webpack不包括ProvidePlugins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆