首先我們需要安裝一個webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創建html入口文件。執行如下命令
npm install html-webpack-plugin --save-dev
在項目app目錄下建立component.js文件,寫入如下代碼
export default (text='hello world')=>{ const element=document.createElement('div'); element.innerHTML=text; return element;}在根目錄下創建webpack.config.js文件
const path=require('path');const HtmlWebpackPlugin=require('html-webpack-plugin');const PATHS={ app:path.join(__dirname,'app'), build:path.join(__dirname,'build'),};module.exports = { entry: { app:PATHS.app, }, output: { path:PATHS.build, filename: "[name].js" }, plugins: [ new HtmlWebpackPlugin({ title: 'webpack demo', }) ]};打開命令行,切換到項目目錄下,執行webpack命令。

這就代表著打包成功,看下我們多出的index.html文件。

首先我們需要安裝一個webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創建html入口文件。執行如下命令
npm install html-webpack-plugin --save-dev
在項目app目錄下建立component.js文件,寫入如下代碼
export default (text='hello world')=>{ const element=document.createElement('div'); element.innerHTML=text; return element;}在根目錄下創建webpack.config.js文件
const path=require('path');const HtmlWebpackPlugin=require('html-webpack-plugin');const PATHS={ app:path.join(__dirname,'app'), build:path.join(__dirname,'build'),};module.exports = { entry: { app:PATHS.app, }, output: { path:PATHS.build, filename: "[name].js" }, plugins: [ new HtmlWebpackPlugin({ title: 'webpack demo', }) ]};打開命令行,切換到項目目錄下,執行webpack命令。

這就代表著打包成功,看下我們多出的index.html文件。

看下我們的build/app.js

可以看到我們的index.js代碼和component.js經過了webpack特殊的處理。
用瀏覽器打開index.html可以看到如下效果

即為成功。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答