webpack教程(二)——webpack.config.js文件


首先我們需要安裝一個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可以看到如下效果

即為成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM