webpack是一個模塊打包工具,可使用它管理項目中的模塊依賴,並編譯輸出模塊所需的靜態文件。它能夠很好地管理、打包開發中所用到的HTML,CSS,JavaScript和靜態文件(圖片,字體)等,讓開發更高效。對於不一樣類型的依賴,webpack有對應的模塊加載器,並且會分析模塊間的依賴關係,最後合併生成優化的靜態資源。css
將全部依賴打包成一個bundle.js,經過代碼分割成單元片斷按需加載html
bundle是webpack打包出來的文件,chunk是webpack在進行模塊的依賴分析的時候,代碼分割出來的代碼塊。module是開發中的單個模塊前端
能夠用一些官方腳手架vue
// 首先安裝
npm install -g @vue/cli
// 新建項目hello
vue create hello
複製代碼
// 確保安裝了npx,npx在npm5.2.0默認安裝了
// 新建項目hello
npx create-nuxt-app hello
複製代碼
module.exports = {
entry: './path/to/my/entry/file.js'
}
複製代碼
module.entrys = {
entry: {
pageOne: './src/pageOne/index.js',
pageTwo: './src/pageTwo/index.js'
}
}
複製代碼
webpack-dev-server使用內存來存儲webpack開發環境下的打包文件,而且可使用模塊熱更新,相比傳統http服務器開發更加簡單高效node
webpack的一個功能,可使代碼修改後不用刷新瀏覽器就自動更新,高級版的自動刷新瀏覽器webpack
webpack-dev-server支持兩種模式來自動刷新頁面nginx
http://localhost:8080/webpack-dev-server/index.html
// 以命令行啓動webpack-dev-server有兩種方式
// 方式1 在命令行中添加--inline命令
// 方式2 在webpack-config.js添加devServer:{inline: true}
// 以node.js API啓動有兩種方式
// 方式1 添加webpack-dev-server/client?http://localhost:8080/到webpack配置的entry入口點
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080/");
// 將<script src="http://localhost:8080/webpack-dev-server.js"></script>添加到html文件中
複製代碼
loaderweb
// url-loader加強版的file-loader,小於limit的轉爲Base64,大於limit的調用file-loader
npm install url-loader -D
// 使用
module.exports = {
module: {
rules: [{
test: /\.(png|jpg|gif)$/,
use: [{
loader: 'url-loader',
options: {
outputPath: 'images/',
limit: 500 //小於500B的文件打包出Base64格式,寫入JS
}
}]
}]
}
}
複製代碼
plugins面試
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
//...
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html', // 配置輸出文件名和路徑
template: './public/index.html', // 配置要被編譯的html文件
hash: true,
// 壓縮 => production 模式使用
minify: {
removeAttributeQuotes: true, //刪除雙引號
collapseWhitespace: true //摺疊 html 爲一行
}
})
]
}
複製代碼
npm install clean-webpack-plugin -D
// 修改webpack.config.js
const cleanWebpackPlugin=require('clean-webpack-plugin')
module.exports = {
plugins: [new cleanWebpackPlugin(['dist'])]
}
複製代碼
安裝 npm i -D @babel-preset-env @babel-core babel-loader
vue-cli
配置.babelrc
{
"presets": ['@babel/preset-env']
}
複製代碼
配置webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {loader: 'babel-loader'}
}]
}
}
複製代碼
module.exports = {
optimization: {
splitChunks: {
common: {
// 抽離公共代碼
chunks: 'initial',
name: 'common', // 打包後的文件名
minChunks: 2, // 最小引用2次
minSize: 0 // 超出0字節就生成一個新包
},
styles: {
// 抽離公用代碼
name: 'styles',
test: /\.css$/,
chunks: 'all',
minChunks: 2,
enforce: true
},
vender: {
// 抽離第三方插件
test: /node_modules/,
chunks: 'initial',
name: 'vendor', // 打包後的文件名
priority: 10 // 設置優先級,防止與自定義公共代碼提取時被覆蓋,不進行打包
}
}
}
}
複製代碼
Tree-shaking是指在打包中取出那些引入了但在代碼中沒有被用到的死代碼。webpack中經過uglifysPlugin來Tree-shaking JS。CSS須要使用purify-CSS
Child.prototype = new Parent();
Parent.call(this)
function Child(name,age){
// 繼承屬性
Parent.call(this, name)
this.age=age
}
// 繼承方法
Child.prototype = new Parent()
Child.prototype.constructor = Child;
複製代碼
function object(obj){
function F(){}
F.prototype=obj
return new F();
}
var person1=object(person);
複製代碼
在ES5中Object.create()可替換上面的方法object() var person1 = Object.create(person);
function createAnother(obj){
var clone=object(obj);
// ES5中用這個
// var clone=Object.create(obj);
// 加強對象
clone.sayHi=function(){};
return clone;
}
var person1=createAnother(person)
複製代碼
// 借用構造函數加強子類實例屬性(支持傳參和避免篡改)
function Child(name,age){
// 繼承屬性
Parent.call(this, name)
this.age=age
}
function inheritPrototype(Child, Parent){
var prototype=Object.create(Parent.prototype);
prototype.constructor=Child;
Child.prototype=prototype;
}
// 將父類原型指向子類,這樣子類就能使用父類原型鏈的屬性/方法
inheritPrototype(Child, Parent);
複製代碼
優勢:只調用一次構造函數,原型鏈不變,是最成熟的
重點:利用Object.assign將父類原型上的方法拷貝到子類原型上,這樣子類實例實例就可使用父類的方法
Object.assign(Child.prototype, Parent.prototype);
Child.prototype.constructor=Child;
複製代碼
重點:使用extends代表繼承自哪一個父類,而且在子類構造函數中必須使用super,能夠看作是Parent.call(this,value)
class Parent{
constructor(value){
this.val=value
}
}
class Child extends Parent{
constructor(value){
super(value)
this.val = value
}
}
複製代碼