在纯npm版本的NW.js(node-webkit)应用程序中加载Bootstrap
我正在尝试使用NW.js构build一个简单的桌面应用程序。 受到包括这一个职位在内的一些职位的启发,我想尝试一种纯粹的npm
方法,没有任何bower
, grunt
或gulp
。
所以我继续使用安装了npm
模块的jquery
和bootstrap
库。 当我在我的应用程序的JavaScript注入模块像这样:
global.jQuery = $ = require("jquery"); var bootstrap = require("bootstrap");
我得到一个ReferenceError: document is not defined
从bootstrap
ReferenceError: document is not defined
。 我可以通过老式的方式加载HTML文件,虽然:
<script type="text/javascript" src="node_modules/bootstrap/dist/js/bootstrap.js"></script>
我问自己,如果这是一个愚蠢的想法加载引导低谷require
在脚本文件? 基本上,我不需要那里。 它可以通过html中的脚本标签加载。 什么是最佳实践? 我很困惑。
更新。 解决scheme
从@Kuf的build议,我创build了一个build.js
脚本
var copyfiles = require('copyfiles'); copyfiles(["node_modules/bootstrap/dist/js/bootstrap.js", "vendor/js"], true, function (err) { if (err) return console.error(err); }); copyfiles(["node_modules/bootstrap/dist/css/bootstrap.css", "vendor/css"], true, function (err) { if (err) return console.error(err); });
由prestart
钩子触发:
"scripts": { "build": "node build.js", "prestart": "npm run build" }
这使我可以通过方便的path访问bootstrap
资源:
<link href="vendor/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript" src="vendor/js/bootstrap.js"></script>
我想这个错误是因为你正在运行require而没有窗口上下文。 你在哪里运行的包括? 请注意,对于从node-main
运行的代码:
窗口:…在脚本加载时不可用,因为脚本在DOM窗口加载之前执行。 ( 来源 )
即使使用require导入JS,仍然需要包含css文件。我不认为有一个“干净的”或即开即用的解决scheme。 虽然你正在做的事情肯定会起作用,但会产生怪异而不自然的脚本path。
我会添加一个构build脚本 :
"scripts": { "build": "node build.js" }
在package.json中,将引导程序从node_modules复制到'/ dist /'中以使path更清洁。