From 92e22ea519dfeb863140abc96e92cd4e3bfd78d2 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 10 Dec 2017 09:06:11 -0500 Subject: [PATCH] support special imports --- js/embark.js | 7 --- lib/core/config.js | 102 ++++++++++++++++++++++++----------- test_app/app/js/new_index.js | 5 +- 3 files changed, 72 insertions(+), 42 deletions(-) diff --git a/js/embark.js b/js/embark.js index e0b53c5c1..cd69dc8a0 100644 --- a/js/embark.js +++ b/js/embark.js @@ -1,10 +1,3 @@ -/*jshint esversion: 6 */ -//var Ipfs = require('./ipfs.js'); - -//========================================================= -// Embark Smart Contracts -//========================================================= - var EmbarkJS = {}; EmbarkJS.isNewWeb3 = function() { diff --git a/lib/core/config.js b/lib/core/config.js index 56f04bad4..f3b8d2621 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -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,47 +345,84 @@ 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("---"); - webpack({ - entry: utils.joinPath(fs.dappPath(), file), - output: { - //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'}"); - } - callback(); - }, - module: { - rules: [ - { - test: /\.css$/, - use: [ - { loader: "style-loader" }, - { loader: "css-loader" } + + let importsList = {}; + async.waterfall([ + function findImports(next) { + webpack({ + entry: utils.joinPath(fs.dappPath(), file), + output: { + libraryTarget: 'umd', + path: utils.joinPath(fs.dappPath(), '.embark'), + filename: file + }, + externals: function(context, request, callback) { + 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: [ + { + test: /\.css$/, + use: [ + { loader: "style-loader" }, + { loader: "css-loader" } + ] + } ] } - ] + }).run((err, stats) => { + 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(); + }); + } - }).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()); + + ], function(err, _result) { + fileCallback(fs.readFileSync('./.embark/' + file).toString()); }); + }})); + } else { readFiles.push(new File({filename: file, type: "dapp_file", path: file})); } diff --git a/test_app/app/js/new_index.js b/test_app/app/js/new_index.js index 64fe318d3..7ae34e164 100644 --- a/test_app/app/js/new_index.js +++ b/test_app/app/js/new_index.js @@ -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);