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

Undefined imports in production build with shared components #4083

Closed
6 tasks done
cuiqui opened this issue Jul 2, 2021 · 13 comments
Closed
6 tasks done

Undefined imports in production build with shared components #4083

cuiqui opened this issue Jul 2, 2021 · 13 comments
Labels
has workaround inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@cuiqui
Copy link

cuiqui commented Jul 2, 2021

Describe the bug

When building for production, several pages import from FontAwesome (import { faXXX } from '@fortawesome/sub/faXXX.js'). Some of those imports are shared between a lot of components. These shared imports in dist/assets are seen, after running npm run build, as a separate chunk of the form faXXX.hash.js, apparently empty. Example from the repo to reproduce issue:

$ cat faPlusCircle.ef7d87a6.js
var faPlusCircle = {};
export { faPlusCircle as f };
//# sourceMappingURL=faPlusCircle.ef7d87a6.js.map

Which produces following error in some pages:

Uncaught (in promise) TypeError: Cannot read property 'icon' of undefined
    at Object.$$self.$$.update (FaIcon.svelte:8)
    at init$1 (index.mjs:1671)
    at new FaIcon (FaIcon.svelte:12)
    at create_fragment (two.e78d4eea.js:8)
    at init$1 (index.mjs:1675)
    at new Two (two.svelte:8)
    at Array.create_default_slot (Route.svelte:114)
    at create_slot (index.mjs:61)
    at create_fragment$3 (index.js:174)
    at init$1 (index.mjs:1675)

Pages in which it does not result in error are those that concatenated the import code in its chunk as such:

import { f as faMinusCircle } from "./faMinusCircle.3d152943.js";
import { F as FaIcon } from "./FaIcon.2537ee2b.js";
(function(exports) {
  Object.defineProperty(exports, "__esModule", { value: true });
  var prefix = "fas";
  var iconName = "minus-circle";
  var width = 512;
  var height = 512;
  var ligatures = [];
  var unicode = "f056";
  var svgPathData = "M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8zM124 296c-6.6 0-12-5.4-12-12v-56c0-6.6 5.4-12 12-12h264c6.6 0 12 5.4 12 12v56c0 6.6-5.4 12-12 12H124z";
  exports.definition = {
    prefix,
    iconName,
    icon: [
      width,
      height,
      ligatures,
      unicode,
      svgPathData
    ]
  };
  exports.faMinusCircle = exports.definition;
  exports.prefix = prefix;
  exports.iconName = iconName;
  exports.width = width;
  exports.height = height;
  exports.ligatures = ligatures;
  exports.unicode = unicode;
  exports.svgPathData = svgPathData;
})(faMinusCircle);

More information: https://github.com/cuiqui/vite-issue-repo#weird-bundling

Reproduction

https://github.com/cuiqui/vite-issue-repo

System Info

System:
    OS: Linux 4.15 Ubuntu 18.04.5 LTS (Bionic Beaver)
    CPU: (4) x64 Intel(R) Core(TM) i5-4300M CPU @ 2.60GHz
    Memory: 160.38 MB / 11.43 GB
    Container: Yes
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 14.16.0 - /usr/bin/node
    npm: 6.14.11 - /usr/bin/npm
  Browsers:
    Chrome: 89.0.4389.114
    Firefox: 87.0
  npmPackages:
    vite: ^2.3.8 => 2.3.8

Used Package Manager

npm

Logs

No response

Validations

@patak-dev
Copy link
Member

As a workaround to this issue, you can use

  import { faMinusCircle } from "@fortawesome/free-solid-svg-icons";
  import { faPlusCircle } from "@fortawesome/free-solid-svg-icons";

instead of

  import { faMinusCircle } from "@fortawesome/free-solid-svg-icons/faMinusCircle";
  import { faPlusCircle } from "@fortawesome/free-solid-svg-icons/faPlusCircle";

@patak-dev patak-dev added bug p3-minor-bug An edge case that only affects very specific usage (priority) has workaround and removed pending triage labels Aug 1, 2021
@patak-dev
Copy link
Member

@cuiqui it would be useful that you try to create a reproduction only using vanilla, so it is easier for others to check out.

@cuiqui
Copy link
Author

cuiqui commented Aug 9, 2021

Thanks for the reply @patak-js, I ended up building the icon library instead of importing an icon in each component:

