相关连接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…