• 欢迎光临~

rollup babel

开发技术 开发技术 2022-08-05 次浏览

https://babeljs.io/setup#installation
https://www.rollupjs.com/guide/command-line-reference
yarn init -y
https://babel.dev/docs/en/babel-preset-env

// a.js
var promise = new Promise((resolve, reject) => {
    console.log('执行promise');
    resolve('promise');
})
// rollup.config.js
import babel from '@rollup/plugin-babel';

const config = {
  input: 'a.js',
  output: {
    dir: 'output',
    format: 'esm'
  },
  plugins: [babel({babelHelpers: 'bundled'})]
};

export default config;

// babel.config.json
{
  "presets": [
    [
      "@babel/preset-env",
      {
        "useBuiltIns": "usage",
        "corejs": "2"
      }
    ]
  ]
}
// output/a.js
import 'core-js/modules/es6.object.to-string.js';
import 'core-js/modules/es6.promise.js';

new Promise(function (resolve, reject) {
  console.log('执行promise');
  resolve('promise');
});

2、rollup.config.js
修改format

// -f, --format <format>       输出的文件类型 (amd, cjs, esm, iife, umd)
format: 'iife'

此时,output/a.js

(function () {
    'use strict';

    new Promise(function (resolve, reject) {
      console.log('执行promise');
      resolve('promise');
    });

})();

程序员灯塔
转载请注明原文链接:rollup babel
喜欢 (0)