Javascript 从输出文件中删除Webpack引导

Javascript 从输出文件中删除Webpack引导,javascript,node.js,webpack,Javascript,Node.js,Webpack,嗯,我知道Webpack允许我们导入带有require的包,这就是Webpack的基础结构 但是,当您在条目文件中不使用require时,它不是没有用吗 我有一个test.js条目: console.log('Test'); 以及输出 /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /

嗯,我知道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.config.js
export default {
  input: 'src/main.js',
  output: {
    file: 'bundle.js',
    format: 'cjs'
  }
};
它也可以被编译成不同的格式

生成的捆绑包的格式。以下其中一项:

  • amd
    –异步模块定义,与模块加载程序(如RequireJS)一起使用
  • cjs
    –CommonJS,适用于节点和浏览/网页包
  • es
    –将捆绑包作为es模块文件保存
  • iLife
    –一种自动执行功能,适合作为标签包含。(如果要为应用程序创建捆绑包, 您可能想使用它,因为它会导致更小的文件 尺寸。)umd–通用模块定义,可作为amd、cjs和iife使用 一应俱全
欲了解更多信息,请访问

我的问题的解决办法 使用格式
iife
,它封装了我的模块的范围,因此编译的
test.js
将导致:

(function () {
'use strict';

console.log('Test');

}());

这是编译
ES模块的更合理的方法,具体取决于输出格式。

如果需要使用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;

的副本仍然想知道Webpack是否能够在没有模块加载器引导的情况下输出捆绑包,这可以通过Rollup实现。@Robula关于这一点有一个公开的问题,请看一看,我不明白您为什么要使用Webpack或希望使用全局变量。似乎您的项目不需要WebPack或Rollup,所以您应该完全删除它们。@sctskw显然
console.log('Test')不是我的项目:)我需要一个用于es6rollup的transpiler与webpackThank相比还有很多其他缺点谢谢你@Jose Paredes我也在做同样的事情。我将看一看rollupjs。你自己找到了答案并分享了。美好的在这里回答/评论的人还有一件事。我认为人们给出的评论和回答只是质疑和反驳,这是令人悲哀的。要么你试着帮忙并给出一个严肃的回答,要么你离开。这就是为什么我经常决定不在stackoverflow提出问题的原因。因为我知道我可能会比现在更沮丧。因为在我看来,人们会提出“愚蠢”的评论或相反的问题。