【问题标题】:How to make rollup expand `require` statements?如何使汇总扩展`require`语句?
【发布时间】:2018-10-09 10:18:23
【问题描述】:

我正试图绕过rollup

我正在使用一个库来生成具有这种格式的文件:IIFE 和 require 语句。例如

// index.js
(function() {
  const myThing = require('./thing');
})()

//thing.js
module.exports = { a: 3 };

我正在尝试将rollup 与一堆其他东西一起使用,但我的 bundle.js 最终看起来像这样:

(function () {
  var myThing = require('./thing');
})();

我需要怎么做才能让我的bundle.js 看起来像这样?

(function () {
  var myThing = { a: 3 };
})();

如果我的设置有问题,这里是我正在使用的rollup.config.js

var babel = require('rollup-plugin-babel');

export default {
  input: 'index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'es'
  },
  plugins: [
    babel({
      exclude: 'node_modules/**'
    })
  ]
};

这些是我安装的包:

"babel-core": "^6.26.3",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.6.1",
"rollup": "^0.58.2",
"rollup-plugin-babel": "^3.0.4"

还有我的 babel 配置:

{
  "presets": [
    [
      "env",
      {
        "modules": false
      }
    ]
  ],
  "plugins": [
    "external-helpers"
  ]
}

要构建,我只是打电话给rollup -c

【问题讨论】:

    标签: babeljs rollupjs


    【解决方案1】:

    好的,我想通了。我只需要使用 CommonJS 插件:

    import babel from 'rollup-plugin-babel';
    import resolve from 'rollup-plugin-node-resolve';
    import commonjs from 'rollup-plugin-commonjs';
    
    export default {
      input: 'index.js',
      output: {
        file: 'dist/bundle.js',
        format: 'cjs'
      },
      plugins: [
        resolve(),
        commonjs(),
        babel({
          exclude: 'node_modules/**'
        })
      ]
    };
    

    【讨论】:

    • 不适用于我的情况。代码就像export const pinyin = require('chinese-to-pinyin') 一样简单。最糟糕的是。这只是编译 - export const pinyin = require('invalid')
    猜你喜欢
    • 2023-01-28
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 2018-06-24
    • 1970-01-01
    • 2011-03-26
    相关资源
    最近更新 更多