support special imports

This commit is contained in:
Iuri Matias 2017-12-10 09:06:11 -05:00
parent 038f57e7d2
commit 92e22ea519
3 changed files with 72 additions and 42 deletions

View File

@ -1,10 +1,3 @@
/*jshint esversion: 6 */
//var Ipfs = require('./ipfs.js');
//=========================================================
// Embark Smart Contracts
//=========================================================
var EmbarkJS = {};
EmbarkJS.isNewWeb3 = function() {

View File

@ -3,6 +3,7 @@ var File = require('./file.js');
var Plugins = require('./plugins.js');
var utils = require('../utils/utils.js');
var Npm = require('../pipeline/npm.js');
let async = require('async');
let currentWeb3Version = require('../../package.json').dependencies.web3.replace("^","");
const webpack = require("webpack");
@ -344,29 +345,33 @@ Config.prototype.loadFile = function(files) {
if (file.indexOf('.js') >= 0) {
//if (file.indexOf('.js') >= 0) {
readFiles.push(new File({filename: file, type: "custom", path: file, resolver: function(callback) {
readFiles.push(new File({filename: file, type: "custom", path: file, resolver: function(fileCallback) {
console.log("---");
console.log(fs.dappPath());
console.log(file);
console.log("---");
let importsList = {};
async.waterfall([
function findImports(next) {
webpack({
entry: utils.joinPath(fs.dappPath(), file),
output: {
//libraryTarget: 'umd',
libraryTarget: 'umd',
path: utils.joinPath(fs.dappPath(), '.embark'),
//filename: 'my-first-webpack.bundle.js'
filename: file
},
externals: function(context, request, callback) {
if (request == "MyLib") {
console.log("MyLib Detected!");
return callback(null, "{foo: 'hello'}");
}
if (request == "Embark/EmbarkJS") {
console.log("EmbarkJS Detected!");
return callback(null, "{foo: 'hello'}");
}
if (request === utils.joinPath(fs.dappPath(), file)) {
callback();
} else {
if (request === "Embark/EmbarkJS") {
importsList["Embark/EmbarkJS"] = fs.embarkPath("js/embark.js");
} else if (request === "Embark/test") {
importsList["Embark/test"] = fs.embarkPath("js/test.js");
}
callback(null, "amd " + Math.random());
}
},
module: {
rules: [
@ -380,11 +385,44 @@ Config.prototype.loadFile = function(files) {
]
}
}).run((err, stats) => {
console.log(err, stats);
//return callback(fs.readFileSync('./.embark/my-first-webpack.bundle.js').toString());
return callback(fs.readFileSync('./.embark/' + file).toString());
next();
});
},
function runWebpack(next) {
webpack({
entry: utils.joinPath(fs.dappPath(), file),
output: {
libraryTarget: 'umd',
path: utils.joinPath(fs.dappPath(), '.embark'),
filename: file
},
resolve: {
alias: importsList
},
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
}
]
}
}).run((err, stats) => {
next();
});
}
], function(err, _result) {
fileCallback(fs.readFileSync('./.embark/' + file).toString());
});
}}));
} else {
readFiles.push(new File({filename: file, type: "dapp_file", path: file}));
}

View File

@ -2,8 +2,8 @@
import $ from './_vendor/jquery.min';
//import EmbarkJS from 'Embark/EmbarkJS';
import Test2 from 'MyLib';
import EmbarkJS from 'Embark/EmbarkJS';
console.log(EmbarkJS);
import test3 from './foo';
import './foo.css';
@ -20,7 +20,6 @@ var addToLog = function(id, txt) {
$(document).ready(function() {
console.log([1,2,3].map(v => v + 1));
alert('hello');
window.test_2 = Test2;
$("#blockchain button.set").click(function() {
var value = parseInt($("#blockchain input.text").val(), 10);