🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@lsky/preset-data

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lsky/preset-data

> 在编译期,在项目中,预设数据。

latest
Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
3
-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

@lsky/preset-data

在编译期,在项目中,预设数据。

使用方法

安装

$ yarn add @lsky/preset-data

添加 loader

babel-loader处增加PresetDataLoader

{
  test: /\.(js|jsx)$/,
  exclude: /node_modules/,
  use: [
    "babel-loader",
    {
      loader: "@lsky/preset-data/loader",
    },
  ],
},

添加 plugin

const PresetDataPlugin = require("@lsky/preset-data/plugin")
plugins: [
  new PresetDataPlugin([
    {
      name: "jobs", // 必须参数
      // -------
      // fetch 请求参数请参照 axios 配置
      url: "http://localhost:8030/test",  // required
      method: "get",
      //-------
      cb: (data) => {
        // axios response data
        // must return data
        return data.data
      },
    },
  ]),
  new HtmlWebPackPlugin({
    template: path.resolve(__dirname, "../index.ejs"),
    filename: path.resolve(__dirname, "../dist/index.html"),
  }),
],

PresetDataPlugin 使用说明

interface Option extends axios.AxiosRequestConfig {
  name: string;
  cb?: (responseData: any) => data:any;
  raw?: any;
}
new PresetDataPlugin(options: (Option | Option[]))
NameTypeDefaultDescription
namestringnull预设数据名称,用于项目中取值。注意,该字段必填。
cbfunctionnull处理数据
rawanynull如设置了 raw,将不通过 request 获取数据。注意,该优先级较高。

项目中使用预设数据

import presetDataFunc from "@lsky/preset-data"

console.log(presetDataFunc("jobs")) // 参数是 plugin 的 name
console.log(presetDataFunc())

Keywords

preset data in compiler

FAQs

Package last updated on 18 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts