How to run functions that are converted via webpack?

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) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};

/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {

/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId])
/******/ 			return installedModules[moduleId].exports;

/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};

/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ 		// Flag the module as loaded
/******/ 		module.loaded = true;

/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}


/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;

/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;

/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";

/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 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!");
	}

/***/ },
/* 1 */
/***/ 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 result

First question: how can I execute (and access) the function callMedefined in main.jsand now bundled with webpack?

, , ?

, webpack - , , , browserify Gulp. ?

+4
2

( ) callMe, main.js?

, . - :

import Test from './test';

export default function callMe(){
  console.log("I am damn called!");
}

:

import callMe from 'dist/main.js';
callMe(); // logs "I am damn called!"

, ?

. , , .

+2

webpack , , , .

, callMe() () , webpack-bundeled- script. , Angular2 , .


, main.js :

export function callMe(){
  console.log("I am damn called!");
  // And I do more things ;)
}

-docs webpack.js :

module.exports = {
  entry {
    app: "dist/babel/main.js"
  }, 
  output {
    // My funky output configs...

    libraryTarget: "this"

  }
}

. this , <script> html.

OP, Gulp -webpack. webpack() Gulp -webpack docs.

+1

Source: https://habr.com/ru/post/1661465/


All Articles