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 = {}; var EmbarkJS = {};
EmbarkJS.isNewWeb3 = function() { EmbarkJS.isNewWeb3 = function() {

View File

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

View File

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