1.1.0 • Published 5 years ago

fetch-write-webpack-plugin v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Webpack plugin that would fetch the data source and write the file on build time 🚀

Install

npm i -D fetch-write-webpack-plugin
yarn add --dev fetch-write-webpack-plugin

This is a webpack plugin that could fetch the data on the other server and generate the corresponding files that you want bundle at the build time.

Usage

The plugin support fetch multiple data sources and build them each other before the compile.

webpack.config.js

const FetchWritePlugin = require('fetch-write-webpack-plugin')

module.exports = {
  entry: 'index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'index_bundle.js'
  },
  plugins: [
    new FetchWritePlugin([
      {
        fetchOpts: {
          url: 'http://localhost:4080/get/style',
          retryOn: true,
          headers: {
            'Content-Type': 'application/json'
          }
        },
        output: {
          name: 'style.json',
          basePath: './static/bundle'
        }
      },
      {
        fetchOpts: {
          url: 'http://localhost:4080/get/name',
          retryOn: true,
          headers: {
            'Content-Type': 'application/json'
          }
        },
        output: {
          name: 'name',
          ext: 'json'
        }
      }
    ])
  ]
}

This will fetch to the data from http://host/data/you/want and generate the file at ./static/name.json.

If you have multiple data sources and file you want to generate on run time, you could use an array to wrap all configure.

Want to know more detail. Ref

Options

The plugin's signature

webpack.config.js

module.exports = {
  plugins: [new CopyPlugin([{fetchOpts, output}, ...])]
}

Fetch Options

NameTypeDefaultDescription
urlstringundefineddata source's url
methodstringGETfetch method
headersanyundefinedhttp headers
bodyanyundefinedhttp body
retryOnbooleanfalsedetermine the retry fetch whether turn on or not
retriesnumber3retry count only work while the retry on is true
retryDelaynumber1000retry delay only work while the retry on is true

Output Options

NameTypeDefaultDescription
namestringundefinedfilename
extstringundefinedfile extension
basePathstringwebapck output pathoutput file base path

Roadmap

Show your support

Give a ⭐️ if this project helped you!

📝 License

MIT licensed.