簡體   English   中英

在具有多個子目錄的大型Javascript項目上使用grunt

[英]Using grunt on a large Javascript project with multiple subdirectories

我有一個帶有多個子目錄的Javascript項目,每個子目錄都有自己的單獨項目。 對於每個項目,我只能使用一個具有不同任務的大型Gruntfile,但是我寧願在每個子文件夾中都有一個Gruntfile。 典型的文件結構是這樣

main_folder/
    project_1
        src
            js/
        dist/
        doc/
        Gruntfile.js
        package.json
    package.json
    node_modules/

然后為每個項目重復文件結構

這是我在project_1中的Gruntfile

module.exports = function(grunt) {



    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            build: {
                src: 'src/<%= pkg.name %>.js',
                dest: 'build/<%= pkg.name %>.min.js'
            }
        }
    });

    // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-uglify');


    grunt.registerTask('default', ['uglify']);

};

它的任務少於我將要使用的任務,但錯誤仍然存​​在。 每當我嘗試運行grunt時,都會收到錯誤消息: Local Npm module "grunt-contrib-uglify" not found. Is it installed? Local Npm module "grunt-contrib-uglify" not found. Is it installed? ,即使我將它安裝在node_modules中。

如何指定node_modules的位置,還是必須在每個文件夾中重新安裝node_modules?

我認為,如果您更明確地告訴Grunt,在哪里可以找到插件,您仍然可以將所有內容保存在同一位置。 例如,以下是我的一個Gruntfile文件中的一些內容:

grunt.loadTasks tasks for tasks in grunt.file.expand '../node_modules/grunt-*/tasks'

附帶說明:我面對的選擇完全相同,但選擇卻相反:整個項目使用一個Gruntfile,並從每個項目子目錄創建一個符號鏈接。 事實證明,這是一種將事物放在一起的非常簡單的方法,並且回避了許多令人困惑的問題,例如您面臨的問題。

解決此問題的另一種好方法可能是使用subgrunt 它使您可以為子項目配置各種目標,並具有npmInstall選項,該選項“確定是否將為子項目運行npm安裝(從而安裝dev依賴項)。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM