我想知道您如何在项目中使用npm作为程序包管理器吗?
我的项目使用以下文件结构:
.
|_ site/
| |_ bower_components/
| |_ index.html
| |_ assets/
| |_ js/
| |_ scripts.js
|
|_ dist/
|_ node_modules/
| |_ underscore
| |_ underscore-min.js
|
|_ gulpfile.js
|_ package.json
|_ ...
我使用此gulp任务进行服务/浏览器刷新:
gulp.task('serve', function () {
browserSync.init({
server: {
baseDir: ['site']
},
notify: false
});
gulp.watch(['site/**/*.html'], reload);
gulp.watch(['site/assets/css/**/*.css'], ['styles', reload]);
gulp.watch(['site/assets/images/**/*'], ['images']);
});
现在,当我想包括来自我的node_modules文件夹中的脚本(例如,下划线)时,我将执行以下操作:
<script src="../node_modules/underscore/underscore-min.js"></script>
但这会给我一个在underscore-min.js文件上找不到的404文件,但路径正确吗?在“ gulp服务任务”中,基本目录设置为“ site”,所以我想我无法使用“ ../”进入一个文件夹吗?
我不能告诉npm将其模块保存在其他文件夹中。另外,项目结构在我看来似乎很常见,所以我想知道有人会如何设置一个项目以使用软件包。
使用npm安装somePackage时,工作流程会如何?您是否将其从node_modules中复制到项目文件夹中(例如,assets / js / ..)。
使用browsersync,您可以传递“路线”选项:
// Since version 1.2.1
server: {
baseDir: "app",
routes: {
"/node_modules": "../node_modules"
}
}
然后就做
<script src="node_modules/underscore/underscore-min.js"></script>
据我的建议:只需使用browserify并让browsersync服务该dist
文件夹。您应该运行将其投入生产的代码,而不是上载dist文件夹并希望它能正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句