25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Modules<br />

A solution to the dependency management problem is to use a module system,<br />

such as CommonJS or native ES modules. These systems allow JavaScript code<br />

to be modularized <strong>and</strong> imported into other files.<br />

Here is a CommonJS example: // moduleA.<strong>js</strong> module.exports = function(value) {<br />

return value * 2; }<br />

// moduleB.<strong>js</strong> var multiplyByTwo = require('./moduleA');<br />

console.log(multiplyByTwo(2));<br />

// Output: 4<br />

And here is a Native ES modules example: // moduleA.<strong>js</strong> export default<br />

function(value) { return value * 2; }<br />

// moduleB.<strong>js</strong> import multiplyByTwo from './moduleA';<br />

console.log(multiplyByTwo(2));<br />

// Output: 4<br />

The problem is that CommonJS cannot be used in a browser (it was designed for<br />

server-side JavaScript) <strong>and</strong> native ES modules are only now getting browser<br />

support. If we want to use a module system in a project, we'll need a build tool:<br />

Webpack.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!