故事:其实咱们在作vue开发或者react开发 或者普通的webpack项目的时候 通常可能都是直接copy以前的webpack配置文件或者脚手架,可是当让咱们本身配置的时候 或者解读脚手架原理的时候,特别是细节的问题 或多或少都有些迷茫,所以我记录了webpack配置项目的细节javascript
node中路径大体分为5类 __dirname __filename process.cwd() ./ 和 ../
css
const path = require("path")
console.log(__dirname)
console.log(__filename)
console.log(process.cwd())
console.log(path.resolve('./'))
console.log(path.resolve('../'))复制代码
执行 noed src/main.js 结果以下html
总结:vue
__dirname
: 老是返回被执行的 js 所在文件夹的绝对路径java
__filename
:老是返回被执行的 js文件 的绝对路径 node
process.cwd()
: 老是返回运行 node 命令时所在的文件夹的绝对路径 react
./
:在 require() 中使用是跟 __dirname 的效果相同,不会由于启动脚本的目录不同而改变, 在其余状况下跟 process.cwd() 效果相同,是相对于启动脚本所在目录的路径 webpack
../
: 和./差很少 只是他是返回的上一级目录
web
注意: 在上面咱们使用了path.resolve()方法将'./' 和 '../'解析成了绝对路径express
·
在node.js中,提供了一个path某块,在这个模块中,提供了许多使用的,可被用来处理与转换路径的方法与属性。下面咱们就来对这些方法与属性作一下介绍。
在webpack中比较经常使用的方法就是path.join()
和path.resolve()
const path = require("path")
console.log(path.join('src','main.js'))
console.log(path.resolve('src','main.js'))复制代码
执行 node src/main.js 结果以下
总结:
path.join()
:方法使用平台特定的分隔符把所有给定的 path 片断链接到一块儿,并规范化生成的路径。
path.resolve
:方法会把一个路径或路径片断的序列解析为一个绝对路径。
经过环境变量划分开发环境和生产环境
"scripts": {
"dev": "set NODE_ENV=development && node src/main.js"
}复制代码
----------main.js
console.log(process.env.NODE_ENV)
复制代码
注意 node中经常使用的到的环境变量是NODE_ENV
,首先查看是否存在 set NODE_ENV
若是不存在则添加环境变量 set NODE_ENV=development
执行 npm run dev 结果以下
所以 咱们经常在项目中以下设置
"scripts": {
"dev": "set NODE_ENV=development && node build/dev-server.js",
"build": "set NODE_ENV=production && node build/build.js"
}
复制代码
通常方法咱们都是用webpack-dev-server
作热更新执行webpack-dev-server –config wepack.config.js
配置以下
devServer: {
historyApiFallback: true, // 404的页面会自动跳转到/页面
inline: true, // 文件改变自动刷新页面
port: 8088// 服务器端口
}
复制代码
可是咱们在实际项目中并不会这样作 咱们通常会使用中间件 webpack-dev-middleware
webpack-hot-middleware
express 结合使用 由于 webpack-dev-server
其实是一个轻量级的node.js express服务器,实际上至关因而一个封装好的express的http服务器+调用webpack-dev-middleware。 所以咱们使用express服务器进行更多的扩展,结合使用其余的中间件来响应http请求及其余的功能这样扩展性更好更灵活 咱们通常经过执行 node build/dev-server.js
(配置文件路径)。 中间件,是一个函数,用于访问请求对象、响应对象和web应用中处于请求-响应循环流程中的中间件。它的功能包括:执行任何代码、修改请求和响应对象、终结请求-响应循环和调用堆栈中的下一个中间件。
dev-server.js部分配置以下
//使用Node自带的文件路径工具
const path = require('path');
//使用express
const express = require('express');
//使用express启动一个服务
const app = express();
const webpack = require('webpack');
const config = require('../config');
const port = 8088
const webpackdevConfig = require('./webpack.dev.config.js')
//启动webpack进行编译
const compiler = webpack(webpackdevConfig)
// 启动 webpack-dev-middleware,将 编译后的文件暂存到内存中
const devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackdevConfig.output.publicPath
})
const hotMiddleware = require('webpack-hot-middleware')(compiler, {
log: () => {}
})
// 服务器部署 webpack 打包的静态资源
app.use(devMiddleware)
// 使用热更新, 若是编译出现错误会实时展现编译错误
app.use(hotMiddleware)
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
//为静态资源提供响应服务
app.use(express.static('./'))
const server = app.listen(port)
复制代码
一下能够忽略
const path = require('path');
const config = require('../config')
const CleanWebpackPlugin = require('clean-webpack-plugin');
const setPath = function(_path) {
return path.posix.join(config.dev.assetsSubDirectory, _path)
}
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
module.exports = {
resolve: {
alias: {
'@': resolve('src')
}
},
module: {
rules: [
{
test: /\.(png|jpg|gif)$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000,
name: setPath('images/[name].[hash:7].[ext]')
}
}]
},
{
test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader'
}
]
},
plugins: [
new CleanWebpackPlugin(['dist'], {
root: path.resolve(__dirname, ".."),
verbose: false
})
]
}复制代码
const path = require('path');
const webpack = require('webpack')
const config = require('../config')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpackbaseConfig = require('./webpack.base.config.js');
const merge = require('webpack-merge')
function resolve (dir) {
return path.join(__dirname, '..', dir)
}
const assetsSubDirectory = "static"
const oEntry = {
'bundle':'./src/main.js',
'vendor': './src/scripts/carousel.js'
}
Object.keys(oEntry).forEach(function(name) {
oEntry[name] = ['./build/dev-client'].concat(oEntry[name])
})
module.exports = merge(webpackbaseConfig,{
entry: oEntry,
output: {
path: config.build.assetsRoot,
publicPath: config.build.assetsPublicPath,
filename: '[name].js'
},
devtool: '#cheap-module-eval-source-map',
plugins: [
new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
})
]
}) 复制代码
const path = require('path');
const webpack = require('webpack')
const config = require('../config')
const webpackbaseConfig = require('./webpack.base.config.js');
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const merge = require('webpack-merge')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const setPath = function(_path) {
return path.posix.join(config.build.assetsSubDirectory, _path)
}
var oEntry = {
'bundle':'./src/main.js',
'vendor': './src/scripts/carousel.js'
}
module.exports = merge(webpackbaseConfig,{
entry: oEntry,
output: {
path: config.build.assetsRoot,
publicPath: config.build.assetsPublicPath,
filename: setPath('js/[name].[chunkhash].js')
},
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader:'css-loader'
},
{
loader:'sass-loader'
}
]
})
},
{
test: /\.html$/,
loader: 'html-withimg-loader'
}
]
},
plugins: [
new webpack.DefinePlugin({
"process.env" : {
NODE_ENV : "production"
}
}),
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
removeComments: true,
removeRedundantAttributes: true
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
},
sourceMap: true
}),
new ExtractTextPlugin({
filename: setPath('css/[name].[contenthash].css')
})/*,
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
}
])*/
]
})复制代码