【问题标题】:Rollup.js - Use rollup.config.js in JS API?Rollup.js - 在 JS API 中使用 rollup.config.js?
【发布时间】:2020-06-02 13:23:13
【问题描述】:

我有一个可用的 rollup.config.js 文件,但需要在 Rollup 完成后运行单独的打包脚本。我的计划是通过他们的 JS API 使用 Rollup 的观察者。但是,我根本无法让 JS API 工作。

我从汇总站点引用此代码...

const loadConfigFile = require('rollup/dist/loadConfigFile');
const path = require('path');
const rollup = require('rollup');

loadConfigFile(path.resolve(__dirname, 'rollup.config.js'))
  .then(async ({options, warnings}) => {
    warnings.flush();
    const bundle = await rollup.rollup(options);
    await Promise.all(options.output.map(bundle.write));
    rollup.watch(options);
  })

但我不断收到错误Unknown input options: 0.......Error: You must supply options.input to rollup

我的 rollup.config.js 如下...

import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from "rollup-plugin-terser";
import replace from '@rollup/plugin-replace';
import json from '@rollup/plugin-json';

const production = !process.env.ROLLUP_WATCH;

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/bundle.js'
    },
    plugins: [
        json(),
        production && replace({
            'eruda': ``,
            exclude: 'node_modules/**',
            delimiters: ['import * as eruda from \'', '\'']
        }),
        production && replace({
            'eruda': ``,
            exclude: 'node_modules/**',
            delimiters: ['', '.init()']
        }),
        svelte({
            dev: !production,
            css: css => {
                css.write('public/bundle.css');
            }
        }),
        resolve({ browser: true }),
        commonjs(),

        !production && livereload('public'),
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

感谢任何想法!

【问题讨论】:

    标签: javascript node.js bundler rollup rollupjs


    【解决方案1】:

    我认为 rollupjs.org 上的示例是错误的。不应该是这样吗?

    const loadConfigFile = require('rollup/dist/loadConfigFile')
    const path = require('path')
    const rollup = require('rollup')
    
    // load the config file next to the current script;
    // the provided config object has the same effect as passing "--format es"
    // on the command line and will override the format of all outputs
    loadConfigFile(path.resolve(__dirname, 'rollup.config.js'), {format: 'es'})
      .then(({options, warnings}) => {
        // "warnings" wraps the default `onwarn` handler passed by the CLI.
        // This prints all warnings up to this point:
        console.log(`We currently have ${warnings.count} warnings`)
    
        // This prints all deferred warnings
        warnings.flush()
        
        // options is an "inputOptions" object with an additional "output"
        // property that contains an array of "outputOptions".
        // The following will generate all outputs and write them to disk the same
        // way the CLI does it:
        options.map(async options => {
            const bundle = await rollup.rollup(options)
            await Promise.all(options.output.map(bundle.write))
         
            // You can also pass this directly to "rollup.watch"
            rollup.watch(options)
          })
      })

    【讨论】:

      【解决方案2】:

      想通了,显然从loadConfigFile 返回的options 是一个数组,所以我不得不在异步函数中执行options[0]

      【讨论】:

        猜你喜欢
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 2017-03-13
        • 2021-05-20
        • 1970-01-01
        相关资源
        最近更新 更多