相關連接css
build/
js/
xxx.js
css/
xxx.css
images/
xxx.jpg
index.html
複製代碼
config/webpack.common.jswebpack
function webpackCommonConfigCreator(options){
...
return {
...
output: {
- filename: "bundle.js",
+ filename: "js/bundle.js",
path: path.resolve(__dirname, "../build"),
},
module: {
rules: [
...
{
test: /\.(jpg|png|svg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
- name: '[hash].[ext]',
+ name: 'images/[hash].[ext]',
}
},
]
},
]
},
plugins: [
...
new ExtractTextPlugin({
- filename: "[name][hash].css"
+ filename: "css/[name][hash].css"
}),
]
}
}
複製代碼
經過相對output目錄對資源命名前加上目錄名git
yarn build
, 效果:yarn add antd
複製代碼
config/webpack.common.jsgithub
...
modules: {
rules: [
{
test: /\.(css|scss)$/,
include: path.resolve(__dirname, '../src'),
use: ExtractTextPlugin.extract({
...
})
},
+ {
+ test: /\.(css|scss)$/,
+ exclude: path.resolve(__dirname, '../src'),
+ use: [
+ "style-loader/url",
+ {
+ loader: 'file-loader',
+ options: {
+ name: "css/[name].css"
+ }
+ }
+ ]
+ },
]
}
...
複製代碼
引入antd樣式表web
src/index.jsvue-cli
import React from 'react';
import ReactDom from 'react-dom';
import App from './App.js';
+ import 'antd/dist/antd.css';
ReactDom.render(<App />, document.getElementById('root'));
複製代碼
建立目錄 src/component
npm
src/component/Btn.js
import React from 'react';
import {Button} from 'antd';
export default function Btn(){
return (
<div>
<Button type="danger">click me2</Button>
</div>
)
}
複製代碼
引入組件
src/App.js
+ import Btn from './components/Btn';
function App(){
return (
<div className={styles.title}>
...
+ <Btn />
</div>
)
}
...
複製代碼
yarn start
,成功, 可是bundle.js
體積很是的大,須要優化對chunks屬性我在一個思否回答中答了答案,連接: segmentfault.com/q/101000001…
config/webpack.common.js
function webpackCommonConfigCreator(options){
return {
...
output: {
- filename: "js/bundle.js",
+ filename: "js/[name].js",
path: path.resolve(__dirname, "../build"),
},
...
+ optimization: {
+ splitChunks: {
+ chunks: "all",
+ minSize: 50000,
+ minChunks: 1,
+ }
+ }
}
}
複製代碼
yarn build
, 打包以下config/webpack.common.js
output: {
- filename: "js/[name].js",
+ filename: "js/[name][hash].js",
path: path.resolve(__dirname, "../build"),
},
複製代碼
yarn build
**修改開發代碼後再次打包 **
能夠看到修改代碼後,打包的js文件名hash值變了,瀏覽器請求總可以獲取到最新的代碼
[chunkhash]
替代[hash]
config/webpack.common.js
output: {
- filename: "js/[name][hash].js",
path: path.resolve(__dirname, "../build"),
},
複製代碼
config/webpack.prod.js
+ output: {
+ filename: "js/[name][chunkhash].js",
+ },
複製代碼
config/webpack.dev.js
+ output: {
+ filename: "js/[name][hash].js",
+ },
複製代碼
yarn build
修改開發代碼後再次打包
inline-source-map
, 生產模式使用source-map
config/webpack.dev.js
const config = {
...
+ devtool: "inline-source-map",
...
}
複製代碼
config/webpack.prod.js
const config = {
...
+ devtool: "source-map",
...
}
複製代碼
安裝
yarn add webpack-manifest-plugin -D
複製代碼
配置
config/webpack.prod.js
...
const ManifestPlugin = require('webpack-manifest-plugin');
const config = {
...
plugins: [
...
new ManifestPlugin(),
]
}
複製代碼
當咱們使用vue-cli
或者create-react-app
腳手架打包項目後,未修改publicPath的狀況下直接打開index.html
會報錯沒法找到js、css靜態資源
由於腳手架默認的publicPath設置爲 /
,則對應的資源外鏈都是從服務器路徑/
開始尋找資源
config/webpack.common.js
function webpackCommonConfigCreator(options){
return {
...
output: {
...
+ publicPath: "/"
},
...
module: {
rules: [
...
{
test: /\.(jpg|png|svg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
name: 'images/[hash].[ext]',
+ publicPath: "/"
}
},
]
},
]
}
}
}
複製代碼
yarn build
, 打包完成後推薦使用http-serveryarn global add http-server
npm install http-server -g
複製代碼
http-server build
源碼github倉庫: github.com/tanf1995/We…