我有一个简单的库,并且正在使用ES6,并且有一些require关键字,然后,我需要将其转换为浏览器可以理解的版本,我使用webpack来创建我的库的浏览器版本。
这是一个例子:
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(我使用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/'));
});
现在,当我运行咕嘟咕嘟任务(babel
和webpack
),我会得到一个文件,该文件是结果webpack
(和手段都需要进口,现在被转换)
这是webpack的结果(我是指转换结果):
/******/ (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;
});
};
/***/ }
/******/ ]);
第一个问题是,现在,我怎么能执行(和访问)callMe
,在定义的功能main.js
,现在通过捆绑的WebPack?
另一个问题是,代码看起来丑陋而不简单,有什么方法可以简化它吗?
而且我还认为,webpack
似乎只是为了将其转换为需要浏览器支持的版本,并且browserify
Gulp出现了问题。有什么建议吗?
我怎么能执行(和访问)
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!"
代码看起来丑陋而不简单,有什么方法可以简化它吗?
无需简化它。看起来丑陋的捆绑代码没有什么错,因为无论如何它不会被人类阅读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句