如何直接在 script 标签中制作 webpack 捆绑 js,而不是通过 src

2024-03-05

My webpack.config.js现在的文件是:

const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const path = require('path')

module.exports = (env, argv) => ({
    mode: argv.mode === 'production' ? 'production' : 'development',

    // This is necessary because Figma's 'eval' works differently than normal eval
    devtool: argv.mode === 'production' ? false : 'inline-source-map',

    entry: {
        ui: './src/ui.js', // The entry point for your UI code
        code: './src/code.js', // The entry point for your plugin code
    },

    output: {
        clean: true,
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'), // Compile into a folder called "dist"
    },

    // Tells Webpack to generate "ui.html" and to inline "ui.ts" into it
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/ui.html',
            filename: 'ui.html',
            inlineSource: '.(js)$',
            chunks: ['ui'],
        })
    ],
})

它符合的文件之一是:

ui.html:(这是当前问题的目标文件)

<head>
    <script defer="defer" src="ui.js"></script>
</head>
<h2>Figma auto layout</h2><p>Count: <input id="count" value="5"></p>
<button id="create">Create</button>
<button id="cancel">Cancel</button><br>
<button id="create-structure">Create structure</button>

我希望 ui.html 捆绑生成的 html 文件,如下所示:

<h2>Figma auto layout</h2><p>Count: <input id="count" value="5"></p>
<button id="create">Create</button>
<button id="cancel">Cancel</button><br>
<button id="create-structure">Create structure</button>
++ <script>
++     // here is js code from ui.js
++ </script>

我怎样才能让 webpack 以这种方式编译?

EDIT 1

If I use

plugins: [
    new HtmlWebpackPlugin({
        template: './src/ui.html',
        filename: 'ui.html',
        inlineSource: '.(js)$',
        chunks: ['ui'],
    }),
    new HtmlWebpackInlineSourcePlugin()
]

它返回一个错误(我已经安装了这个插件)

[webpack-cli] TypeError: Cannot read property 'getHooks' of undefined

我知道它需要 webpack 5。我的 webpack 版本是``[电子邮件受保护] /cdn-cgi/l/email-protection`

html-webpack-inline-source-plugin 有替代品吗?


这是我遇到类似问题后发现的两个解决方案:

  1. 您可以使用InlineChunkHtmlPlugin from react-dev-utils,优点是它是即插即用的......在大多数情况下!就我而言,我注入的脚本包含一个</script>,这打破了页面。您可以通过修改来控制脚本的发出位置(头部、主体等)inject的财产HtmlWebpackPlugin

欲了解更多信息,请阅读此处 https://github.com/facebook/create-react-app/tree/main/packages/react-dev-utils#new-inlinechunkhtmlpluginhtmlwebpackplugin-htmlwebpackplugin-tests-regex

  1. 您可以使用 HTML 模板注入脚本,您可以将其提供给HtmlWebpackPlugin。例如,此模板将所有 JS 和 CSS 资源内联到网页中:
doctype html
html
  head
    meta(charset="utf-8")
    title #{htmlWebpackPlugin.options.title}
  body
    each cssFile in htmlWebpackPlugin.files.css
      style !{compilation.assets[cssFile.substr(htmlWebpackPlugin.files.publicPath.length)].source()}
    each jsFile in htmlWebpackPlugin.files.js
      script !{compilation.assets[jsFile.substr(htmlWebpackPlugin.files.publicPath.length)].source()}

欲了解更多信息,请阅读此处 https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何直接在 script 标签中制作 webpack 捆绑 js,而不是通过 src 的相关文章

随机推荐