2015-09-01 2 views
14

मेरे पास दो निर्देशिका src और compiled हैं। मैं src से compiled से Grunt Watch के साथ एक तरफा डेटा सिंक्रनाइज़ेशन सुनिश्चित करना चाहता हूं। मध्यस्थ चरण के रूप में, मैं *.less फ़ाइलों के साथ-साथ *.js फ़ाइलों का सबसेट संकलित करना चाहता हूं जो ES6 वाक्यविन्यास के साथ लिखे गए हैं।गंदगी-contrib-watch का उपयोग कर दो निर्देशिकाओं की गहरी, एक तरफा सिंक्रनाइज़ेशन। कोड काम करता है, लेकिन grunt-contrib-watch re-init time बहुत धीमा है

// NOTE: Spawn must be disabled to keep watch running under same context in order to dynamically modify config file. 
watch: { 
    // Compile LESS files to 'compiled' directory. 
    less: { 
    options: { 
     interrupt: true, 
     spawn: false, 
     cwd: 'src/less' 
    }, 
    files: ['**/*.less'], 
    tasks: ['less'] 
    }, 
    // Copy all non-ES6/LESS files to 'compiled' directory. Include main files because they're not ES6. Exclude LESS because they're compiled. 
    copyUncompiled: { 
    options: { 
     event: ['added', 'changed'], 
     spawn: false, 
     cwd: 'src' 
    }, 
    files: ['**/*', '!**/background/**', '!**/common/**', '!contentScript/youTubePlayer/**/*', '!**/foreground/**', '!**/test/**', '!**/less/**', '**/main.js'], 
    tasks: ['copy:compileSingle'] 
    }, 
    // Compile and copy ES6 files to 'compiled' directory. Exclude main files because they're not ES6. 
    copyCompiled: { 
    options: { 
     event: ['added', 'changed'], 
     spawn: false, 
     cwd: 'src/js' 
    }, 
    files: ['background/**/*', 'common/**/*', 'contentScript/youTubePlayer/**/*', 'foreground/**/*', 'test/**/*', '!**/main.js'], 
    tasks: ['babel:compileSingle'] 
    }, 
    // Whenever a file is deleted from 'src' ensure it is also deleted from 'compiled' 
    remove: { 
    options: { 
     event: ['deleted'], 
     spawn: false, 
     cwd: 'src' 
    }, 
    files: ['**/*'], 
    tasks: ['clean:compiledFile'] 
    } 
} 

    grunt.event.on('watch', function(action, filepath, target) { 
    // Determine which task config to modify based on the event action. 
    var taskTarget = ''; 
    if (action === 'deleted') { 
     taskTarget = 'clean.compiledFile'; 
    } else if (action === 'changed' || action === 'added') { 
     if (target === 'copyCompiled') { 
     taskTarget = 'babel.compileSingle'; 
     } else if (target === 'copyUncompiled') { 
     taskTarget = 'copy.compileSingle'; 
     } 
    } 

    if (taskTarget === '') { 
     console.error('Unable to determine taskTarget for: ', action, filepath, target); 
    } else { 
     // Drop src off of filepath to properly rely on 'cwd' task configuration. 
     grunt.config(taskTarget + '.src', filepath.replace('src\\', '')); 
    } 
    }); 

इन कार्यों उपयुक्त फ़ाइलें देखने के लिए:

मैं सफलतापूर्वक कार्यों के लिए जो मैं क्या आवश्यकता है लिखा है। इवेंट हैंडलर गतिशील रूप से cleancopy और babel कार्यों को संशोधित करता है जैसे कि वे फ़ाइलों को जोड़ा/बदला/हटाया जाता है।

हालांकि, मैं कई हजार फाइलें देख रहा हूं और घड़ी कार्य शुरू करने के लिए एक गैर-तुच्छ समय लेता है। मेरे उच्च अंत विकास पीसी प्रारंभ में 6+ सेकंड लेता है। यह मुद्दा इस तथ्य से उत्साहित है कि घड़ी कार्य प्रत्येक कार्य के बाद पुन: प्रारंभ होता है।

इसका मतलब यह है फिर वहाँ एक 6 + दूसरी अवधि जहां घड़ी fileB में किए गए संशोधन का पता लगाने में विफल रहता है अगर मैं दो फ़ाइलें, fileA और fileB, है और मैं fileA को संशोधित करने और बचाने के लिए है। इसके परिणामस्वरूप मेरी दो निर्देशिकाओं के बीच डी-सिंक्रनाइज़ेशन होता है।

मैं मेरी समस्या के बारे में इस GitHub मुद्दा मिल गया है, लेकिन यह अभी भी खुला है और अनुत्तरित है: https://github.com/gruntjs/grunt-contrib-watch/issues/443

GitHub पर चर्चा डाला गया है कि समस्या केवल हो सकता है जब spawn: false स्थापित किया गया है, लेकिन, Grunt Watch documentation के अनुसार:

यदि आपको अपनी कॉन्फ़िगरेशन को गतिशील रूप से संशोधित करने की आवश्यकता है, तो स्पॉन विकल्प को उसी संदर्भ के तहत चलने के लिए अक्षम होना चाहिए।

