function resolve(dir) { return path.join(__dirname, dir); }
时间: 2024-02-03 08:03:30 浏览: 22
这段代码是什么意思?
这段代码定义了一个名为 resolve 的函数,用于获取项目中指定目录的绝对路径。其中,path.join() 方法用于拼接路径信息,__dirname 表示当前执行文件的绝对路径,dir 参数表示需要拼接的目录路径。该函数主要用于简化在项目中获取绝对路径的操作,方便在各个模块中引用其他模块或文件时使用相对路径。例如,在 webpack 配置文件中,可以使用 resolve 函数获取指定目录的绝对路径,如:resolve('src/components')。
相关问题
const { defineConfig } = require("@vue/cli-service"); const path = require("path"); function resolve(dir) { return path.join(__dirname, dir); } module.exports = defineConfig({ transpileDependencies: true, lintOnSave: false, css: { loaderOptions: { less: { additionalData: '@import "@/design/var/index.less";@import "@/components/style/index.less";', }, }, }, chainWebpack: (config) => { config.module.rules.delete("svg"); // 重点:删除默认配置中处理svg config.module .rule("svg-sprite-loader") // rule 匹配规则 .test(/\.svg$/) // 用正则匹配 文件 .include // 包含 .add(resolve("src/icons")) // 处理svg目录 .end() .use("svg-sprite-loader") // 配置loader use() 使用哪个loader .loader("svg-sprite-loader") // 加载loader .options({ // [name] 变量。一般表示匹配到的文件名 xxx.svg // 注意: symbolId 在 <use xlink:href="#dl-icon-svg文件名" /> symbolId: "dl-icon-[name]", // 将所有的.svg 集成到 symbol中,当使用 类名 icon-文件名 }); }, });在这个代码里如何添加devServer和proxy
你可以通过在 `module.exports` 下面添加 `devServer` 和 `proxy` 配置来配置 `devServer` 和 `proxy`,具体如下:
```javascript
const { defineConfig } = require("@vue/cli-service");
const path = require("path");
function resolve(dir) {
return path.join(__dirname, dir);
}
module.exports = defineConfig({
transpileDependencies: true,
lintOnSave: false,
css: {
loaderOptions: {
less: {
additionalData:
'@import "@/design/var/index.less";@import "@/components/style/index.less";',
},
},
},
chainWebpack: (config) => {
config.module.rules.delete("svg"); // 重点:删除默认配置中处理svg
config.module
.rule("svg-sprite-loader") // rule 匹配规则
.test(/\.svg$/) // 用正则匹配 文件
.include // 包含
.add(resolve("src/icons")) // 处理svg目录
.end()
.use("svg-sprite-loader") // 配置loader use() 使用哪个loader
.loader("svg-sprite-loader") // 加载loader
.options({
// [name] 变量。一般表示匹配到的文件名 xxx.svg
// 注意: symbolId 在 <use xlink:href="#dl-icon-svg文件名" />
symbolId: "dl-icon-[name]", // 将所有的.svg 集成到 symbol中,当使用 类名 icon-文件名
});
},
devServer: {
host: '0.0.0.0',
port: 8080,
https: false,
open: true,
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
},
proxy: {
'/foo': {
target: 'http://localhost:8080',
changeOrigin: true,
pathRewrite: {
'^/foo': ''
}
}
}
});
```
上面的代码中,`devServer` 和 `proxy` 都是在 `module.exports` 中添加的配置项,其中:
- `devServer` 用于配置本地开发服务器;
- `proxy` 用于配置代理请求;
这里的配置和之前的一样,你可以根据自己的需求进行修改。
js动态修改path值 svg_vue.config.js 的完整配置(超详细)!
要动态修改 SVG 文件中的 path 值,需要使用 webpack 和 svg-sprite-loader 插件。以下是完整的配置:
1. 安装 svg-sprite-loader 插件
```bash
npm install svg-sprite-loader --save-dev
```
2. 在 vue.config.js 中添加配置
```javascript
const path = require('path')
function resolve(dir) {
return path.join(__dirname, '.', dir)
}
module.exports = {
chainWebpack: config => {
const svgRule = config.module.rule('svg')
svgRule.uses.clear()
svgRule
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]',
extract: true,
spriteFilename: 'img/icons.svg',
runtimeCompat: true,
publicPath: process.env.NODE_ENV === 'production' ? '/your/public/path/' : '/',
symbolDest: 'img/icons/[name].svg',
pluginHooks: {
// 用于动态修改 path 值
beforeSanitizeElements: [function (sprite) {
sprite.querySelectorAll('path').forEach((path) => {
path.setAttribute('d', 'M0 0H10V10H0z')
})
}]
}
})
const fileRule = config.module.rule('file')
fileRule.uses.clear()
fileRule
.use('file-loader')
.loader('file-loader')
.options({
name: 'img/[name].[hash:8].[ext]'
})
},
configureWebpack: {
resolve: {
alias: {
'@': resolve('src')
}
}
}
}
```
这里解释一下每个配置项的含义:
- `symbolId`:设置 SVG sprite 的 symbol id 的名字,这里使用 `icon-[name]` 的方式命名
- `extract`:是否将构建后的 sprite 文件提取出来,默认为 true
- `spriteFilename`:提取出来的 sprite 文件名称,默认为 `img/icons.svg`
- `runtimeCompat`:兼容各种运行时,可以让你在 Vue 中使用 SVG sprite,如果使用了 Vue CLI 4.x,建议开启
- `publicPath`:构建后的 publicPath,建议使用绝对路径
- `symbolDest`:构建后的 sprite 文件存储路径,这里设置为 `img/icons/[name].svg`
- `pluginHooks.beforeSanitizeElements`:使用插件钩子,在 sanitizeElements 之前修改 path 值
3. 在组件中使用 SVG sprite
```vue
<template>
<div>
<svg aria-hidden="true" class="icon">
<use xlink:href="#icon-xxx"></use>
</svg>
</div>
</template>
<script>
export default {
name: 'ComponentName'
}
</script>
<style scoped>
.icon {
width: 20px;
height: 20px;
fill: currentColor;
}
</style>
```
其中,`xlink:href` 中的 `#icon-xxx` 对应到 SVG sprite 中的 symbol id,`fill: currentColor` 可以自动填充颜色。
注意:如果要修改 SVG sprite 中的 path 值,需要在插件钩子中进行修改,具体的修改方式可以根据实际情况进行调整。