jd-opensource / jd-opensource/nutui

按需引入报错

Open
#3,230 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Vue
Stars
6.5k
Forks
835
PR merge metrics
No merged PRs in 30d

Description

### NutUI 包名

@nutui/nutui-taro

### NutUI 版本号

4.3.13

### 平台

tt

### 重现链接

https://github.com/enjoy-myLife/taro-feishu-demo

### 重现步骤

// config/index.ts
import { defineConfig, type UserConfigExport } from "@tarojs/cli";
import path from "path";
import Components from "unplugin-vue-components/vite";
import NutUIResolver from "@nutui/auto-import-resolver";

import devConfig from "./dev";
import prodConfig from "./prod";

// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
export default defineConfig<"vite">(async (merge, { command, mode }) => {
const baseConfig: UserConfigExport<"vite"> = {
projectName: "food",
date: "2024-11-27",
designWidth(input) {
// 配置 NutUI 375 尺寸
if (input?.file?.replace(/\\+/g, "/").indexOf("@nutui") > -1) {
return 375;
}
// 全局使用 Taro 默认的 750 尺寸
return 750;
},
deviceRatio: {
640: 2.34 / 2,
750: 1,
828: 1.81 / 2,
375: 2 / 1,
},
sass: {
resource: [path.resolve(__dirname, "../src/styles/custom_theme.scss")],
data: `@import "@nutui/nutui-taro/dist/styles/variables.scss";`,
},
sourceRoot: "src",
outputRoot: "dist",
plugins: ["@tarojs/plugin-html", "@tarojs/plugin-platform-lark"],
alias: {
"@": path.resolve(__dirname, "../src"),
},
defineConstants: {
TARO_APP_BASE_URL: JSON.stringify(process.env.TARO_APP_BASE_URL), // 这里需要JSON.stringify 序列化一次,否则会报错
},
copy: {
patterns: [],
options: {},
},
framework: "vue3",
compiler: {
type: "vite",
vitePlugins: [
// 开启 unplugin 插件,自动按需引入 NutUI 组件
Components({
resolvers: [
NutUIResolver({
taro: true,
autoImport: true,
importStyle: false,
}),
],
}),
],
},
mini: {
postcss: {
pxtransform: {
enable: true,
config: {},
},
cssModules: {
enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
config: {
namingPattern: "module", // 转换模式,取值为 global/module
generateScopedName: "[name]__[local]___[hash:base64:5]",
},
},
},
},
h5: {
publicPath: "/",
staticDirectory: "static",

miniCssExtractPluginOption: {
ignoreOrder: true,
filename: "css/[name].[hash].css",
chunkFilename: "css/[name].[chunkhash].css",
},
postcss: {
autoprefixer: {
enable: true,
config: {},
},
cssModules: {
enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
config: {
namingPattern: "module", // 转换模式,取值为 global/module
generateScopedName: "[name]__[local]___[hash:base64:5]",
},
},
},
},
rn: {
appName: "taroDemo",
postcss: {
cssModules: {
enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
},
},
},
};
if (process.env.NODE_ENV === "development") {
// 本地开发构建配置(不混淆压缩)
return merge({}, baseConfig, devConfig);
}
// 生产构建配置(默认开启压缩混淆等)
return merge({}, baseConfig, prodConfig);
});

// app.ts
import { createApp } from "vue";
// 国际化
import i18n from "@/lang/i18n";
import { local } from "@/lang";
import "./app.scss";

const App = createApp({
onShow(options) {
// console.log('i18n',i18n)
},
onLaunch(options) {
// initI18n();
},
// 入口组件不需要实现 render 方法,即使实现了也会被 taro 所覆盖
});
App.use(i18n, local);

export default App;

### 期望的结果是什么?

按需加载正常

### 实际的结果是什么?

无法按需加载,甚至在app.ts里直接引用都会报错taro.defineComponent is not a function

### 环境信息

Taro CLI 4.0.7 environment info:
System:
OS: Windows 10 10.0.19045
Binaries:
Node: 20.16.0 - E:\tools\nodejs\node.EXE
Yarn: 1.22.22 - E:\tools\nodejs\node_global\yarn.CMD
npm: 10.8.1 - E:\tools\nodejs\npm.CMD
npmPackages:
@tarojs/cli: 4.0.7 => 4.0.7
@tarojs/components: 4.0.7 => 4.0.7
@tarojs/helper: 4.0.7 => 4.0.7
@tarojs/plugin-framework-vue3: 4.0.7 => 4.0.7
@tarojs/plugin-html: 4.0.7 => 4.0.7
@tarojs/plugin-platform-alipay: 4.0.7 => 4.0.7
@tarojs/plugin-platform-h5: 4.0.7 => 4.0.7
@tarojs/plugin-platform-harmony-hybrid: 4.0.7 => 4.0.7
@tarojs/plugin-platform-jd: 4.0.7 => 4.0.7
@tarojs/plugin-platform-qq: 4.0.7 => 4.0.7
@tarojs/plugin-platform-swan: 4.0.7 => 4.0.7
@tarojs/plugin-platform-tt: 4.0.7 => 4.0.7
@tarojs/plugin-platform-weapp: 4.0.7 => 4.0.7
@tarojs/runtime: 4.0.7 => 4.0.7
@tarojs/shared: 4.0.7 => 4.0.7
@tarojs/taro: 4.0.7 => 4.0.7
@tarojs/vite-runner: 4.0.7 => 4.0.7
babel-preset-taro: 4.0.7 => 4.0.7
eslint-config-taro: 4.0.7 => 4.0.7
taro-ui: 3.3.0 => 3.3.0

### 其他补充信息

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.