expose-webpack-module

expose webpack production code module

Usage no npm install needed!

<script type="module">
  import exposeWebpackModule from 'https://cdn.skypack.dev/expose-webpack-module';
</script>

README

expose-webpack-module

An expose function implemented by AST (Syntax Abstract Tree) that modifies the webpack production code to return its running results.

image

install

npm i expose-webpack-module --save

example

./test/index.js

import expose from "expose-webpack-module"
const source = "some code like ./test/source.js"
const target = expose(source)

with inject-content-webpack-plugin

plugins: [
    new InjectPlugin({
        inject: expose
    })
]

Last but not least !!!

After reading the html-webpack-plugin source code, I learned to use LibraryTemplatePlugin to solve this problem

// webpack.config.js
plugins: [
    new LibraryTemplatePlugin('HTML_WEBPACK_PLUGIN_RESULT', 'var'),
    new InjectPlugin({
        inject: source => source.replace('var HTML_WEBPACK_PLUGIN_RESULT =', '')
    })
]