简体   繁体   English

HTML 内联 javascript 与 webpack

[英]HTML inline javascript with webpack

I am currently starting to use webpack for my website and I have a question.我目前开始在我的网站上使用 webpack,我有一个问题。

I currently have a html page that includes my javascript bundle and has some onclick functions in buttons embedded in the html.我目前有一个 html 页面,其中包括我的 javascript 捆绑包,并且在 ZFC35FDC70D5FC69D253ECZ8 中嵌入的按钮中具有一些onclick功能。 I would like to keep the functionality of these buttons and the onclick , but currently I have not found a way to do this.我想保留这些按钮和onclick的功能,但目前我还没有找到这样做的方法。 Obviously, if I try to minify the javascript functions, the name of the function would change and it won't work.显然,如果我尝试缩小 javascript 函数,则 function 的名称会发生变化,并且无法正常工作。

Here's an example, where the bundle produced by webpack is included in the html file:这是一个示例,其中 webpack 生成的包包含在 html 文件中:

<button onclick="foo("bar")">test</button> and currently foo is undefined. <button onclick="foo("bar")">test</button>并且当前foo未定义。

I have tried using html webpack plugin without success.我试过使用 html webpack 插件没有成功。

https://jsfiddle.net/danwillm/bkgtnm6s/ https://jsfiddle.net/danwillm/bkgtnm6s/

Is there any way to do this?有没有办法做到这一点?

Yes you can get to the function but you will still have to modify the code slightly - onClick.是的,您可以访问 function,但您仍然需要稍微修改代码 - onClick。

webpack webpack

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const {
  CleanWebpackPlugin
} = require('clean-webpack-plugin');

module.exports = (env, argv) => {
  return {
    devtool: argv.mode === 'production' ? 'none' : 'source-map',
    mode: argv.mode === 'production' ? 'production' : 'development',
    entry: {
      MYSCRIPT: './sources/script.js'
    },
    output: {
      path: path.resolve(__dirname, './dist'),
      filename: './[name].js',
      library: '[name]',
      libraryTarget: 'var',
    },
    module: {
      rules: [{
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }, ]
    },
    plugins: [
      new CleanWebpackPlugin({
        verbose: true
      }),
      new HtmlWebPackPlugin({
        filename: 'index.html',
        template: './sources/index.html'
      })
    ]
  }
}

The most important part is the name entry and outputs.最重要的部分是名称输入和输出。 You must also export each function.您还必须导出每个 function。

JS JS

export function test(type) {
  alert(type);
}

And we can get to the function this way.我们可以通过这种方式到达 function。

HTML HTML

<a onClick="MYSCRIPT.test('bar')">click</a>

You can find the whole devising example here .您可以在 此处找到整个设计示例。

Your script works in a SO snippet, as you can see below您的脚本在 SO 片段中工作,如下所示

 function foo(test){ console.log(test); }
 <a onClick="foo('bar');" class="blue left desktop-only pointer">click here</a>

It did not work in jsfiddle probably because of scope issues: The script is probably executed in its own scope and a function definition there was not reachable by the onclick in the HTML. It did not work in jsfiddle probably because of scope issues: The script is probably executed in its own scope and a function definition there was not reachable by the onclick in the HTML. I modified your function definition so it would always land in the global scope我修改了您的 function 定义,因此它将始终落在全局 scope

foo=function(test){console.log(test);}

and now it works there too, see here: https://jsfiddle.net/obuvjn0m/现在它也可以在那里工作,请参见此处: https://jsfiddle.net/obuvjn0m/

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

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