parcel 打包一个小爬虫工具

你离开我真会死。 提交于 2020-02-26 14:38:21

使用 parcel打包工具, 打包一个爬虫应用

创建文件夹并安装相关包

npm i  parcel-bundler axios   parcel-plugin-static-files-copy   -D

最终项目结构大概如下:
dist为输出目录, 在该目录下执行index.js文件, 可以爬取config中自定义的网址, 并保存为t.html

开发时, npm run dev, 修改代码后再在dist目录下执行index, parcel打包速度还是很快的 

 

修改config中的网址, 程序会下载该网页

 

相关代码

package.json

将config放入静态文件中, 这样就不会被打包和压缩转码了, json文件默认会被转为js

并且设置打包输出为node格式, 因为用到axios还需要打包dep模块

https://github.com/elwin013/parcel-plugin-static-files-copy

{
  "staticFiles": {
    "staticPath": [
      {
        "staticOutDir": "config",
        "staticPath": "./src/config"
      }
    ]
  },
  "scripts": {
    "dev": "npx parcel ./src/index.js --target node --bundle-node-modules",
    "build": "npx parcel build ./src/index.js   --target node --bundle-node-modules  --experimental-scope-hoisting"
  },
  "devDependencies": {
    "axios": "^0.19.2",
    "parcel-bundler": "^1.12.4",
    "parcel-plugin-static-files-copy": "^2.3.1"
  }
}

 

index.js

node的模块对tree shaking不太友好, 这么明显都不能去除

const fs = require('fs')
const path = require('path')
const axios = require("axios")

// path 没用使用, 但最终输出还是有path....
// let p = path.resolve ('./config/config.json')
let p = './config/config.json'
let s = fs.readFileSync(p,'utf8')
let {url} = JSON.parse(s)
console.log(url)
axios.get(url).then(
    resp=>{
        fs.writeFileSync('./t.html',resp.data,'utf8')
        console.log('end----22')
    }
)

config.json

简单配置信息

{
    "hello":"world",
    "url":"http://www.baidu.com"
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!