Node.js-没有webpack捆绑

时间:2019-01-07 20:11:00

标签: javascript node.js webpack build

我想将一些文件“捆绑”到一个单个js 文件中,但是我不想该Webpack包装器。


假设我有3个文件:

// file1.js
export default { hello: "hello" };

// file2.js
export default { world: "world" };

// index.js
import file1 from "./file1";
import file2 from "./file2";

(() => ({ ...file1, ...file2 }))()

我想要以下结果:

// build/output.js (+ babel...)
(function(){
  return Object.assign({}, { hello: "hello" }, { world: "world" });
})()

除了上述构建输出外,没有一行代码。


使用webpack可以吗?谢谢!

1 个答案:

答案 0 :(得分:0)

O.P。解决方案

好的,所以我找到了解决方案! 它可能不是最好的,但它可以工作。

我发现this library here将文件串联在一起。


建筑物

我可以使用npm run build进行构建。

连接文件的代码是:

// ======================================================
// Tools / Bundle
// ======================================================

// Libs
var path = require("path");
var bundle = require("bundle-js");

module.exports.exec = function() {
  // Disable logging (hack for 'bundle-js' library).
  var _log = console.log;
  console.log = function() {};

  // Concatenate each file (required by the application).
  var file = path.resolve(__dirname, "../src/index.js");
  var bundledCode = bundle({
    entry: file,
    print: false,
    disablebeautify: true
  });

  // Enable logging.
  console.log = _log;

  // Return bundled code.
  return bundledCode;
};

由于某些原因,即使使用选项{ print: false } bundle-js 也会始终输出某些内容。因此,我添加了一个小技巧来解决此问题。

相关问题