简体   繁体   English

rollupjs 和工兵 - 未定义要求

[英]rollupjs and sapper - require is not defined

i am using sapper und svelte with rollupjs .我使用sapper UND svelterollupjs But when i try to use the repo scrollscene from Github, i get an error on console when run npm run dev : ReferenceError: require is not defined但是当我尝试使用 Github 中的 repo scrollscene时,我在运行npm run dev时在控制台上收到一个错误: ReferenceError: require is not defined

Here is my rollup.config.js这是我的rollup.config.js

import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
import svelte from 'rollup-plugin-svelte';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import config from 'sapper/config/rollup.js';
import pkg from './package.json';

const mode = process.env.NODE_ENV;
const dev = mode === 'development';
const legacy = !!process.env.SAPPER_LEGACY_BUILD;

const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning);

export default {
    client: {
        input: config.client.input(),
        output: config.client.output(),
        plugins: [
            replace({
                'process.browser': true,
                'process.env.NODE_ENV': JSON.stringify(mode)
            }),
            svelte({
                dev,
                hydratable: true,
                emitCss: true
            }),
            resolve({
                browser: true,
                preferBuiltins: true,
                dedupe: ['svelte']
            }),
            commonjs(),
            globals(),
            builtins(),
            legacy && babel({
                extensions: ['.js', '.mjs', '.html', '.svelte'],
                runtimeHelpers: true,
                exclude: ['node_modules/@babel/**'],
                presets: [
                    ['@babel/preset-env', {
                        targets: '> 0.25%, not dead'
                    }]
                ],
                plugins: [
                    '@babel/plugin-syntax-dynamic-import',
                    ['@babel/plugin-transform-runtime', {
                        useESModules: true
                    }]
                ]
            }),

            !dev && terser({
                module: true
            })
        ],

        onwarn,
    },

    server: {
        input: config.server.input(),
        output: config.server.output(),
        plugins: [
            replace({
                'process.browser': false,
                'process.env.NODE_ENV': JSON.stringify(mode)
            }),
            svelte({
                generate: 'ssr',
                dev
            }),
            resolve({
                dedupe: ['svelte']
            }),
            commonjs()
        ],
        external: Object.keys(pkg.dependencies).concat(
            require('module').builtinModules || Object.keys(process.binding('natives'))
        ),

        onwarn,
    },

    serviceworker: {
        input: config.serviceworker.input(),
        output: config.serviceworker.output(),
        plugins: [
            resolve(),
            replace({
                'process.browser': true,
                'process.env.NODE_ENV': JSON.stringify(mode)
            }),
            commonjs(),
            !dev && terser()
        ],

        onwarn,
    }
};

I tried to add the packages: rollup-plugin-node-builtins and rollup-plugin-node-globals , but nothing changed.我尝试添加包: rollup-plugin-node-builtinsrollup-plugin-node-globals ,但没有任何改变。 The developer of scrollscene send me a link to a similiar issue , but i couldn't solve the problem with this. scrollscene的开发人员给我发送了一个类似问题的链接,但我无法解决这个问题。

What's going wrong here and how can i solve this, so i can use this library?这里出了什么问题,我该如何解决这个问题,以便我可以使用这个库? Thanks for your answer and for your help.感谢您的回答和帮助。

Niklas,尼克拉斯,

I had the same problem, and what I had to do was do the require inside of a .js file instead of in the .svelte file.我有同样的问题,而我所要做的就是做require内的.js文件,而不是在.svelte文件。

EDIT: This is the reference I had .编辑: 这是我的参考资料 The posts in the blog example of the sapper-template project on GitHub GitHub 上sapper-template项目的博客示例中的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM