golang生成webpack打包文件

EasyNetCN · · 615 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

main.go

package main

import (
    "fmt"
    "html/template"
    "io/ioutil"
    "os"
    "path"
    "strings"

    "gopkg.in/yaml.v3"
)

var (
    skips = map[string]string{"yd-layout": "yd-layout"}
)

func main() {
    config, err := NewConfiguration()

    if err != nil {
        fmt.Println("can not read configuration file,err:", err)

        return
    }

    if files, err := ioutil.ReadDir(config.Src); err != nil {
        fmt.Println("can not read src directory,err:", err)

        return
    } else {
        components := make(map[string]string)

        for _, file := range files {
            if file.IsDir() && (strings.HasPrefix(file.Name(), "yd-") ||
                strings.HasPrefix(file.Name(), "ydh-") ||
                strings.HasPrefix(file.Name(), "ydo-") ||
                strings.HasPrefix(file.Name(), "ydp-")) {
                if _, ok := skips[file.Name()]; !ok {
                    components[file.Name()] = file.Name()
                }
            }
        }

        fmt.Println("component: ", len(components))

        if err := RemoveDist(config.Dist, components); err != nil {
            fmt.Println("can not remove dist file,err:", err)
        }

        if t, err := template.ParseFiles("tpl/entry.tpl"); err != nil {
            if err != nil {
                fmt.Println("parse file err:", err)
                return
            }
        } else {
            for component := range components {
                fileSb := new(strings.Builder)

                fileSb.WriteString(config.Dist)
                fileSb.WriteString("/")
                fileSb.WriteString(component)
                fileSb.WriteString(".js")

                comment := "ERP"

                if strings.HasPrefix(comment, "ydh-") {
                    comment = "帮助中心"
                } else if strings.HasPrefix(comment, "ydo-") {
                    comment = "管理平台"
                } else if strings.HasPrefix(comment, "ydp-") {
                    comment = "药学服务"
                }

                f, err := os.OpenFile(fileSb.String(), os.O_CREATE|os.O_WRONLY, 0666)

                if err != nil {
                    fmt.Println("can not create output file,err:", err)

                    return
                }

                defer f.Close()

                if err := t.Execute(f, &Component{Name: UpperCamelCase(component), Path: component, Comment: comment}); err != nil {
                    fmt.Println("There was an error:", err.Error())
                }
            }
        }
    }
}

type Configuration struct {
    Src  string `yaml:"src"`
    Dist string `yaml:"dist"`
}

func NewConfiguration() (*Configuration, error) {
    conf := new(Configuration)

    data, err := ioutil.ReadFile("config.yml")

    if err != nil {
        return conf, err
    }

    err = yaml.Unmarshal(data, &conf)

    return conf, err
}

type Component struct {
    Name    string
    Path    string
    Comment string
}

func RemoveDist(dist string, components map[string]string) error {
    if files, err := ioutil.ReadDir(dist); err != nil {
        fmt.Println("can not read src directory,err:", err)

        return err
    } else {
        for _, file := range files {
            if file.Mode().IsRegular() && (strings.HasPrefix(file.Name(), "yd-") ||
                strings.HasPrefix(file.Name(), "ydh-") ||
                strings.HasPrefix(file.Name(), "ydo-") ||
                strings.HasPrefix(file.Name(), "ydp-")) {
                if _, ok := components[file.Name()[0:strings.LastIndex(file.Name(), ".")]]; !ok {
                    if err := os.Remove(path.Join(dist, file.Name())); err != nil {
                        fmt.Println("can not remove dist file,file: ", path.Join(dist, file.Name()), " err: ", err)
                    }
                }
            }
        }

        return nil
    }
}

func UpperCamelCase(txt string) string {
    sb := new(strings.Builder)

    strs := strings.Split(txt, "-")

    for _, str := range strs {
        sb.WriteString(strings.ToUpper(string(str[0])))
        sb.WriteString(str[1:])
    }

    return sb.String()
}

tpl/entry.tpl


// {{.Comment}}
import {{.Name}} from './components/{{.Path}}';

import locale from './locale/index';

import style from './directives/style';


const components = {
    // {{.Comment}}
    {{.Name}}
};

const directives = {
    display: style.display,
    width: style.width,
    height: style.height,
    margin: style.margin,
    padding: style.padding,
    font: style.font,
    color: style.color,
    'bg-color': style.bgColor
};

const iview_pro = {
    ...components
};

const install = function (Vue, opts = {}) {
    if (install.installed) return;
    locale.use(opts.locale);
    locale.i18n(opts.i18n);

    Object.keys(iview_pro).forEach(key => {
        Vue.component(key, iview_pro[key]);
    });
    
    Object.keys(directives).forEach(key => {
        Vue.directive(key, directives[key]);
    });

    Vue.prototype.$IVIEWPRO = {
        size: opts.size || '',
        transfer: 'transfer' in opts ? opts.transfer : ''
    };
};

// auto install
if (typeof window !== 'undefined' && window.Vue) {
    install(window.Vue);
}

const API = {
    version: process.env.VERSION, // eslint-disable-line no-undef
    locale: locale.use,
    i18n: locale.i18n,
    install,
    ...components
};

API.lang = (code) => {
    const langObject = window['iview/locale'].default;
    if (code === langObject.i.locale) locale.use(langObject);
    else console.log(`The ${code} language pack is not loaded.`); // eslint-disable-line no-console
};

//module.exports.default = module.exports = API;   // eslint-disable-line no-undef

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:EasyNetCN

查看原文:golang生成webpack打包文件

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

615 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传