Skip to content

bug: Building for Vite doesn't work even with enableImportInjection: true #5637

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

Closed
3 tasks done
thebrandonlucas opened this issue Apr 8, 2024 · 6 comments
Closed
3 tasks done
Assignees
Labels

Comments

@thebrandonlucas
Copy link

thebrandonlucas commented Apr 8, 2024

Prerequisites

Stencil Version

4.7.0

Current Behavior

Importing my web component in a Vite project (sveltekit, in this case) gives me the following error:

file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/entries/pages/_page.svelte.js:2
import { defineCustomElements } from "bitcoin-qr/loader/index.js";
         ^^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'defineCustomElements' not found. The requested module 'bitcoin-qr/loader/index.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'bitcoin-qr/loader/index.js';
const { defineCustomElements } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async Module.component (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/nodes/2.js:5:59)
    at async Promise.all (index 1)
    at async render_response (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/index.js:1355:21)
    at async render_page (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/index.js:2218:12)
    at async resolve2 (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/index.js:2839:24)
    at async respond (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/.svelte-kit/output/server/index.js:2730:22)
    at async visit (file:///Users/username/bitcoin-qr/examples/sveltekit-kitchen-sink/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:202:20)

node:internal/event_target:1100

After searching around and seeing that Vite only works with ESM modules, I found a couple issues (#3961 and #4598) related that suggest setting enableImportInjection: true in stencil.config.ts. I set this, rebuilt and packed the stencil project, imported into my Vite project, and got the same error. Notably, it works fine in the dev build/server, just not production.

Expected Behavior

The project should build with no errors and the element should render in both the production and dev servers.

System Info

`npx stencil info`

      System: node 20.11.1
    Platform: darwin (22.5.0)
   CPU Model: Apple M1 Pro (8 cpus)
    Compiler: /Users/brandonlucas/src/lightning/bitcoin-qr/node_modules/@stencil/core/compiler/stencil.js
       Build: 1710782619
     Stencil: 4.13.0 🚞
  TypeScript: 5.4.2
      Rollup: 2.56.3
      Parse5: 7.1.2
      jQuery: 4.0.0-pre
      Terser: 5.29.2

Steps to Reproduce

To reproduce:

  1. Create a sveltekit vite project:
npm create svelte@latest vite-build-test
cd vite-build-test
npm install
npm run dev -- --open
  1. Create and build a stencil project with enableImportInjection: true.
import { Config } from '@stencil/core';

export const config: Config = {
  namespace: 'bitcoin-qr',
  outputTargets: [
    {
      type: 'dist',
      esmLoaderPath: '../loader',
    },
  ],
...
  extras: {
    enableImportInjection: true,
  },
};
  1. npm build then npm pack the Stencil project, unpack with tar -xvf <package>. Install it in the Vite project via npm install --save <path-to-package>
  2. Import defineCustomElements into the app and call it:
<script lang="ts">
	import { defineCustomElements } from '<stencil-project>/loader';

	defineCustomElements();
</script>
  1. Run npm build then npm preview to build & start the production server.

I created a minimum reproducible Vite example here.

Code Reproduction URL

https://github.com/thebrandonlucas/bitcoin-qr/

Additional Information

No response

@ionitron-bot ionitron-bot bot added the triage label Apr 8, 2024
@thebrandonlucas
Copy link
Author

thebrandonlucas commented Apr 8, 2024

UPDATE: I was able to get this to work in the minimum reproducible example by changing "main": "dist/index.cjs.js" to `"main": "dist/index.js". However, I get a console warning when doing so:

[ WARN  ]  Package Json: package.json:5:3
           package.json "main" property is set to "dist/index.js". It's recommended to set the "main" property to:
           dist/index.cjs.js
      ...
      L5:  "main": "dist/index.js",
      L6:  "module": "dist/index.js",

Howver, I'm still unable to get building to work in the original example repo I'm doing this in here, despite installing the same version of the package above. It's throwing the same error on build: SyntaxError: Named export 'defineCustomElements' not found. The requested module 'bitcoin-qr/loader/index.js' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:, despite the fact that I explicitly changed it to NOT be using "main" as commonJS

@rwaskiewicz rwaskiewicz self-assigned this Apr 8, 2024
@rwaskiewicz
Copy link
Contributor

Hey @thebrandonlucas 👋

I'm not seeing a Stencil project in the minimal reproduction URL - https://github.com/thebrandonlucas/vite-build-test. Can you do me a favor and update that when you get a chance so that the team and I can take a closer look? Thanks!

@rwaskiewicz rwaskiewicz added the ionitron: needs reproduction This PR or Issue does not have a reproduction case URL label Apr 8, 2024
@rwaskiewicz rwaskiewicz removed their assignment Apr 8, 2024
Copy link

ionitron-bot bot commented Apr 8, 2024

Thanks for the issue! This issue has been labeled as needs reproduction. This label is added to issues that need a code reproduction.

Please reproduce this issue in an Stencil starter component library and provide a way for us to access it (GitHub repo, StackBlitz, etc). Without a reliable code reproduction, it is unlikely we will be able to resolve the issue, leading to it being closed.

If you have already provided a code snippet and are seeing this message, it is likely that the code snippet was not enough for our team to reproduce the issue.

For a guide on how to create a good reproduction, see our Contributing Guide.

@ionitron-bot ionitron-bot bot removed the triage label Apr 8, 2024
@thebrandonlucas
Copy link
Author

thebrandonlucas commented Apr 9, 2024

@rwaskiewicz My apologies, I was using a locally built package and must've removed it.

The bitcoin-qr package I'm trying to build with has been added now.

@rwaskiewicz rwaskiewicz added triage and removed ionitron: needs reproduction This PR or Issue does not have a reproduction case URL labels Apr 9, 2024
@rwaskiewicz rwaskiewicz self-assigned this Apr 9, 2024
@rwaskiewicz
Copy link
Contributor

rwaskiewicz commented Apr 10, 2024

It looks like to get Vite working in this particular instance, you'll need to add a loader export in your Stencil package's package.json. E.g.:

"exports": {
    ".": {
      "import": "./dist/my-stencil/my-stencil.esm.js",
      "require": "./dist/my-stencil/my-stencil.cjs.js"
    },
    "./my-component": {
      "import": "./dist/components/my-component.js",
      "types": "./dist/components/my-component.d.ts"
    },
+    "./loader": {
+      "import": "./loader/index.cjs.js",
+      "types": "./loader/index.d.ts"
+    }
  },

From there, rebuild your Stencil project, then publish/npm pack it, install the updated version in your Svelte project, then everything should work without additional changes 🙂

I've closed this issue out, feel free to open a new one if this doesn't work as expected.

@thebrandonlucas
Copy link
Author

Worked like a charm. Thanks for your help!

rwaskiewicz added a commit to stenciljs/component-starter that referenced this issue Apr 18, 2024
this commit adds an export mapping to the loader package to
`package.json`.

prior to this commit, users working their way through stencil's guide
for setting up the react output target would run into an error when
trying to spin up a react app with vite:

```
vite v5.2.9 building for production...
✓ 7 modules transformed.
x Build failed in 31ms
error during build:
Error: [commonjs--resolver] Missing "./loader" specifier in "stencil-library" package
     <STACK_TRACE_OMITTED>

npm ERR! Lifecycle script `build` failed with error:
npm ERR! Error: command failed
npm ERR!   in workspace: [email protected]
npm ERR!   at location: /private/tmp/react-set-asset-path/packages/my-app
```

this restores the behavior previously supported in #134,
without requiring us to rewrite the react guide

relates to: stenciljs/core#5637
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants