This commit is contained in:
Jonathan Rainville 2018-05-08 09:04:53 -04:00
parent 1fe6f4ee2c
commit af38383e0f
1 changed files with 143 additions and 131 deletions

View File

@ -39,12 +39,12 @@ class Pipeline {
const context = {}; const context = {};
context.console = console; context.console = console;
context.console.log = this.log; context.console.log = this.log;
context.console.warn = this.log; context.console.warn = this.log;
context.console.info = this.log; context.console.info = this.log;
context.console.debug = this.log; context.console.debug = this.log;
context.console.trace = this.log; context.console.trace = this.log;
context.console.dir = this.log; context.console.dir = this.log;
} }
log() { log() {
@ -56,7 +56,7 @@ class Pipeline {
this.buildContracts(contractsJSON); this.buildContracts(contractsJSON);
self.buildWeb3JS(function(err) { self.buildWeb3JS(function (err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -67,145 +67,145 @@ class Pipeline {
importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js'); importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js');
self.pluginImports.forEach(function (importObject) { self.pluginImports.forEach(function (importObject) {
let [importName, importLocation] = importObject; let [importName, importLocation] = importObject;
importsList[importName] = importLocation; importsList[importName] = importLocation;
}); });
for (let contractName in contractsJSON) { for (let contractName in contractsJSON) {
let contractCode = self.buildContractJS(contractName); let contractCode = self.buildContractJS(contractName);
let filePath = fs.dappPath(".embark", contractName + '.js'); let filePath = fs.dappPath(".embark", contractName + '.js');
fs.writeFileSync(filePath, contractCode); fs.writeFileSync(filePath, contractCode);
importsList["Embark/contracts/" + contractName] = filePath; importsList["Embark/contracts/" + contractName] = filePath;
} }
// limit:1 due to issues when downloading required files such as web3.js // limit:1 due to issues when downloading required files such as web3.js
async.eachOfLimit(self.assetFiles, 1, function (files, targetFile, cb) { async.eachOfLimit(self.assetFiles, 1, function (files, targetFile, cb) {
// limit:1 due to issues when downloading required files such as web3.js // limit:1 due to issues when downloading required files such as web3.js
async.mapLimit(files, 1, async.mapLimit(files, 1,
function(file, fileCb) { function (file, fileCb) {
file = new File(file); // Re-instantiate a File as through the process, we lose its prototype file = new File(file); // Re-instantiate a File as through the process, we lose its prototype
self.log("reading " + file.filename); self.log("reading " + file.filename);
if (file.filename.indexOf('.js') >= 0) { if (file.filename.indexOf('.js') >= 0) {
let realCwd; let realCwd;
async.waterfall([ async.waterfall([
function findImports(next) { function findImports(next) {
self.webpackRun(file.filename, {}, false, importsList, false, next); self.webpackRun(file.filename, {}, false, importsList, false, next);
}, },
function changeCwd(next) { function changeCwd(next) {
realCwd = utils.pwd(); realCwd = utils.pwd();
process.chdir(fs.embarkPath('')); process.chdir(fs.embarkPath(''));
next();
},
//function findImportsPhase2(next) {
// console.log("====> findImports_2");
// self.webpackRun(file.filename, {
// externals: function(context, request, callback) {
// if (request === utils.joinPath(fs.dappPath(), file.filename)) {
// callback();
// } else {
// //if (request.indexOf('Embark/contracts/') === 0) {
// // let contractName = request.split('/')[2];
// // let contractCode = self.buildContractJS(contractName);
// // let filePath = utils.joinPath(fs.dappPath(), ".embark", contractName + '.js');
// // fs.writeFileSync(filePath, contractCode);
// // importsList[request] = filePath;
// //}
// callback(null, "amd " + Math.random());
// }
// }
// }, true, importsList, next);
//},
function runWebpack(next) {
self.webpackRun(file.filename, {}, true, importsList, true, next);
},
function changeCwdBack(next) {
process.chdir(realCwd);
next();
},
function checkFile(next) {
fs.access('./.embark/' + file.filename, (err) => {
if (err) {
self.log("couldn't find file: " + file.filename);
return next("couldn't find file: " + file.filename);
}
next(); next();
}); },
},
function readFile(next) { //function findImportsPhase2(next) {
fs.readFile('./.embark/' + file.filename, (err, data) => { // console.log("====> findImports_2");
if (err) { // self.webpackRun(file.filename, {
return next(err); // externals: function(context, request, callback) {
} // if (request === utils.joinPath(fs.dappPath(), file.filename)) {
next(null, data.toString()); // callback();
}); // } else {
}, // //if (request.indexOf('Embark/contracts/') === 0) {
// // let contractName = request.split('/')[2];
// // let contractCode = self.buildContractJS(contractName);
// // let filePath = utils.joinPath(fs.dappPath(), ".embark", contractName + '.js');
// // fs.writeFileSync(filePath, contractCode);
// // importsList[request] = filePath;
// //}
// callback(null, "amd " + Math.random());
// }
// }
// }, true, importsList, next);
//},
function runPluginsOnContent(fileContent, next) { function runWebpack(next) {
self.runPlugins(file, fileContent, next); self.webpackRun(file.filename, {}, true, importsList, true, next);
},
function changeCwdBack(next) {
process.chdir(realCwd);
next();
},
function checkFile(next) {
fs.access('./.embark/' + file.filename, (err) => {
if (err) {
self.log("couldn't find file: " + file.filename);
return next("couldn't find file: " + file.filename);
}
next();
});
},
function readFile(next) {
fs.readFile('./.embark/' + file.filename, (err, data) => {
if (err) {
return next(err);
}
next(null, data.toString());
});
},
function runPluginsOnContent(fileContent, next) {
self.runPlugins(file, fileContent, next);
}
], function (err, contentFile) {
if (err) {
process.chdir(realCwd);
self.log(err);
return fileCb(err);
}
fileCb(null, contentFile);
});
} else {
file.content(function (fileContent) {
self.runPlugins(file, fileContent, fileCb);
});
}
},
function (err, contentFiles) {
if (err) {
self.log('errors found while generating ' + targetFile);
}
let dir = targetFile.split('/').slice(0, -1).join('/');
self.log("creating dir " + self.buildDir + dir);
fs.mkdirpSync(self.buildDir + dir);
// if it's a directory
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
let targetDir = targetFile;
if (targetDir.slice(-1) !== '/') {
targetDir = targetDir + '/';
} }
], function(err, contentFile) { async.each(contentFiles, function (file, mapCb) {
if (err) { let filename = file.filename.replace(file.basedir + '/', '');
process.chdir(realCwd); self.log("writing file " + (self.buildDir + targetDir + filename).bold.dim);
self.log(err);
return fileCb(err); fs.copy(file.path, self.buildDir + targetDir + filename, {overwrite: true}, mapCb);
}, cb);
return;
}
let content = contentFiles.map(function (file) {
if (file === undefined) {
return "";
} }
return file.content;
}).join("\n");
fileCb(null, contentFile); self.log("writing file ASYNC " + (self.buildDir + targetFile).bold.dim);
}); fs.writeFile(self.buildDir + targetFile, content, cb);
} else {
file.content(function(fileContent) {
self.runPlugins(file, fileContent, fileCb);
});
} }
}, );
function (err, contentFiles) { },
if (err) {
self.log('errors found while generating ' + targetFile);
}
let dir = targetFile.split('/').slice(0, -1).join('/');
self.log("creating dir " + self.buildDir + dir);
fs.mkdirpSync(self.buildDir + dir);
// if it's a directory
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
let targetDir = targetFile;
if (targetDir.slice(-1) !== '/') {
targetDir = targetDir + '/';
}
async.each(contentFiles, function (file, mapCb) {
let filename = file.filename.replace(file.basedir + '/', '');
self.log("writing file " + (self.buildDir + targetDir + filename).bold.dim);
fs.copy(file.path, self.buildDir + targetDir + filename, {overwrite: true}, mapCb);
}, cb);
return;
}
let content = contentFiles.map(function (file) {
if (file === undefined) {
return "";
}
return file.content;
}).join("\n");
self.log("writing file ASYNC " + (self.buildDir + targetFile).bold.dim);
fs.writeFile(self.buildDir + targetFile, content, cb);
}
);
},
function (_err) { function (_err) {
callback(); callback();
}); });
@ -215,10 +215,16 @@ class Pipeline {
runPlugins(file, fileContent, fileCb) { runPlugins(file, fileContent, fileCb) {
const self = this; const self = this;
if (self.pipelinePlugins.length <= 0) { if (self.pipelinePlugins.length <= 0) {
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true}); return fileCb(null, {
content: fileContent,
filename: file.filename,
path: file.path,
basedir: file.basedir,
modified: true
});
} }
async.eachSeries(self.pipelinePlugins, async.eachSeries(self.pipelinePlugins,
function(plugin, pluginCB) { function (plugin, pluginCB) {
if (file.options && file.options.skipPipeline) { if (file.options && file.options.skipPipeline) {
return pluginCB(); return pluginCB();
} }
@ -231,7 +237,13 @@ class Pipeline {
if (err) { if (err) {
self.log(err.message); self.log(err.message);
} }
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true}); return fileCb(null, {
content: fileContent,
filename: file.filename,
path: file.path,
basedir: file.basedir,
modified: true
});
} }
); );
} }
@ -251,12 +263,12 @@ class Pipeline {
fs.dappPath('node_modules') fs.dappPath('node_modules')
] ]
}, },
externals: function(context, request, callback) { externals: function (context, request, callback) {
callback(); callback();
} }
}; };
let webpackOptions = utils.recursiveMerge(defaultOptions, options); let webpackOptions = utils.recursiveMerge(defaultOptions, options);
if (includeModules) { if (includeModules) {
webpackOptions.module = { webpackOptions.module = {