简体   繁体   English

使用npm和grunt进行开发和生产的Javascript项目结构

[英]Javascript project structure for dev and production using npm and grunt

I am trying to structure javascript files in a project. 我正在尝试在项目中构建javascript文件。 I have used NPM to manage the modules and planning to use Grunt to concatenate and compress the js and css files for deployment. 我已经使用NPM来管理模块,并计划使用Grunt连接和压缩js和css文件以进行部署。

I am currently using the following structure 我目前正在使用以下结构

-[project root]   
-- [node modules] :packages such as requirejs, jquery, semantic-ui etc using npm  

--[war]  
---[Dev]   
----[css] multiple css files from modules (Question 2:?)  
----[js] multiple js files from modeuls (Question 2:?)

- Gruntfile.js :for concatenate and compress    
---[Production] -   
----[css]:This is where the compressed and concatenated css files are kept  
----[js] :This is where the compressed and concatenated js files are kept  

Question 1: Is the above approach to structure the project correct ? 问题1:上述构建项目的方法是否正确? Any other recommendations which allows to manage the packages, dev and production files. 允许管理软件包,开发和生产文件的任何其他建议。

Question 2: Can NPM or another tool allows me to pick up the js and css files from the [node modules] folder and place them to (dev>>css or dev>>js) folder ? 问题2:NPM或其他工具能否允许我从[node modules]文件夹中提取js和css文件并将它们放置到(dev >> css或dev >> js)文件夹中? If am doing this manually how do I track the versions ? 如果要手动执行此操作,如何跟踪版本? Seems like I am missing something here, there must be a better solution. 似乎我在这里缺少什么,必须有一个更好的解决方案。

Suggestions/recommendations/comments are much appreciated. 建议/建议/意见,不胜感激。

Thanks 谢谢

The question is a bit too wide for SO format, but in general your structure is good. 对于SO格式,这个问题有点过宽,但总的来说,您的结构很好。 Instead of copying files from node_modules , you have your own JavaScript files under js and you import/require them to your own files. 您无需在node_modules中复制文件,而是在js下拥有自己的JavaScript文件,然后将它们导入/需要到自己的文件中。

//foo.js
//ES6 style imports
import {Foo as Bar} from "biz";

//Common JS style requires
var Bar = require("biz");

//AMD style requires
require(["biz"], function (Bar) {

If you want to use your node_modules in a browser, you'll want to bundle them using Browserify , Webpack , Rollup or similar. 如果你想用你的node_modules在浏览器中,你要使用捆绑他们Browserify的WebPack汇总或相似。 To automate this, you can easily use Grunt tasks such as grunt-browserify together with grunt-watch . 要自动执行此操作,您可以轻松地将Grunt任务(例如grunt-browserifygrunt-watch一起使用。

Same applies for your CSS files: You store your own files under css and if you need CSS files from node_modules you can import them to your own files: if you are using some preprocessor (such as SASS or LESS), the preprocessors usually inline your imports when building the .css -file. CSS文件也是如此:您可以将自己的文件存储在css下,如果需要来自node_modules CSS文件, node_modules可以将它们导入到自己的文件中:如果使用的是预处理器(例如SASS或LESS),则预处理器通常会内联生成.css -file时导入。 If you are just using plain .css files, see grunt-css-import for example. 如果仅使用普通的.css文件,请参见例如grunt-css-import

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM