1

i wrote 3 modules ( Model, View, Controller ) with pure JS syntax. I mean like that:

var Model = (function () {
  //...
})();

var View = (function () {
  //...
})();

var Controller = (function (model, view) {
  //do the job...
})(Model, View);

how can i simulate imports/exports using JS (ES5.1)? I just want to divide code into separated files using only JS.

I tried to transpile ES6 imports via babel and it created someting with like:

  var model = require('model'); 

but when i add that to my code i got error that "require is not allowed"

//* I dont want to use babel in projects

Dizip
  • 21
  • 5
  • what do you want to have in the end? Do you want to have a builder that merges all your files in one on production? Do you want to load the files on request? Modules themselves are really easy, the question is how will you manage the files: in the runtime or precompiled as one bundle – smnbbrv Oct 20 '17 at 19:28
  • I am using webpack to bundle everything at the end, but i want to keep this structure which i described. Just want to divide that into 3 files. – Dizip Oct 20 '17 at 19:34
  • 1
    aha, so if you use webpack `require` should already work for you. Just keep in mind https://webpack.js.org/concepts/module-resolution/ : most likely you want to use a relative path for your module, but currently you are using the path that forces webpack to look into 'node_modules/module'. Use `require('./module')` or similar – smnbbrv Oct 20 '17 at 19:40
  • 2
    It;s working, thanks! – Dizip Oct 20 '17 at 19:59
  • you are welcome :) – smnbbrv Oct 20 '17 at 20:31

0 Answers0