2015-11-12 22 views
1

從應用程序構建完成後,是否可以從外部JS文件獲取代碼,然後將其內聯粘貼到腳本標記中(在index.html中)?Grunt將外部JS編譯爲內聯HTML

例如,在下面兩個文件都旨在是相同的,但是我想的JS到在src/DIR外部來實現,並且內嵌內構建/ DIR:

SRC /index.html

<head> 
    <script src="long/minified/code.js"></script> 
</head> 

構建 /index.html

<head> 
    <script> 
    // long... minified code to be added inline here 
    </script> 
</head> 

長/精縮/code.js

(function(){var this,isSomeLong='minifiedCode';})(); 

Gruntfile.js

grunt.initConfig({ 
    pkg: grunt.file.readJSON('package.json'), 
     dist: { 
      src: [ 
       'long/minified/code.js', 

        ], 
      dest: 'build/index.html', 
     } 
}); 

這是可能的,我完全關閉,需要像grunt-include-source

回答

3

您可以使用grunt-processhtml輕鬆內嵌您scrips:

HTML

<head> 
<!-- build:js inline --> 
<script src="long/minified/code.js"></script> 
<!-- /build --> 
</head> 

Gruntfile任務:

grunt.initConfig({ 
    processhtml: { 
    dist: { 
     files: { 
     'build/index.html': ['src/index.html'] 
     } 
    } 
    } 
}); 
+0

你是最棒的傢伙。 – bruh