日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

webpack常用配置

發(fā)布時(shí)間:2023/12/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 webpack常用配置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.加載CSS
命令行輸入

npm install --save-dev style-loader css-loader

webpack.config.js配置如下

const path = require('path');module.exports = {entry: './src/index.js',output: {filename: 'bundle.js',path: path.resolve(__dirname, 'dist')}, + module: { + rules: [ + { + test: /\.css$/, + use: [ + 'style-loader', + 'css-loader' + ] + } + ] + }};

文件結(jié)構(gòu)如下

webpack-demo|- package.json|- webpack.config.js|- /dist|- bundle.js|- index.html|- /src + |- style.css|- index.js|- /node_modules

style.css

.hello {color: red; }

src/index.js

import _ from 'lodash'; + import './style.css';function component() {var element = document.createElement('div');// Lodash, now imported by this scriptelement.innerHTML = _.join(['Hello', 'webpack'], ' '); + element.classList.add('hello');return element;}document.body.appendChild(component());

命令行 npm run build

2.加載圖片

npm install --save-dev file-loader

這里的文件結(jié)構(gòu)將要發(fā)生變化,以下內(nèi)容將于實(shí)際的開發(fā)調(diào)試相關(guān)


webpack-demo|- package.json|- webpack.config.js|- /dist|- /src|- index.js + |- print.js|- /node_modules

3.HtmlWebpackPlugin 自動(dòng)生成index.html

命令行

npm install --save-dev html-webpack-plugin

src/print.js

export default function printMe() {console.log('I get called from print.js!'); }

src/index.js

import _ from 'lodash'; + import printMe from './print.js';function component() {var element = document.createElement('div'); + var btn = document.createElement('button');element.innerHTML = _.join(['Hello', 'webpack'], ' ');+ btn.innerHTML = 'Click me and check the console!'; + btn.onclick = printMe; + + element.appendChild(btn);return element;}document.body.appendChild(component());

webpack.config.js

const path = require('path'); + const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = {entry: { - index: './src/index.js', + app: './src/index.js', + print: './src/print.js'},output: { - filename: 'bundle.js', + filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}, + plugins: [ + new HtmlWebpackPlugin({ + title: 'Output Management' + }) + ],};

先刪除dist/index.html,然后命令行 npm run build, 觀察現(xiàn)象

4.Source maps
用于調(diào)試時(shí),控制臺(tái)精準(zhǔn)定定位錯(cuò)誤位置。這里只介紹一個(gè)簡單的 'inline-source-map'。
webpack.config.js

const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const CleanWebpackPlugin = require('clean-webpack-plugin');module.exports = {entry: {app: './src/index.js',print: './src/print.js'}, + devtool: 'inline-source-map',plugins: [new CleanWebpackPlugin(['dist']),new HtmlWebpackPlugin({title: 'Development'})],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}};

5.webpack-dev-server 熱更新
首先命令行

npm install --save-dev webpack-dev-server

webpack.config.js

const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const CleanWebpackPlugin = require('clean-webpack-plugin');module.exports = {entry: {app: './src/index.js',print: './src/print.js'},devtool: 'inline-source-map', + devServer: { + contentBase: './dist' + },plugins: [new CleanWebpackPlugin(['dist']),new HtmlWebpackPlugin({title: 'Development'})],output: {filename: '[name].bundle.js',path: path.resolve(__dirname, 'dist')}};

package.json

{"name": "development","version": "1.0.0","description": "","main": "webpack.config.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1", + "start": "webpack-dev-server --open","build": "webpack"},"keywords": [],"author": "","license": "ISC","devDependencies": {"clean-webpack-plugin": "^0.1.16","css-loader": "^0.28.4","csv-loader": "^2.1.1","file-loader": "^0.11.2","html-webpack-plugin": "^2.29.0","style-loader": "^0.18.2","webpack": "^3.0.0","xml-loader": "^1.2.1"}}

最后 npm start 啟動(dòng)熱加載,更改文件瀏覽器會(huì)自動(dòng)刷新。

關(guān)于熱加載的配置還可以使用 watch mode 和webpack-dev-middleware。 其中watch mode就是在package.json文件的"scripts"屬性下添加

....... "scripts": {"watch": "webpack --progress --watch",}, .......

轉(zhuǎn)載于:https://www.cnblogs.com/renzhiwei2017/p/7676926.html

總結(jié)

以上是生活随笔為你收集整理的webpack常用配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。