如何将js的内容进入到html页面中
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%= htmlWebpackPlugin.options.title %></title>
<script type="text/javascript" >
<%=
compilation.assets[htmlWebpackPlugin.files.chunks.main.entry.substr(htmlWebpackPlugin.files.publicPath.length)].source() %>
</script>
</head>
<body>
</body>
</html>
配置文件如下:
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: {
main:'./src/script/main.js',
a:'./src/script/a.js',
},
output:{
path:path.resolve(__dirname,'dist/js'),
filename:'[name]-[hash].js'
},
plugins:[
new htmlWebpackPlugin({
filename: 'index-[hash].html',
template: 'index.html',
inject: 'body',
title: "test config file title "
})
]
}
打包生成的html如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test config file title </title>
<script type="text/javascript" >
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports) {
function hewwwwww(){
var hee = "dasdasdasd";
alert(hee);
}
/***/ }
/******/ ]);
</script>
</head>
<body>
<script type="text/javascript" src="main-4e082b2fc9be4cc4a21f.js"></script><script type="text/javascript" src="a-4e082b2fc9be4cc4a21f.js"></script></body>
</html>
网友评论