首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack与PapaParse

Webpack与PapaParse
EN

Stack Overflow用户
提问于 2017-10-19 20:28:53
回答 1查看 1.7K关注 0票数 0

我一直试图在Vue的webpack模板(https://github.com/vuejs-templates/webpack)中使用PapaParse的worker选项(https://github.com/vuejs-templates/webpack),但我遇到了一个问题:

未定义的ReferenceError:未定义窗口

因此,我认为它需要将文件与包文件分开,所以我使用了像https://github.com/danderson00/webpack-worker这样的依赖项,并将其设置为从Worker文件初始化一个worker类。但我也犯了同样的错误,我不知道为什么。

这是我的工作

Vue文件:

代码语言:javascript
复制
import axios from 'axios'
import client from 'webpack-worker/client'
import squel from 'squel'
import FileSaver from 'filesaver.js'
export default {
  name: 'Country',
  data () {
    return {
      downloading: true,
      dl_progress: 0,
      parsing: false,
      processing: false,
      data: null,
      ipv4: [],
      meta: [],
      dict: {},
      selected_country: []
    }
  },
  mounted () {
    // Unfortunately, MaxMind site has no cross-origin header, so we are unable to download directly
    axios.get(require('../../assets/archive/GeoLite2-Country-CSV.zip'), {
      responseType: 'blob', // JSZIP only takes in binary data, so we set the response as blob (binary)
      onDownloadProgress: (progressEvent) => {
        this.dl_progress = Math.round((event.loaded * 100) / event.total)
      }
    })
      .then((response) => {
        this.progress = 100
        this.downloading = false
        this.parsing = true
        this.data = response.data
        this.parse()
      })
  },
  methods: {
    parse () {
      client(new Worker('country.js'), {
        data: this.data
      })
        .subscribe(progress => {
          console.log(progress)
        })
        .then(result => {
          console.log('got result')
        })
    },
    process () {
      let out = []
      this.ipv4.forEach((block) => {
        if (this.selected_country.includes(block.registered_country_geoname_id)) {
          out.push({cidr: block.network, comment: this.dict[block.registered_country_geoname_id]})
        }
      })
      let SQL = squel
                  .insert()
                  .into('cidr_list')
                  .setFieldsRows(out)
                  .toString()
      let blob = new Blob([SQL], {type: 'application/sql;charset=utf-8'})
      FileSaver.saveAs(blob, 'country.sql')
      this.$toast.open({
        message: 'Successfully downloaded country import file',
        type: 'is-success'
      })
    }
  }
}

工人档案:

代码语言:javascript
复制
import process from 'webpack-worker/process'
import Papa from 'papaparse'
import JSZip from 'jszip'
import _ from 'lodash'

process((params, emit) => {
  emit(0)
  let zip = new JSZip()
  zip.loadAsync(params.data)
    .then((zipContent) => {
      zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Blocks-IPv4.csv')
        .async('string')
        .then(data => {
          emit(25)
          Papa.parse(data, {
            header: true,
            complete: ipv4 => {
              emit(50)
              zip.file('GeoLite2-Country-CSV_20171003/GeoLite2-Country-Locations-en.csv')
                .async('string')
                .then(data => {
                  emit(75)
                  Papa.parse(data, {
                    header: true,
                    complete: meta => {
                      emit(100)
                      let dict = {}
                      meta.data.forEach(geo => {
                        dict[geo.geoname_id] = geo.country_name
                      })
                      return {
                        ipv4: ipv4.data,
                        meta: _.sortBy(meta.data, 'country_name'),
                        dict: dict
                      }
                    }
                  })
                })
            }
          })
        })
    })
})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-22 18:56:50

使用worker-loader并为工作文件设置规则解决了我的问题。

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

https://stackoverflow.com/questions/46838380

复制
相关文章

相似问题

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