From 33711c8db2e9adeea2bb0fa59249f2d3fdd7a0c9 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 7 Dec 2017 15:27:03 -0500 Subject: [PATCH] support imports --- lib/core/config.js | 53 ++++++++++++++++++++++++++++++++++++---- lib/core/fs.js | 3 ++- package.json | 2 ++ test_app/app/js/foo.css | 3 +++ test_app/app/js/foo.js | 4 +++ test_app/app/js/index.js | 10 ++++++++ test_app/embark.json | 3 ++- 7 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 test_app/app/js/foo.css create mode 100644 test_app/app/js/foo.js diff --git a/lib/core/config.js b/lib/core/config.js index 0d290e9e0..a71a72eeb 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -4,9 +4,8 @@ var Plugins = require('./plugins.js'); var utils = require('../utils/utils.js'); var Npm = require('../pipeline/npm.js'); let currentWeb3Version = require('../../package.json').dependencies.web3.replace("^",""); +const webpack = require("webpack"); -// TODO: add wrapper for fs so it can also work in the browser -// can work with both read and save var Config = function(options) { this.env = options.env; this.blockchainConfig = {}; @@ -336,11 +335,55 @@ Config.prototype.loadFiles = function(files) { //} else if (file === 'abi.js') { // readFiles.push({filename: file, content: "", path: file}); } else { - if (file.indexOf('.') >= 0) { + + if (file.indexOf('index.js') >= 0) { + //if (file.indexOf('.js') >= 0) { + readFiles.push(new File({filename: file, type: "custom", path: file, resolver: function(callback) { + 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'}"); + } + callback(); + }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { loader: "style-loader" }, + { loader: "css-loader" } + ] + } + ] + } + }).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()); + }); + }})); + } else { readFiles.push(new File({filename: file, type: "dapp_file", path: file})); - } else if (file[0] === '$') { - //readFiles.push(new File({filename: file, type: 'embark_internal', path: file})); } + + //if (file.indexOf('.') >= 0) { + // readFiles.push(new File({filename: file, type: "dapp_file", path: file})); + //} else if (file[0] === '$') { + // //readFiles.push(new File({filename: file, type: 'embark_internal', path: file})); + //} } }); diff --git a/lib/core/fs.js b/lib/core/fs.js index d61933faa..94a9c9f1c 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -46,5 +46,6 @@ module.exports = { readJSONSync: readJSONSync, writeJSONSync: writeJSONSync, existsSync: existsSync, - embarkPath: embarkPath + embarkPath: embarkPath, + dappPath: dappPath }; diff --git a/package.json b/package.json index efe961264..f5f4258ac 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "chokidar": "^1.6.0", "colors": "^1.1.2", "commander": "^2.8.1", + "css-loader": "^0.28.7", "ejs": "^2.5.7", "ethereumjs-testrpc": "3.9.2", "finalhandler": "^0.5.0", @@ -37,6 +38,7 @@ "serve-static": "^1.11.1", "shelljs": "^0.5.0", "solc": "0.4.17", + "style-loader": "^0.19.0", "tar": "^3.1.5", "toposort": "^1.0.0", "underscore": "^1.8.3", diff --git a/test_app/app/js/foo.css b/test_app/app/js/foo.css new file mode 100644 index 000000000..c4d554e05 --- /dev/null +++ b/test_app/app/js/foo.css @@ -0,0 +1,3 @@ +body { + background-color: black; +} diff --git a/test_app/app/js/foo.js b/test_app/app/js/foo.js new file mode 100644 index 000000000..0ab4b0c58 --- /dev/null +++ b/test_app/app/js/foo.js @@ -0,0 +1,4 @@ + +const FOO = "dude"; + +export default FOO; diff --git a/test_app/app/js/index.js b/test_app/app/js/index.js index cbf160911..caa255cba 100644 --- a/test_app/app/js/index.js +++ b/test_app/app/js/index.js @@ -1,5 +1,12 @@ /*globals $, SimpleStorage, document*/ +import Test2 from 'MyLib'; + +import test3 from './foo'; +import './foo.css'; + +window.test_3 = test3; + var addToLog = function(id, txt) { $(id + " .logs").append("
" + txt); }; @@ -8,6 +15,9 @@ var addToLog = function(id, txt) { // Blockchain example // =========================== $(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); diff --git a/test_app/embark.json b/test_app/embark.json index 2b275be5a..2f08464d4 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -1,9 +1,10 @@ { "contracts": ["app/contracts/**"], "app": { + "js/webpack_test.js": "app/js/index.js", "css/app.css": ["app/css/**"], "images/": ["app/images/**"], - "js/app.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**", "!app/js/test.js"], + "js/app.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**", "!app/js/test.js", "!app/js/foo.js", "!app/js/foo.css"], "js/embark.js": ["embark.js"], "js/abi.js": "abi.js", "js/test.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/async.min.js", "app/js/test.js"],