webpack教程之webpack.config.js配置文件
首先我們需要安裝一個webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創(chuàng)建html入口文件。執(zhí)行如下命令
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;
}
在根目錄下創(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',
})
]
};
打開命令行,切換到項目目錄下,執(zhí)行webpack命令。

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

首先我們需要安裝一個webpack插件html-webpack-plugin,該插件的作用是幫助我們生成創(chuàng)建html入口文件。執(zhí)行如下命令
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;
}
在根目錄下創(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',
})
]
};
打開命令行,切換到項目目錄下,執(zhí)行webpack命令。

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

看下我們的build/app.js

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

即為成功。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JAVASCRIPT style 中visibility和display之間的區(qū)別
visibility屬性用來確定元素是顯示還是隱藏的,這用visibility="visible|hidden"來表示(visible表示顯示,hidden表示隱藏)。2010-01-01

