首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rollup.js -在JS应用编程接口中使用rollup.config.js?

Rollup.js -在JS应用编程接口中使用rollup.config.js?
EN

Stack Overflow用户
提问于 2020-06-02 21:23:13
回答 2查看 2.2K关注 0票数 0

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

我引用了来自Rollup站点的代码...

代码语言:javascript
复制
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如下...

代码语言:javascript
复制
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
    }
};

任何想法都很感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-07 14:36:49

我认为rollupjs.org的例子是错误的。难道不应该是这样吗?

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-06-03 03:30:37

弄清楚了,显然从loadConfigFile返回的options是一个数组,所以我必须在异步函数中执行options[0]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62153013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档