简体   繁体   English

当模块已被 Z424516CA53B4AD4BEFZ37ED0 捆绑时,如何从 HTML 的模块中调用 javascript function?

[英]How can I call a javascript function in a module from HTML when the module has been bundled by webpack?

This is probably a basic question but for some reason Google won't give me the answer.这可能是一个基本问题,但由于某种原因谷歌不会给我答案。

I have a file called dropzone.stub.js in my src/ folder我的 src/ 文件夹中有一个名为 dropzone.stub.js 的文件

import { Dropzone } from "dropzone";
Dropzone.autoDiscover = true;

// Callback takes one argument and that is the file that was 
// added.
export default function createDropZone(selector, url, callback) {
  const myDropzone = new Dropzone(selector, { url: url });
  myDropzone.on("addedfile", callback);
}

Basically I am just trying to call it from with script tags within my HTML file.基本上我只是想用我的 HTML 文件中的脚本标签来调用它。

My webpack bundler is configured as follows:我的 webpack 捆绑器配置如下:

const path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/dropzone.stub.js',
  output: {
    filename: 'dropzone.bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
      //      presets: ['@babel/preset-env', {targets: 'defaults'}]
          }
        }
      },
//      {
//        test: /\.css$/i,
//        use: ['style-loader', 'css-loader'],
//      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
    ],
    // resolve: {
    //  extensions: [ '.js', '.jsx', '.css' ],
    //}
  },
};

The script section in my HTML browser at the moment just looks like this:目前我的 HTML 浏览器中的脚本部分如下所示:

<script type="module">
  import { createDropZone } from '/static/dist/dropzone.bundle.js';

</script>

The browser reports the following error:浏览器报以下错误:

Uncaught SyntaxError: The requested module '/static/dist/dropzone.bundle.js' does not provide an export named 'createDropZone'

Any ideas anyone?有什么想法吗?

You probably want你可能想要

import createDropZone from '/static/dist/dropzone.bundle.js';

since you are exporting default因为您正在导出default

What does the bundled file look like.捆绑的文件是什么样的。 Webpack is most likely not bundling it as a es module. Webpack 很可能不会将其捆绑为 es 模块。

暂无
暂无

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

相关问题 如何从 html onclick 中的 javascript 模块调用函数 - How can I call a function from javascript module in html onclick 是否应该从 html 中删除“type=&quot;module&quot;”声明<script> tag after the associated Firebase source has been bundled? - Should a 'type="module"' declaration be removed from an html <script> tag after the associated Firebase source has been bundled? 如何确定节点模块是否可以使用Webpack捆绑并在浏览器中运行? - How can I determine if a node module can be bundled using Webpack and run in a browser? 从 HTML 中的 JavaScript 模块调用 function - Call function from JavaScript module in HTML 从HTML文件中访问捆绑的javascript(使用webpack)中的函数 - Accessing function in bundled javascript (using webpack) from HTML file 如何从 html 页面调用在 javascript 模块(type=module)中声明的函数 - How to call a function declared in a javascript module (type=module) from an html page 在使用javascript模块模式时,如何从私有方法中调用公共方法? - How can i call a public method from within a private one when using the javascript Module Pattern? Webpack编译后如何在模块中调用函数 - Webpack how to call function in module after compiled 通过Webpack捆绑的Expose模块 - Expose module bundled through Webpack 如何将自定义内容转换为 webpack 中的 javascript 模块? - How can I transform custom content into a javascript module in webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM