简体   繁体   English

如何在 webpack 配置中定义 url 的别名,类似于 vite

[英]How to define alias to url in webpack config similarly to vite

using Vite to generate a VueJs project npm init vue@latest I get the following alias in the configuration file vite.config.js :使用Vite生成 VueJs 项目npm init vue@latest我在配置文件vite.config.js中得到以下别名:

import { fileURLToPath, URL } from 'node:url'
resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
}

Replacing the upper chunck of code in webpack v5 (for another project):替换webpack v5中的上部代码块(用于另一个项目):

const { fileURLToPath, URL } = require("url")
resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
}

yields the following error:产生以下错误:

'@': fileURLToPath(new URL('./src', import.meta.url))
SyntaxError: Cannot use 'import.meta' outside a module

Can you please tell me how can I replace the aliasing correctly in webpack v5 ?您能告诉我如何在webpack v5中正确替换别名吗? thank in advance.预先感谢。

I'm not sure about the fileURLToPath , but in the docs , they are using path.resolve我不确定fileURLToPath ,但在docs中,他们正在使用path.resolve

const path = require('path');
resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src/components')
    }
}

WEBPACK WEBPACK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM