资讯专栏INFORMATION COLUMN

基于requirejs的vue2项目 (三)

lijy91 / 568人阅读

摘要:这里是打包篇使用的是的进行打包思路是通过里面的进行项目的初打包因为和里面的引用是动态生成的所以无法对其进行处理这里我们用到了来进行文件整合具体看代码这里是通过配置文件来组装配置信息对设置了同步的模板进行打包这里是通过

这里是打包篇

使用的是requirejs的r.js进行打包

思路是,通过entrance.js里面的require.config进行项目的初打包

因为router.js和store.js里面的引用是动态生成的,所以r.js无法对其进行处理,这里我们用到了nodejs来进行文件整合

具体看代码

node2build.js

var fs = require("fs")
var cp = require("child_process")
var __config__ = require("./../dist/config")
var fromdir = (process.argv.length >= 3
  ? process.argv[2]
    ? process.argv[2]
    : ""
  : "dist")
var todir = (process.argv.length >= 4
  ? process.argv[3]
    ? process.argv[3]
    : ""
  : "pack")
var base = {
  appDir: "../" + fromdir,
  baseUrl: "./",
  dir: "../" + todir,
  // fileExclusionRegExp: "^*.less$",
  removeCombined: true,
  optimize: "uglify2",
  // uglify: {   ascii_only: true,   beautify: true,   preserveComments: false },
  // uglify2: {   output: {     ascii_only: true,     beautify: false, comments:
  // false   } },
  optimizeCss: "standard", // "standard",
  paths: {
    "libs": "libs",
    "vue": "libs/vue/vue",
    "vuex": "libs/vuex/vuex",
    "vue-router": "libs/vue-router/vue-router",
    "vue-popup": "libs/vue-popup/index",
    "jquery": "libs/jquery/jquery",
    "fastclick": "libs/fastclick/fastclick",
    "wind-dom": "libs/wind-dom/index",
    "__module__": "common/__module__",
    "__component__": "common/__component__",
    "__install__": "common/__install__",
    "__store__factory__": "common/__store__factory__",
    "detector": "common/detector",
    "calc": "common/calculate",
    "emitter": "common/mixins/emitter",
    "clickoutside": "common/utils/clickoutside",
    "isvisible": "common/utils/isvisible",
    "install": "components/install"
  },
  map: {
    "*": {
      "text": "libs/require-text/text"
    }
  },
  modules: [
    {
      name: "entrance",
      include: [
        "__module__",
        "__store__factory__",
        "libs/es6-promise/promise",
        "jquery",
        "vue",
        "vue-router",
        "detector",
        "calc",
        "fastclick",
        "libs/require-text/text",
        "store/transition"
      ],
      exclude: ["store/index", "router/index"]
    }
  ]
}

// 这里是通过配置文件来组装配置信息对设置了同步的模板进行打包
base.modules[0].include = base
  .modules[0]
  .include
  .concat(__config__.modules.filter(o => o.store).map(o => {
    return "store/modules/" + o.path + "/store"
  }))
base.modules[0].include = base
  .modules[0]
  .include
  .concat(__config__.modules.filter(o => o.sync).map(o => {
    return "business/" + o.path + "/index"
  }))

base.modules[0].include = base
  .modules[0]
  .include
  .concat(__config__.modules.filter(o => o.sync).map(o => {
    return "libs/require-text/text!business/" + o.path + "/tpl.html"
  }))

fs.writeFileSync("build/b.js", "(" + JSON.stringify(base) + ")")

var spawn = cp.spawn
var exec = cp.exec

//这里是通过控件台执行r.js的打包命令
node2build = spawn("node", ["node_modules/requirejs/bin/r.js", "-o", "build/b.js"])

node2build
  .stdout
  .on("data", function (data) {
    console.log("" + data)
  })
node2build
  .stderr
  .on("data", function (data) {
    console.log("" + data)
  })