इस तरह, मेरा मानना ​​है कि मुझे spawn: false का उपयोग जारी रखने की आवश्यकता है।

मुझे यह मानना ​​है कि यह ग्रंट कार्यों के लिए एक सुंदर मानक प्रक्रिया है। क्या में यहां कुछ भूल रहा हूँ? क्या इस उद्देश्य के लिए वॉच कार्य अनुचित है? अन्य विकल्प?

+0

क्या आपने [grunt-newer] (https://www.npmjs.com/package/grunt-newer) पर एक नज़र डाली? ऐसा लगता है कि खुले मुद्दे में * लोके * का उल्लेख किया गया है। – DavidDomain

+0

पिछले दो या दो घंटे के लिए ग्रंट-न्यूर के साथ खेल रहे थे। यह संकलित कोड के लिए अच्छी तरह से काम करता है, लेकिन अगर किसी फ़ाइल को 'src' से 'compiled' में परिवर्तन की आवश्यकता के बिना कॉपी किया गया है तो फ़ाइल का 'अंतिम संशोधित' समय अपडेट नहीं किया गया है। परिणामस्वरूप हर बार कार्य चलने पर फ़ाइल को शामिल किया जाता है। अगर मुझे इसे ठीक करने का कोई तरीका मिल सकता है तो ग्रंट-न्यूर पर्याप्त होगा। –

+1

क्या आप [jit-grunt] (https://github.com/shootaroo/jit-grunt) का उपयोग करते हैं? इससे घड़ी के कार्य को तेज करने में मदद मिलेगी –

उत्तर

4

ठीक है, तो मेरे पास एक कामकाजी समाधान है, लेकिन यह सुंदर नहीं है।

मैंने समाधान के साथ सहायता के लिए grunt-newer का उपयोग कर समाप्त किया। दुर्भाग्यवश, यह grunt-contrib-copy के साथ अच्छी तरह से नहीं खेलता है क्योंकि फ़ाइल की प्रतिलिपि अपने अंतिम संशोधित समय को अपडेट नहीं करती है और इसलिए ग्रंट-न्यूर 100% समय निष्पादित करेगा।

तो, मैं घुरघुराना-योगदान कॉपी काँटेदार और अंतिम संशोधित समय अद्यतन करने की अनुमति के लिए एक विकल्प जोड़ा गया: https://github.com/MeoMix/grunt-contrib-copy

इसी के साथ

, मैं अब लिखने में सक्षम हूँ:

// NOTE: Spawn must be disabled to keep watch running under same context in order to dynamically modify config file. 
watch: { 
    // Compile LESS files to 'compiled' directory. 
    less: { 
    options: { 
     interrupt: true, 
     cwd: 'src/less' 
    }, 
    files: ['**/*.less'], 
    tasks: ['less'] 
    }, 
    // Copy all non-ES6/LESS files to 'compiled' directory. Include main files because they're not ES6. Exclude LESS because they're compiled. 
    copyUncompiled: { 
    options: { 
     event: ['added', 'changed'], 
     cwd: 'src' 
    }, 
    files: ['**/*', '!**/background/**', '!**/common/**', '!contentScript/youTubePlayer/**/*', '!**/foreground/**', '!**/test/**', '!**/less/**', '**/main.js'], 
    tasks: ['newer:copy:compiled'] 
    }, 
    // Compile and copy ES6 files to 'compiled' directory. Exclude main files because they're not ES6. 
    copyCompiled: { 
    options: { 
     event: ['added', 'changed'], 
     cwd: 'src/js' 
    }, 
    files: ['background/**/*', 'common/**/*', 'contentScript/youTubePlayer/**/*', 'foreground/**/*', 'test/**/*', '!**/main.js'], 
    tasks: ['newer:babel:compiled'] 
    }, 
    // Whenever a file is deleted from 'src' ensure it is also deleted from 'compiled' 
    remove: { 
    options: { 
     event: ['deleted'], 
     spawn: false, 
     cwd: 'src' 
    }, 
    files: ['**/*'], 
    tasks: ['clean:compiledFile'] 
    } 
} 

grunt.event.on('watch', function(action, filepath) { 
    if (action === 'deleted') { 
    // Drop src off of filepath to properly rely on 'cwd' task configuration. 
    grunt.config('clean.compiledFile.src', filepath.replace('src\\', '')); 
    } 
}); 

अब कॉपी करने ईएस 6 फाइलों के साथ-साथ गैर-कम/गैर-ईएस 6 फाइलें तब होती हैं जब 'src' 'dest' से नई है।'

दुर्भाग्य से,' src 'से हटाए जाने पर गलती-नए को वास्तव में एक डिलीट ऑपरेशन को सिंक करने के लिए समर्थन नहीं है। इसलिए, मैं 'पिछले' ऑपरेशन के लिए अपने पिछले कोड का उपयोग करना जारी रखता हूं। यह अभी भी एक ही त्रुटि है जहां एक हटाई जाने के बाद घड़ी कार्य एक पल के लिए निष्क्रिय हो जाएगा।

संबंधित मुद्दे