Subscribe on changes!

When I package the service, the method getcurrentinstance () in the constructor returns null

avatar
Jun 23rd 2022

Vue version

3.2.37

Link to minimal reproduction

https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlZiB9IGZyb20gJ3Z1ZSdcbmltcG9ydCBGaWxlVXBsb2FkSGVscGVyIGZyb20gJ3Rlc3QnXG5cbmNvbnN0IGZpbGUgPSBuZXcgRmlsZVVwbG9hZEhlbHBlcigpO1xuY29uc3QgbXNnID0gcmVmKCdIZWxsbyBXb3JsZCEnKVxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbiAgPGgxPnt7IG1zZyB9fTwvaDE+XG4gIDxpbnB1dCB2LW1vZGVsPVwibXNnXCI+XG48L3RlbXBsYXRlPiIsImltcG9ydC1tYXAuanNvbiI6IntcbiAgXCJpbXBvcnRzXCI6IHtcbiAgICBcInZ1ZVwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy92dWUucnVudGltZS5lc20tYnJvd3Nlci5qc1wiLFxuICAgIFwidnVlL3NlcnZlci1yZW5kZXJlclwiOiBcImh0dHBzOi8vc2ZjLnZ1ZWpzLm9yZy9zZXJ2ZXItcmVuZGVyZXIuZXNtLWJyb3dzZXIuanNcIlxuICB9XG59IiwidGVzdC50cyI6ImltcG9ydCB7IENvbXBvbmVudEludGVybmFsSW5zdGFuY2UsIGdldEN1cnJlbnRJbnN0YW5jZSB9IGZyb20gXCJ2dWVcIjtcblxuZXhwb3J0IGRlZmF1bHQgY2xhc3MgRmlsZVVwbG9hZEhlbHBlciB7XG4gICAgY29uc3RydWN0b3IoKSB7XG4gICAgICAgIGNvbnN0IHtcbiAgICAgICAgICAgIGFwcENvbnRleHQ6IHtcbiAgICAgICAgICAgICAgICBjb25maWc6IHsgZ2xvYmFsUHJvcGVydGllcyB9LFxuICAgICAgICAgICAgfSxcbiAgICAgICAgfSA9IGdldEN1cnJlbnRJbnN0YW5jZSgpIGFzIENvbXBvbmVudEludGVybmFsSW5zdGFuY2U7Ly9yZXR1cm4gbnVsbCB3aGVuIHBhY2thZ2VkXG5cblxuICAgIH1cblxufSJ9

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

avatar
Jun 23rd 2022

There is an SFC Example and repo, hope this can help you.

avatar
Jun 23rd 2022

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.

avatar
Jul 4th 2022

Closing this issue for inactivity. If you an provide further information, and assuming my last comment doesn't explain the issue you are seeing, we can re-open it.