未捕获的TypeError:svmd_es.Button不是在svelte中实现UI工具包的构造函数

时间:2020-08-23 08:00:26

标签: javascript svelte rollupjs svelte-3 svelte-component

我是Svelte的新手,并且在我的第一个实验中试用了Material Design工具包。我发现https://github.com/hkh12/svmd看起来不错,然后将其添加到我的Svelte项目中。

根据文档,除了导入组件和使用它们外,没有其他内容。但是,这在浏览器中给了我以下错误:

Uncaught TypeError: svmd_es.Button is not a constructor
    create_fragment bundle.js:798
    init index.mjs:1451
    App bundle.js:873
    app main.js:4
    <anonymous> bundle.js:890

在Node svmd模块文件夹中,我发现这些组件本身就是.svelte文件;格式为.svelte,也可以是已编译的格式。我不知道为什么简单地导入它们并使用它们是行不通的。

我唯一想到的就是我的汇总文件中可能缺少什么?也许首先需要翻译一些东西,或者应该(不是)对node_modules文件夹中的.svelte文件进行预处理(或者不进行预处理)?

要尝试解决此问题,我已经尝试过:

  • 在汇总中的resolve配置中添加苗条
  • 将svmd节点文件夹的路径添加到汇总中的svelte include配置中

两个都不起作用。这是我现在使用的文件:

main.js

import App from './App.svelte';

const app = new App({
    target: document.body
});

export default app;

App.svelte

<script>
    import { Button, Slider, Fab } from 'svmd';
    import 'svmd/dist/svmd.css';
</script>

<main>
    <Button>flat button</Button>
    Lets see a button.
</main>

<style>
</style>

Rollup.config.js

import svelte from 'rollup-plugin-svelte';
import scss from 'rollup-plugin-scss';
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';

const production = !process.env.ROLLUP_WATCH;

function serve() {
    let server;
    
    function toExit() {
        if (server) server.kill(0);
    }

    return {
        writeBundle() {
            if (server) return;
            server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
                stdio: ['ignore', 'inherit', 'inherit'],
                shell: true
            });

            process.on('SIGTERM', toExit);
            process.on('exit', toExit);
        }
    };
}

export default {
    input: 'src/main.js',
    output: {
        sourcemap: true,
        format: 'iife',
        name: 'app',
        file: 'public/build/bundle.js'
    },
    plugins: [
        svelte({
            // enable run-time checks when not in production
            dev: !production,
            // we'll extract any component CSS out into
            // a separate file - better for performance
            css: css => {
                css.write('public/build/bundle.css');
            }
        }),

        // If you have external dependencies installed from
        // npm, you'll most likely need these plugins. In
        // some cases you'll need additional configuration -
        // consult the documentation for details:
        // https://github.com/rollup/plugins/tree/master/packages/commonjs
        scss(),
        resolve({
            browser: true,
            dedupe: ['svelte']
        }),
        commonjs(),

        // In dev mode, call `npm run start` once
        // the bundle has been generated
        !production && serve(),

        // Watch the `public` directory and refresh the
        // browser on changes when not in production
        !production && livereload('public'),

        // If we're building for production (npm run build
        // instead of npm run dev), minify
        production && terser()
    ],
    watch: {
        clearScreen: false
    }
};

package.json

{
  "name": "svelteapp",
  "version": "1.0.0",
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^14.0.0",
    "@rollup/plugin-node-resolve": "^8.4.0",
    "rollup": "^2.3.4",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-scss": "^2.6.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0",
    "svmd": "^0.3.1"
  },
  "dependencies": {
    "sirv-cli": "^1.0.0"
  }
}

希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

您提到的库似乎在捆绑发布时出现了一些错误。如果直接引用源组件本身,那么运气会更好

import { Button, Slider, Fab } from 'svmd/src';

也就是说,它似乎不再活跃了。

如果您要进行材质设计,则可能需要看看https://sveltematerialui.com/