import { library } from "@fortawesome/fontawesome-svg-core";
import { faMinusCircle, faPlusCircle } from "@fortawesome/free-solid-svg-icons";

library.add(faMinusCircle, faPlusCircle);

Which in essence is your workaround. I'll try to make some time to create a vanilla reproduction, but I'm sorry I can't promise anything right now.

@bluwy
Copy link
Member

bluwy commented Mar 12, 2022

This still seems to happen in Vite 2.8.6

@edisdev
Copy link

edisdev commented Apr 12, 2022

Hi ✋🏻 I have a the same problem.

import { DeviceUUID } from 'device-uuid'

DeviceUUID is undefined for after build. How can i solve this issue?

@pietrofxq
Copy link

Also facing the same problem with vite 3.0.9. Trying to import a class with static members from a private shared package, import is undefined in production build.

@pietrofxq
Copy link

pietrofxq commented Sep 1, 2022

I have code similar to this:

import {Session} from '@company/helpers'

export const mockUser = () => {
  Session.createEmptySession()
}

Session class:

export class Session {
  static createEmptySession() {
     //...
  }
}

I found a weird workaround: adding console.log(Session) in the file fixes it. So it looks like a tree shaking problem

@awebartisan
Copy link

Facing this problem with Shopify Polaris library and Vite.

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

import { Provider } from '@shopify/app-bridge-react';

Provider component gets undefined in the production build.

@zbigniewstefaniuk
Copy link

zbigniewstefaniuk commented Nov 23, 2022

same issue with import { SelfieSegmentation, Results } from '@mediapipe/selfie_segmentation';
after build it's undefined, on dev server it's working perfectly fine

EDIT: here is reproduction code https://github.com/zbigniewstefaniuk/vite-lib-on-build-resolving-issue
I've just scaffolded new project using yarn create vite with vue + typescirpt template, only installed this package @mediapipe/selfie_segmentation' and on dev server it's working however, prod import is undefined. Values are printed to the console as well as printed to the screen in <template />

@bluwy bluwy added the inconsistency Inconsistency between dev & build label Dec 29, 2022
@eslym
Copy link

eslym commented Feb 1, 2023

i am facing same issue while having 2 entries, both have imported faMultiply from @fortawesome/free-solid-svg-icons/faMultiply somewhere,

i read the compiled code and found something interesting, 3 js files generated ex: entry1.js, entry2.js, shared.js

the actual declaration of faMultiply is in the shared.js since both entry refer to it, how ever the actual definitions of faMultiply is only exists in entry2.js, so when entry1.js is loaded, the actual definitions of faMultiply will not be there.

i cant provide the actual code since it is a closed source project, but the below code is roughly how the compiled code will looks like

// shared.js
var faMultiply = {};
export { faMultiply };
// entry1.js
import { faMultiply } from "./shared.js";
console.log(faMultiply.faMultiply); // <= undefiend since faMultiply is just empty object in shared.js
// entry2.js
import { faMultiply } from "./shared.js";
(function(exports){
   /* actual definition is here */
})(faMultiply);
console.log(faMultiply.faMultiply); // <= the definitions of the icon is logged properly

the actual definitions is not compiled into the shared library, i hope this info is useful and i will try to see whether i can replicate it in a clean vanilla vite project or not when i am free

@spencer17x
Copy link
Contributor

Maybe you can use vite-plugin-mediapipe:https://github.com/Spencer17x/arca/tree/main/packages/vite-plugin/vite-plugin-mediapipe

@clarkf
Copy link

clarkf commented Oct 24, 2023

I believe this is fixed as of v3.0.0-alpha5, or, secifically 909cf9c from #8280. At least, a bisect using https://github.com/cuiqui/vite-issue-repo points to that commit.

I'm unable to reproduce any of the other examples as of either main or latest (4.5.0).

@zbigniewstefaniuk it seems that @mediapipe/selfie_segmentation is a commonjs module. The following seems to work as expected:

import selfieSegmentation from "@mediapipe/selfie_segmentation";
const { SelfieSegmentation, VERSION } = selfieSegmentation;

I'm not familiar enough with mediapipe to say whether or not there's a better way.

@bluwy
Copy link
Member

bluwy commented Oct 25, 2023

Thanks for triaging this. Yeah I can also see that it's fixed now. I'll close this for now.

Regarding @mediapipe/selfie_segmentation, that's a separate issue which is tracked at #10612

@bluwy bluwy closed this as completed Oct 25, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

10 participants