简体   繁体   English

Webpack + babel,ReferenceError: class 未定义

[英]Webpack + babel, ReferenceError: class is not defined

I am writing my first JS library and wanted to learn to use babel and webpack.我正在编写我的第一个 JS 库,并想学习使用 babel 和 webpack。

The problem I am having is that the class (entry point?) that I'd like to instantiate in my index.htm file causes the browser to complain that it's "not defined".我遇到的问题是我想在我的index.htm文件中实例化的 class (入口点?)导致浏览器抱怨它“未定义”。

This is my webpack config:这是我的 webpack 配置:

const path = require('path');

module.exports = {
    entry: path.resolve(__dirname, './src/js/FormrEditor.js'),
    output: {
        filename: 'formr-editor.js',
        path: path.resolve(__dirname, 'dist/js'),
    },
    devtool: "source-map",
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: '/node_modules/',
                query: {
                    presets: ['@babel/env']
                }
            }
        ]
    },
};

And the js:和js:

import {Toolbox, ToolboxItem} from "./Toolbox";

export default class FormrEditor{
    constructor(element, config){

        /** Find the element to use as the container */
        if(element instanceof Element)
            this.container = element;
        else
            this.container = document.getElementById(element);

        this.container.classList.add("feditor")

        this.buildEditorDom();
    }

    buildEditorDom()
    {
        let form = document.createElement("div");
        form.classList.add("feditor-form");

        let toolbox = document.createElement("div");
        toolbox.classList.add("feditor-toolbox");

        let handle = document.createElement("div");
        handle.classList.add("feditor-toolbox-handle");

        let testItem = document.createElement("div");
        testItem.classList.add("feditor-toolbox-item");

        toolbox.appendChild(handle);
        toolbox.appendChild(testItem);

        this.container.appendChild(form);
        this.container.appendChild(toolbox);

        this.toolbox = new Toolbox(toolbox);
        this.form = form;
    }
}

So in the index.htm file I am doing:所以在index.htm文件中我正在做:

<script src="formr-editor.js"></script>
<script>
    import FormrEditor from "./formr-editor";

    var formr = FormrEditor(document.getElementById('editor'), {});
</script>

And that's when it complains that FormrEditor isn't defined...那就是它抱怨FormrEditor没有定义的时候......

A bit more info:更多信息:

I am serving index.htm out of a "demo" folder, in which I have symlinks to both formr-editor.js (the output of webpack) and the css.我从“演示”文件夹中提供 index.htm,其中我有两个formr-editor.js (webpack 的 output)和 css 的符号链接。

I have also tried var formr = new FormrEditor(document.getElementById('editor'), {});我也试过var formr = new FormrEditor(document.getElementById('editor'), {}); without the import as per MarcRo and I still get the same error.如果没有按照 MarcRo 进行import ,我仍然会遇到同样的错误。

I have tried both export and export default on the class and nothing...我已经在 class 上尝试了exportexport default ,但什么也没...

There are a few things you might want to check.您可能需要检查几件事。

  1. You cannot use ES6 imports in the browser like this - actually the import statement is entirely unnecessary.你不能像这样在浏览器中使用 ES6 导入——实际上导入语句是完全没有必要的。

  2. Your <script src="formr-editor"> seems like it has the wrong path.您的<script src="formr-editor">似乎路径错误。 Your webpacks output is generating a file at dist/js/formr-editor.js.你的 webpacks output 在 dist/js/formr-editor.js 生成一个文件。 if you want to import the untranspiled src file in your Browser use <script src="src..." type="module"> - make sure the untranspiled file is also accessible.如果您想在浏览器中导入未转译的 src 文件,请使用<script src="src..." type="module"> - 确保未转译的文件也可访问。

  3. How are you serving public assets?您如何为公共资产服务? Is your formr-editor.js file (generated by webpacks) accessible?您的 formr-editor.js 文件(由 webpacks 生成)是否可访问?

  4. You want to call your FormrEditor class with the new keyword.您想使用new关键字调用 FormrEditor class。

Note: you can check in your Browsers dev-tools whether your Browser manages to load the respective files!注意:您可以在您的浏览器开发工具中检查您的浏览器是否能够加载相应的文件!

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

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