模块Path:
path.normalize(url)
path.join(url1, url2, ..)
path.resolve(url1, url2, ..)
path.relative(url1, url2)
path.dirname(url)
path.basename(url)
path.extname(url)
TIPS:
以上是Path对象的API,调用方便简洁,一般在项目中我会先写一个*目录
var publicDir = path.resolve(__dirname, './src')
其余的子目录在此*目录衍生
var pagesDir = path.resolve(publicDir, './pages')
var staticDir = path.resolve(publicDir, '.static')
var buildDir = path.resolve(publicDir, './build')
var vendorDir = path.resolve(publicDir, './vendor')
最后把对象挂载出去,其他文件需要使用到路径的,直接引入就可以了
module.exports = {
pagesDir: path.resolve(publicDir, './pages')
staticDir: path.resolve(publicDir, '.static')
buildDir: path.resolve(publicDir, './build')
vendorDir: path.resolve(publicDir, './vendor')
}
API样例:
var path = require('path'); /*
* 格式化路径:../返回上一级,所以结果为
* -->'/foo/bar/baz/asdf'
*/
path.normalize('/foo/bar//baz/asdf/quux/..'); /*
* 拼接路径:拼接完成后调用normalize(),结果同上
* -->'/foo/bar/baz/asdf'
*/
path.join('///foo', 'bar', '//baz/asdf', 'quux', '..'); /*
* 路径导航:等于cd一遍每个子路径
*/
path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile') /*
* 相对路径:b相对于a的路径为:'../../impl/bbb',在a的路径上结果就可以访问到b文件
* -->'../../impl/bbb'
*/
path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb') /*
* 文件夹名称:文件夹所在路径的名称
* -->'/foo/bar/baz/asdf'
*/
path.dirname('/foo/bar/baz/asdf/quux') /*
* 文件路径:返回指定的文件名,可排除后缀名
* -->'quux'
*/
path.basename('/foo/bar/baz/asdf/quux.html', '.html') /*
* 拓展名:返回文件拓展名
*/
path.extname('index.html') // '.html'
path.extname('index.') // '.'
path.extname('index') // '' module.exports = {
entry: [],
output:{
path: buildDir,
filename: '[name]/entry.js'
}
};