/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
"use strict";

const BaseWasmMainTemplatePlugin = require("../BaseWasmMainTemplatePlugin");
const WasmModuleTemplatePlugin = require("../wasm/WasmModuleTemplatePlugin");

class ReadFileCompileWasmTemplatePlugin {
	apply(compiler) {
		compiler.hooks.thisCompilation.tap(
			"ReadFileCompileWasmTemplatePlugin",
			compilation => {
				const generateLoadBinaryCode = path => `new Promise(function (resolve, reject) {
                      var {readFile} = require("fs");
                      var {join} = require("path");

                      try {
                        readFile(join(__dirname, ${
													path
												}), function(err, buffer){
                          if (err) return reject(err);
                          
                          // Fake fetch response
                          resolve({
                            arrayBuffer() {
                              return Promise.resolve(buffer)
                            }
                          });
                        });
                      } catch (err) {
                        reject(err);
                      }
                    });
                `;

				new BaseWasmMainTemplatePlugin().apply(
					compilation.mainTemplate,
					generateLoadBinaryCode
				);
				new WasmModuleTemplatePlugin().apply(
					compilation.moduleTemplates.javascript
				);
			}
		);
	}
}

module.exports = ReadFileCompileWasmTemplatePlugin;

@@ -508,38 +508,21 @@
 ject) %7B%0A
-                      
+%09%09%09%09%09
 var %7Brea
@@ -545,38 +545,21 @@
 (%22fs%22);%0A
-                      
+%09%09%09%09%09
 var %7Bjoi
@@ -585,118 +585,56 @@
 );%0A%0A
-                      try %7B%0A                        readFile(join(__dirname, $%7B%0A%09%09%09%09%09%09%09%09%09%09%09%09%09path%0A%09%09%09%09%09%09%09%09%09%09%09%09
+%09%09%09%09%09try %7B%0A%09%09%09%09%09%09readFile(join(__dirname, $%7Bpath
 %7D), 
@@ -660,34 +660,15 @@
 r)%7B%0A
-                          
+%09%09%09%09%09%09%09
 if (
@@ -696,452 +696,210 @@
 r);%0A
-                          %0A                          // Fake fetch response%0A                          resolve(%7B%0A                            arrayBuffer() %7B%0A                              return Promise.resolve(buffer)%0A                            %7D%0A                          %7D);%0A                        %7D);%0A                      %7D catch (err) %7B%0A                        reject(err);%0A                      %7D%0A                    %7D);%0A                
+%0A%09%09%09%09%09%09%09// Fake fetch response%0A%09%09%09%09%09%09%09resolve(%7B%0A%09%09%09%09%09%09%09%09arrayBuffer() %7B%0A%09%09%09%09%09%09%09%09%09return Promise.resolve(buffer)%0A%09%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%7D catch (err) %7B%0A%09%09%09%09%09%09reject(err);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%09
 %60;%0A%0A

fix indentation