为浏览器捆绑 node_modules 并保持 "app.js" 不变
Bundle node_modules for browser and keep "app.js" untouched
js 文件使用 npm 节点模块 greetings
我在浏览器中使用我的 app.js 和 browserify:
我的 index.html 看起来像这样:
我知道 app.js
文件的源代码现在嵌入在 bundle.js
.
文件中
我现在的要求是我的生产代码包含 app.js
和一个包含节点模块的附加库,如下所示:
这意味着客户端获得 app.js
文件和 bundle.js
文件,我现在可以像这样使用 app.js
中的节点模块:
基本上我只想要一个 bundle.js
用作我的 app.js
的库并以我可以将它们包含在浏览器端 JavaScript 的方式包含节点模块。
我也厌倦了使用客户端文件和模块加载器。 require.js
加载 node_modules 但根据 this thread 它不起作用。
您需要使用 browserify 的 -r
& -x
选项:https://github.com/substack/node-browserify#multiple-bundles
要编译您的代码,运行:
browserify app.js -r ./greetings -o dist/bundle.js
browserify app.js -x ./greetings -o dist/app.js
dist/bundle.js
将包含 greetings
模块,dist/app.js
将包含您的代码。
js 文件使用 npm 节点模块 greetings
我在浏览器中使用我的 app.js 和 browserify:
我的 index.html 看起来像这样:
我知道 app.js
文件的源代码现在嵌入在 bundle.js
.
我现在的要求是我的生产代码包含 app.js
和一个包含节点模块的附加库,如下所示:
这意味着客户端获得 app.js
文件和 bundle.js
文件,我现在可以像这样使用 app.js
中的节点模块:
基本上我只想要一个 bundle.js
用作我的 app.js
的库并以我可以将它们包含在浏览器端 JavaScript 的方式包含节点模块。
我也厌倦了使用客户端文件和模块加载器。 require.js
加载 node_modules 但根据 this thread 它不起作用。
您需要使用 browserify 的 -r
& -x
选项:https://github.com/substack/node-browserify#multiple-bundles
要编译您的代码,运行:
browserify app.js -r ./greetings -o dist/bundle.js
browserify app.js -x ./greetings -o dist/app.js
dist/bundle.js
将包含 greetings
模块,dist/app.js
将包含您的代码。