【问题标题】:Remove Webpack bootstrap from output file从输出文件中删除 Webpack 引导程序
【发布时间】:2018-01-12 02:32:38
【问题描述】:

嗯,我知道 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');

/***/ })
/******/ ]);

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

至少对我来说是这样!这就是为什么我想知道是否有任何插件或解决方法可以删除它?

【问题讨论】:

  • 仍然想知道 Webpack 是否能够在没有模块加载器引导的情况下输出包,正如 Rollup 可以实现的那样。
  • @Robula 有一个未解决的问题,请查看github.com/webpack/webpack/issues/2638
  • 我完全不明白您为什么要使用 WebPack 或希望使用全局变量。您的项目似乎不需要 WebPack 或 Rollup,因此您应该完全删除它们。
  • @sctskw 显然console.log('Test'); 不是我的项目 :) 我需要一个 es6 的转译器

标签: javascript node.js webpack


【解决方案1】:

如果您需要使用 rollup.js 捆绑/压缩遗留代码并且不想要 iife,我可以使用 --no-treeshake 命令行标志

rollup -c --no-treeshake`

连同谷歌闭包编译器插件来完成这个

import closure from 'rollup-plugin-closure-compiler-js';
import glob from 'glob';

const inputPath = './Static/JavaScript/';
let configArr = [];

for (let val of glob.sync(inputPath + '*.unpack.js')) {
  let obj = {};
  const filenameRegex = val.match(/([\w\d_-]*)\.?[^\\\/]*$/i);

  obj['input'] = filenameRegex['input'];
  obj['output'] = {
    file: inputPath + filenameRegex[1] + '.js',
    format: 'cjs'
  };
  obj['plugins'] = [closure({
    compilationLevel: 'WHITESPACE_ONLY',
    processCommonJsModules: true,
    languageIn: 'ES5',
    languageOut: 'ES5'
  })]

  configArr.push(obj);
}

export default configArr;

【讨论】:

    【解决方案2】:

    经过一番研究,我找不到合适的方法。

    但在寻找替代方案时,我可以找到 rollupjs,这是一个优化的捆绑器,可以像 Webpack 一样工作,但我们可以用更少的代码实现我们的目标

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

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

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

    • amd – 异步模块定义,与 RequireJS 等模块加载器一起使用
    • cjs – CommonJS,适用于 Node 和 Browserify/Webpack
    • es – 将包保存为 ES 模块文件
    • iife - 自执行函数,适合作为标签包含。 (如果您想为您的应用程序创建一个捆绑包, 您可能想使用它,因为它会导致文件更小 大小。) umd – 通用模块定义,用作 amd、cjs 和 iife 一站式服务

    欲了解更多信息,请访问their documentation

    我的问题的解决方案

    使用iife的格式,它封装了我的模块的作用域,所以编译后的test.js会产生:

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

    根据输出格式,编译ES modules哪种方法更合理。

    【讨论】:

    • rollup 与 webpack 相比还有很多其他的缺点
    • 谢谢@Jose Paredes,我也是为了同样的事情。我将看看 rollupjs。您自己找到了答案并分享了它。好的。人们在这里给出答案/cmets的另一件事。我认为人们给出的只是意见和反驳的问题和答案是可悲的。要么您尝试提供帮助并给出认真的评论答案,要么您离开它。这就是为什么我经常不打算在 stackoverflow 上提出问题的原因。因为我知道我可能会比现在更加沮丧。因为在我看来,人们会带着“愚蠢的”问题或相反的问题来。
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2022-07-21
    • 2020-05-18
    • 2018-10-12
    • 2019-12-16
    • 2023-01-25
    • 1970-01-01
    相关资源
    最近更新 更多