将Browserify集成到Cordova的最佳方法 [英] Best way to integrate Browserify into Cordova

查看:100
本文介绍了将Browserify集成到Cordova的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将浏览器集成到Cordova中.我做了以下事情:

I was trying to integrate Browserify into Cordova. I did the following:

  • 已安装Browserify:
    npm install -g browserify
  • 将index.js移至根目录:
    mv www/js/index.js.
  • 创建了一个名为 appBeforeBuild.sh 的钩子脚本,该脚本将 index.js 转换为 bundle.js :
    browserify索引.js -o www/js/bundle.js
    编辑-请在下面查看我的答案
  • 更新了 config.xml 以运行该钩子:
    < hook src ="appBeforeBuild.sh" type ="before_build"/>
  • 更新了 index.html 以包括 bundle.js 而不是 index.js :
    < script type ="text/javascript" src ="js/bundle.js"></script>
  • Installed Browserify:
    npm install -g browserify
  • Moved index.js into the root:
    mv www/js/index.js .
  • Created a hook script named appBeforeBuild.sh which turns index.js into bundle.js:
    browserify index.js -o www/js/bundle.js
    EDIT - Please see my answer below
  • Updated config.xml to run the hook:
    <hook src="appBeforeBuild.sh" type="before_build" />
  • Updated index.html to include bundle.js instead of index.js:
    <script type="text/javascript" src="js/bundle.js"></script>

这本可能是将Browserify集成到Cordova的不错的指南,但不幸的是它不起作用,因为编辑'index.js'不会触发重新编译.

This could be a nice guide for integrating Browserify into Cordova, but unfortunately it is not working, because editing 'index.js' does not trigger a recompile.

任何人都可以解释一下如何将index.js设置为一个文件检查构建依赖项,并触发 before_build 钩子?

推荐答案

我的问题中的清单很适合将Browserify集成到Cordova中,但是 [before_build] 脚本应予以纠正.以下是适用于Mac OSX的脚本:

The check list in my question is just fine for integrating Browserify into Cordova, but the [before_build] script should be corrected. The following is a script suitable for Mac OSX:

文件:appBeforeBuild.sh

echo "[before_build] Start"
b=$(stat -f "%Sm" -t "%Y%m%dT%H%M%S" index.js)
if [ -f timestamp_indexjs.txt ]; then
    a=$(cat timestamp_indexjs.txt)
    if [ $a == $b ]; then
        echo "- No change in index.js"
    else
        echo "- Calling Browserify (timestamp was changed)"
        echo $b>timestamp_indexjs.txt
        browserify index.js -o www/js/bundle.js
    fi
else
    echo "- Calling Browserify (First run, no timestamp)"
    echo $b>timestamp_indexjs.txt
    browserify index.js -o www/js/bundle.js
fi
echo "[before_build] End"

必须授予该文件执行权:

This file must be granted with execution rights:

chmod +x appBeforeBuild.sh

此脚本中的想法是确保仅在更改 index.js 时才调用Browserify.

The idea in this script is to make sure that Browserify is called only when index.js was changed.

提示:

  • timestamp_indexjs.txt 放入您的 .gitignore 文件中.
  • 调查Cordova问题时,请使用 -d 选项运行它,如 cordova -d build android
  • Put timestamp_indexjs.txt into your .gitignore file.
  • When investigating Cordova issues, run it with -d option, as in cordova -d build android

这篇关于将Browserify集成到Cordova的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