webpack教程之webpack.config.js配置文件
首先我們需要安裝一個(gè)webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創(chuàng)建html入口文件。執(zhí)行如下命令
npm install html-webpack-plugin --save-dev
在項(xiàng)目app目錄下建立component.js文件,寫入如下代碼
export default (text='hello world')=>{
const element=document.createElement('div');
element.innerHTML=text;
return element;
}
在根目錄下創(chuàng)建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',
})
]
};
打開命令行,切換到項(xiàng)目目錄下,執(zhí)行webpack命令。

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

首先我們需要安裝一個(gè)webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創(chuàng)建html入口文件。執(zhí)行如下命令
npm install html-webpack-plugin --save-dev
在項(xiàng)目app目錄下建立component.js文件,寫入如下代碼
export default (text='hello world')=>{
const element=document.createElement('div');
element.innerHTML=text;
return element;
}
在根目錄下創(chuàng)建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',
})
]
};
打開命令行,切換到項(xiàng)目目錄下,執(zhí)行webpack命令。

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

看下我們的build/app.js

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

即為成功。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Javascript函數(shù)緩存的實(shí)現(xiàn)及應(yīng)用場景
Javascript函數(shù)緩存是一種提高網(wǎng)頁性能的重要技術(shù),通過將函數(shù)結(jié)果存儲(chǔ)在緩存中,避免重復(fù)計(jì)算,從而提高頁面加載速度和響應(yīng)速度,本文主要介紹了Javascript函數(shù)緩存的實(shí)現(xiàn)及應(yīng)用場景,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
純編碼實(shí)現(xiàn)微信小程序彈幕效果(非視頻底)
這篇文章主要介紹了微信小程序彈幕純編碼實(shí)現(xiàn),這種效果是非視頻底,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
JAVASCRIPT style 中visibility和display之間的區(qū)別
visibility屬性用來確定元素是顯示還是隱藏的,這用visibility="visible|hidden"來表示(visible表示顯示,hidden表示隱藏)。2010-01-01
JS實(shí)現(xiàn)點(diǎn)擊鏈接切換顯示隱藏內(nèi)容的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊鏈接切換顯示隱藏內(nèi)容的方法,涉及javascript鼠標(biāo)事件響應(yīng)及頁面元素屬性動(dòng)態(tài)變換相關(guān)操作技巧,需要的朋友可以參考下2017-10-10

