Gulp注入方法抛出内存不足异常

时间:2019-02-28 12:16:07

标签: javascript node.js gulp gulp-inject

我正在使用gulp-inject插件将html文件注入anothe html fiile中。我已经使用以下任务来做到这一点。

gulp.task("inject:component:html", () => {
let paths = getSelectedComponentHtml();

var sources = gulp.src([path.join(CONFIG.dest, CONFIG.downloads, downloadDir, CONFIG.componentDownloadPath + 'js/ui-pattern.js'), path.join(CONFIG.dest, CONFIG.downloads, downloadDir, 'uxpl/components/ui-pattern.css')]);
var source = '';
var visitedDirectories = [];
if (takeHeaderHtml) {
    // isHeader = true;
    source = gulp.src(CONFIG.site + "/components/header-navigation.html");
} else {
    // isHeader = false;
    source = gulp.src(CONFIG.componentsSrc + '/index.html');
}
// var sources = gulp.src([path.join(CONFIG.dest, CONFIG.downloads, downloadDir, CONFIG.componentDownloadPath + 'js/ui-pattern.js'), path.join(CONFIG.dest, CONFIG.downloads, downloadDir, 'uxpl/components/ui-pattern.css')]);
var stream = source
    .pipe(inject(gulp.src(paths), {
        starttag: '<!-- inject:{{ext}} -->',

        transform: function (filePath, file, i, length) {
            let contents = '';
            try {
                console.log('In transform method');
                var url = filePath;
                var fileName = url.substring(url.lastIndexOf('/') + 1);
                console.log("arguments====" + arguments);

                var snippet = file.contents.toString('utf8');
                console.log("snippet====");

                contents = snippet;
                console.log("contents====");

                // return file contents as string
                let snipp = snippet.match(/\<\!--\s*html\s*:/gm);
                if (snipp && snipp.length) {
                    let snipp = snippet.substring(snippet.indexOf('<!--html:') + 9, snippet.indexOf('html!-->'));
                    contents = fs.readFileSync(snipp.trim(), 'utf8');
                    console.log("contents reading====");
                }
                let folderName = filePath.replace(fileName, '');
                console.log("folderName replace====");

                if (visitedDirectories.length == 0) {
                    contents = '<div class="showcase">\n' + contents + '\n';
                    console.log("In iff====");
                    visitedDirectories.push(folderName);
                } else if (visitedDirectories.indexOf(folderName) === -1) {
                    contents = '</div>\n <div class="showcase">\n' + contents + '\n';
                    console.log("In else====");
                    visitedDirectories.push(folderName);
                }
                if (i == length - 1) {
                    contents = contents + '</div>\n<div class="showcases-padding"></div>';
                }
                console.log("just before return ======");

            }catch(e){
                console.log("in catch block===>>"+e);
            }
            return contents.toString('utf8');
        }
    }))

    .pipe($.replace('/vendor', '../../../../vendor'))
    .pipe($.replace('/assets', '../../../../assets'))
    .pipe(gulp.dest(path.join(CONFIG.dest, CONFIG.downloads, downloadDir, CONFIG.componentDownloadPath)));
console.log("just before return stream ======");

return stream;

});

一切正常,但无法完成trasnform方法。

直到** console.log("just before return stream ======"); **为止,所有日志都可以正常工作,但是此后,我出现以下错误。

    0: ExitFrame [pc: 0000002ADA6DC5C1]
Security context: 0x03557309e6e1 <JSObject>
    1: exec [0000035573089231](this=0x020817aaadb9 <JSRegExp <String[26]: <!--\s*inject:html\b\s*-->>>,0x02fdbcf49f69 <Very long string[3289141]>)
    2: inject(aka inject) [000003A1B6355319] [C:\Users\scheripa\Documents\2019\UXPL_Master\origin\master\node_modules\gulp-inject\src\inject\index.js:190] [bytecode=000000EBD2958729 offset=19](this=0x00
ac6b3826f...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: 00007FF68F630EFA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4810
 2: 00007FF68F60A296 node::MakeCallback+4518
 3: 00007FF68F60AC80 node_module_register+2160
 4: 00007FF68F8A09BE v8::internal::FatalProcessOutOfMemory+846
 5: 00007FF68F8A08EF v8::internal::FatalProcessOutOfMemory+639
 6: 00007FF68FDDE954 v8::internal::Heap::MaxHeapGrowingFactor+11476
 7: 00007FF68FDDC6E8 v8::internal::Heap::MaxHeapGrowingFactor+2664
 8: 00007FF68F97F0CB v8::internal::Factory::AllocateRawWithImmortalMap+59
 9: 00007FF68F981BCD v8::internal::Factory::NewRawTwoByteString+77
10: 00007FF68FB95C58 v8::internal::Smi::SmiPrint+536
11: 00007FF68F893ECB v8::internal::StringHasher::UpdateIndex+219
12: 00007FF68FB03173 v8::internal::RegExpImpl::Exec+131
13: 00007FF68FA3B483 v8::internal::wasm::WasmInterpreter::Thread::Step+32195
14: 0000002ADA6DC5C1

我尝试给出--max-old-space-size = 4096,但仍然出现相同的错误。 如果有人有任何想法,请检查此问题。

0 个答案:

没有答案