从输出文件中移除Webpack引导程序

那么,我知道Webpack允许我们导入带有require包,这就是Webpack的基础结构。

但是,如果不在入口文件中使用require ,那不是没用吗?

我有这个test.js条目:

 console.log('Test'); 

和输出

 /******/ (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] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = 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; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */, /* 1 */ /***/ (function(module, exports, __webpack_require__) { __webpack_require__(2); /***/ }), /* 2 */ /***/ (function(module, exports) { console.log('Test'); /***/ }) /******/ ]); 

这是无用的代码,也阻止我使用全局variables!

至less对我来说,这是! 这就是为什么我想知道是否有任何插件或解决方法将其删除?

经过一番调查,我找不到一个合适的方法来做到这一点。

但是调查一个替代scheme,我可以findrollupjs ,一个优化的捆绑器,像Webpack一样工作,但是我们可以用更less的代码实现我们的目标

 // rollup.config.js export default { input: 'src/main.js', output: { file: 'bundle.js', format: 'cjs' } }; 

它也可以编译成不同的格式。

生成的包的格式。 以下之一:

  • amd – asynchronous模块定义,与像RequireJS这样的模块加载器一起使用
  • CommonJS,适用于Node和Browserify / Webpack
  • es – 将捆绑包保存为ES模块文件
  • iife – 自我执行的function,适合作为标签包含在内。 (如果你想为你的应用程序创build一个包,你可能想要使用它,因为它会导致更小的文件大小。)umd – 通用模块定义,作为amd,cjs和iife在一起

有关更多信息,请访

解决我的问题

使用格式iife ,它封装了我的模块的范围,所以编译的test.js将导致:

 (function () { 'use strict'; console.log('Test'); }()); 

根据输出格式,编译ES modules更合理。