Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

转换后启动项目报错[plugin:vite-plugin-importer] [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly #39

Open
Pehfan opened this issue Aug 31, 2021 · 8 comments

Comments

@Pehfan
Copy link

Pehfan commented Aug 31, 2021

image

@dravenww
Copy link
Member

dravenww commented Sep 1, 2021

项目目录截图看下,辛苦再提供下vite.config.js的内容

@Pehfan
Copy link
Author

Pehfan commented Sep 1, 2021

项目目录截图看下,辛苦再提供下vite.config.js的内容

目录结构如下
微信截图_20210901105301
image

vite.config.js 如下 删除usePluginImport这个错误消失,但是会有引入其它文件的报错
/* eslint-disable */
import legacyPlugin from '@vitejs/plugin-legacy';
import usePluginImport from 'vite-plugin-importer';
import {
viteMockServe
} from 'vite-plugin-mock';
import * as path from 'path';
import {
createVuePlugin
} from 'vite-plugin-vue2';
// @see https://cn.vitejs.dev/config/
export default ({
command,
mode
}) => {
let rollupOptions = {};

let optimizeDeps = {};

let alias = {
'@': path.resolve(__dirname, '.\src'),
'vue$': 'vue/dist/vue.esm.js',
}

let proxy = {}

let define = {
'process.env.NODE_ENV': '"development"',
}

let esbuild = {}

return {
base: './', // index.html文件所在位置
root: './', // js导入的资源路径,src
resolve: {
alias,
},
define: define,
server: {
// 代理
proxy,
},
build: {
target: 'es2015',
minify: 'terser', // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
manifest: false, // 是否产出maifest.json
sourcemap: false, // 是否产出soucemap.json
outDir: 'build', // 产出目录
rollupOptions,
},
esbuild,
optimizeDeps,
plugins: [
legacyPlugin({
targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
}), usePluginImport({
libraryName: " ", // todo please input your babel-plugin-import config
libraryDirectory: " ",
style: "css",
}), viteMockServe({
mockPath: 'mock',
localEnabled: command === 'serve',
}), createVuePlugin(),
],
css: {
preprocessorOptions: {
less: {
// 支持内联 JavaScript
javascriptEnabled: true,
}
}
},
}
}

@dravenww
Copy link
Member

dravenww commented Sep 2, 2021

之所以会有vite-plugin-importer这个插件的引入,是因为在你的项目中有babel-plugin-import,这是与之对应的vite的插件,需要手动把所对应的配置填充到usePluginImport内,有一个TODO需要去改一下,改完后再试下。
另外,如果你的项目中全部都是require的方式引入资源,需要去改成esmodule的方式(import)才可以成功使用vite。

@octopustail
Copy link

把`babel-plugin-import的配置填充到usePluginImport了,但是这个报错还是存在。

// vite.config.js 中
plugins: [
      vitePluginReactJsSupport([], { jsxInject: false }),
      legacyPlugin({
        targets: ['Android > 39', 'Chrome >= 60', 'Safari >= 10.1', 'iOS >= 10.3', 'Firefox >= 54', 'Edge >= 15'],
      }),
      usePluginImport(
        {
          libraryName: "antd", // todo please input your babel-plugin-import config
          libraryDirectory: "es",
          style: true,
        }
      ),
...// 其他plugin
    ],

@dravenww
Copy link
Member

项目中的引用是require的方式,还是import的方式呢?

@oceanchiu
Copy link

我也是一样的问题有人解决了吗

@clddup
Copy link

clddup commented Feb 13, 2023

我用的 ant-design-vue-pro 也是一样的报错,

@guchengnan
Copy link

guchengnan commented Feb 13, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants