I have a simple library, and I work with ES6, and I have the require keyword, then I need to convert this to a version that browsers understand, I use webpack to make my browser version of the library.
Here is an example:
main.js
import Test from './test';
function callMe(){
console.log("I am damn called!");
}
test.js
export default function(string) {
console.log("This is awesome!");
[1,2,3].map(n => n + 1);
}
gulpfile.js (I use Gulp)
var gulp = require('gulp');
var babel = require('gulp-babel');
gulp.task('babel', () => {
return gulp.src('src/*.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(gulp.dest('dist/babel'));
});
var webpack = require('gulp-webpack');
gulp.task('webpack', function() {
return gulp.src('dist/babel/main.js')
.pipe(webpack())
.pipe(gulp.dest('dist/'));
});
Now, when I run the Gulp tasks ( babeland webpack), I get a file that is the result webpack(and means that all requests and imports are now converted)
Here is the result of webpack (I mean the result of the conversion):
(function(modules) {
var installedModules = {};
function __webpack_require__(moduleId) {
if(installedModules[moduleId])
return installedModules[moduleId].exports;
var module = installedModules[moduleId] = {
exports: {},
id: moduleId,
loaded: false
};
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
module.loaded = true;
return module.exports;
}
__webpack_require__.m = modules;
__webpack_require__.c = installedModules;
__webpack_require__.p = "";
return __webpack_require__(0);
})
([
function(module, exports, __webpack_require__) {
"use strict";
var _test = __webpack_require__(1);
var _test2 = _interopRequireDefault(_test);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function callMe() {
console.log("I am damn called!");
}
},
function(module, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (string) {
console.log("This is awesome!");
[1, 2, 3].map(function (n) {
return n + 1;
});
};
}
]);
Run codeHide resultFirst question: how can I execute (and access) the function callMedefined in main.jsand now bundled with webpack?
, , ?
, webpack - , , , browserify Gulp. ?