When I package the service, the method getcurrentinstance () in the constructor returns null
Vue version
3.2.37
Link to minimal reproduction
Steps to reproduce
Get getcurrentinstance() in a class constructor. If it is not packaged, it can return a normal value. When it is packaged with vue-cli-service, it returns null
//package.json
{
"scripts": {
"serve": "vue-cli-service serve",
"serve-lint": "vue-cli-service lint --fix --ext .js,.vue",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"build": "yarn build:clean && yarn build:lib && yarn build:esm-bundle && rimraf lib/demo.html",
"build:clean": "rimraf lib",
"build:lib": "vue-cli-service build --target lib --name index --dest lib packages/index.ts",
"build:esm-bundle": "rollup --config ./build/rollup.config.js",
"lint": "eslint --fix --ext .js,.vue examples"
}
}
//rollup.config.js
// import vue from 'rollup-plugin-vue'
import { nodeResolve } from "@rollup/plugin-node-resolve";
import path from "path";
// import commonjs from '@rollup/plugin-commonjs'
import { terser } from "rollup-plugin-terser";
import typescript from "rollup-plugin-typescript2";
import pkg from "../package.json";
const deps = Object.keys(pkg.dependencies);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const vue = require("rollup-plugin-vue");
export default [
{
input: path.resolve(__dirname, "../packages/index.ts"),
output: [
{
format: "es",
file: pkg.module,
},
],
plugins: [
terser(),
nodeResolve(),
// commonjs(),
vue({
target: "browser",
css: false,
exposeFilename: false,
}),
typescript({
tsconfigOverride: {
compilerOptions: {
declaration: true,
},
include: ["packages/**/*", "typings/shims-vue.d.ts"],
exclude: ["node_modules", "packages/**/__tests__/*"],
},
abortOnError: false,
}),
],
external(id) {
return /^vue/.test(id) || deps.some((k) => new RegExp("^" + k).test(id));
},
},
];
What is expected?
return current instense
What is actually happening?
return null when packaged
System Info
No response
Any additional comments?
No response
There is an SFC Example and repo, hope this can help you.
Please explain what you mean by
"When it is packaged with vue-cli-service, it returns null".
How exactly do you use it when it's packaged? do you link it locally? Then you likely have the common problem of having two distinct copies of Vue included in the app - one from the lib's /node_modules
, one from the app's. And no, the fact that you externalized the dependency has nothing to do with it. It's about package resolving when the app consumes the library code.