//这里是控制台执行完成后进行的文件合并处理
node2build.on("exit", function (code, signal) {
  fs.unlinkSync("build/b.js")
  // 合并代码
  var entrance = fs.readFileSync(todir + "/entrance.js", "utf-8")


  var router = fs.readFileSync(todir + "/router/index.js", "utf-8")
  router = router.replace(/(define()(e,function)/, "$1"router/index",$2")


  var store = fs.readFileSync(todir + "/store/index.js", "utf-8")
  store = store.replace(/(define()(e,function)/, "$1"store/index",$2")

  //合并router和store
  entrance = entrance.replace(/(define([""]application[""])/, router + store + "$1")

  //将配置文件放在文件头处
  var config = fs.readFileSync(todir + "/config.js", "utf-8")
  entrance = config + entrance

  //删除多说的文件
  exec("rm -rf " + todir + "/store")
  exec("rm -rf " + todir + "/router")
  exec("rm -rf " + todir + "/components")
  exec("rm " + todir + "/build.txt")
  exec("rm " + todir + "/config.js")
  fs.writeFileSync(todir + "/entrance.js", entrance)

   
  // 处理html删除config.js的引入
  var html = fs.readFileSync(todir + "/index.html", "utf-8")
  html = html.replace("
", "")
  fs.writeFileSync(todir + "/index.html", html)
  console.log(`打包完成 (返回码 : ${code})`)
})

如果有什么问题可以留言质询,觉得有用就收藏吧

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/83034.html

相关文章

  • 基于requirejsvue2项目 (一)

    摘要:项目截图项目演示地址该项目主要是解决如何让不了解前端构建,并负责大部分业务逻辑的后端开发出一个单页应用觉得有用请给个推荐,谢谢最近做了一次小更新配置文件可以配置模块是否异步加载以及是否关联开发背景公司推进手机端项目,但目前开发模式依旧是后端 项目截图 项目演示地址 该项目主要是解决: 如何让不了解前端构建,并负责大部分业务逻辑的后端 开发出 一个单页应用 觉得有用请给个推荐,谢谢 最近...

    jackzou 评论0 收藏0
  • 基于requirejsvue2项目(二)

    摘要:减少服务器的请求对于和这两个特殊写发的文件因为的打包不识别,要进行特殊处理 上一节提完了构思和大体实现下面来看具体的 配置文件config 配置文件主要是是用来让后端开发自主添加页面,并通过该配置生成route和加载对应的store,这样就省去了后端去了解vue-router和vuex的用法了; 配置文件格式如下 这里采用umd格式开头是为了后续nodejs进行调用 (function...

    tianren124 评论0 收藏0
  • 前端资源系列(4)-前端学习资源分享&前端面试资源汇总

    摘要:特意对前端学习资源做一个汇总,方便自己学习查阅参考,和好友们共同进步。 特意对前端学习资源做一个汇总,方便自己学习查阅参考,和好友们共同进步。 本以为自己收藏的站点多,可以很快搞定,没想到一入汇总深似海。还有很多不足&遗漏的地方,欢迎补充。有错误的地方,还请斧正... 托管: welcome to git,欢迎交流,感谢star 有好友反应和斧正,会及时更新,平时业务工作时也会不定期更...

    princekin 评论0 收藏0
  • 大前端 - 收藏集 - 掘金

    摘要:是目前唯一一个支持同步调用的跨平台年度上最多的个项目前端掘金年接近尾声,在最近的几篇文章中,会整理总结一些年度开源项目。 JS 全栈教程 - 前端 - 掘金本课程是基于阮一峰的 js 全栈教程的视频版本,免费供大家观看... 2016 年 10 个最佳的 CodePen 作品 - 前端 - 掘金说到 CodePen,前端开发者们肯定不会陌生。如果说 Dribbble 是设计师们聚集的圣...

    honhon 评论0 收藏0
  • 前方来报,八月最新资讯--关于vue2&3最佳文章推荐

    摘要:哪吒别人的看法都是狗屁,你是谁只有你自己说了才算,这是爹教我的道理。哪吒去他个鸟命我命由我,不由天是魔是仙,我自己决定哪吒白白搭上一条人命,你傻不傻敖丙不傻谁和你做朋友太乙真人人是否能够改变命运,我不晓得。我只晓得,不认命是哪吒的命。 showImg(https://segmentfault.com/img/bVbwiGL?w=900&h=378); 出处 查看github最新的Vue...

    izhuhaodev 评论0 收藏0

发表评论

0条评论

lijy91

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<