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

[1.0] Drop CJS build and export types first #235

Merged
merged 6 commits into from
Aug 9, 2023

Conversation

benmccann
Copy link
Contributor

@benmccann benmccann commented Aug 4, 2023

Closes #234. There were only four projects I saw using CJS and I've sent PRs to migrate three of them

Also, types must be exported first. See https://publint.dev/laravel-vite-plugin@0.7.8 for more details regarding that

@benmccann benmccann changed the title Export types first Drop CJS build and export types first Aug 4, 2023
@timacdonald
Copy link
Member

timacdonald commented Aug 7, 2023

Hey Ben,

Thank you for this.

Is Vite removing support for CJS in a major version?

I just spun up an older version of the Laravel application skeleton and confirmed that this change alone will break potentially thousands(?) of user-land Laravel applications that were created before we moved the skeleton (https://github.com/laravel/laravel) to use "type": "modules".

The Vite configuration file for the Laravel skeleton has always used ESM import / export...

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});

However, because we didn't have "type": "module" in the package.json we it was looking for the CJS build version of the plugin.

After we migrated the skeleton to use "type": "module" we continued to build the CJS version alongside the ESM version. We also build the plugin with esbuild, not Vite:

vite-plugin/package.json

Lines 38 to 39 in f89fcde

"build-plugin-cjs": "esbuild src/index.ts --platform=node --format=cjs --outfile=dist/index.cjs --define:import.meta.url=import_meta_url --inject:./import.meta.url-polyfill.js",
"build-plugin-esm": "esbuild src/index.ts --platform=node --format=esm --outfile=dist/index.mjs",

Remembering I'm not an expert on the JS build tooling, I think that Vite dropping support for CJS shouldn't impact this library directly, as we don't build the library with Vite - although it would mean we could drop CJS support, which is what your PR is aiming to do.

It would mean that any user-land application that wanted to upgrade to the version of Vite that drops CJS support would need to make sure they have "type": "module" in the projects package.json

I would love to know if that sounds correct to you.

@timacdonald timacdonald changed the title Drop CJS build and export types first [1.0] Drop CJS build and export types first Aug 7, 2023
@benmccann
Copy link
Contributor Author

I just spun up an older version of the Laravel application skeleton and confirmed that this change alone will break potentially thousands(?) of user-land Laravel applications that were created before we moved the skeleton (https://github.com/laravel/laravel) to use "type": "modules".

Did you test or are you inferring that from the code? If you tested it, can you share the steps with me so I can check it out? Can you share a link to one of these old projects with me?

Is Vite removing support for CJS in a major version?

Yes, it would be in a major version. TBD whether Vite 5 will remove it or just cause a warning, but either way we're trying to migrate as much as we can now.

However, because we didn't have "type": "module" in the package.json we it was looking for the CJS build version of the plugin.

I wouldn't expect that to be true. If you're doing an import it should look up the ESM version in the exports map unless there's a build step that's converting the config file from ESM to CJS

It would mean that any user-land application that wanted to upgrade to the version of Vite that drops CJS support would need to make sure they have "type": "module" in the projects package.json

No, they shouldn't need to go that far. They just need to use the plugin from an .mjs config file. Converting just the one file is generally much easier than converting the whole project. I only found four projects that even needed to be updated: #234 (comment)

@timacdonald
Copy link
Member

Did you test or are you inferring that from the code? If you tested it, can you share the steps with me so I can check it out? Can you share a link to one of these old projects with me?

Here is a bash script that produces the problem. It downloads the skeleton before we made the "type": "modules" in the package.json the default for new projects.

This is the commit before we made the change: https://github.com/laravel/laravel/tree/d14bdeeb6db121cc7d181ce5497211612ce4bc10 if you don't feel comfortable running the wget unzip etc.

You will need to have the laravel-vite-plugin locally built with your branch. There is an npm run build script. You will then need to have it linked with npm link.

wget --output-document laravel-skeleton-pre-modules.zip https://github.com/laravel/laravel/archive/d14bdeeb6db121cc7d181ce5497211612ce4bc10.zip
unzip ./laravel-skeleton-pre-modules.zip -d ./
cd laravel-d14bdeeb6db121cc7d181ce5497211612ce4bc10
npm install
npm link laravel-vite-plugin
npm run build

Yes, it would be in a major version. TBD whether Vite 5 will remove it or just cause a warning, but either way we're trying to migrate as much as we can now.

Good to hear there will be a major.

I wouldn't expect that to be true. If you're doing an import it should look up the ESM version in the exports map unless there's a build step that's converting the config file from ESM to CJS

We just have the vite.config.js as plain ESM without anything fancy happening.

This is the error...

Screen Shot 2023-08-07 at 2 26 07 pm

This error is resolved if I add "type": "module" into the projects package.json and the build is successful.

@benmccann
Copy link
Contributor Author

Hmm. The skeleton project doesn't look valid to me because it has ESM code in a .js file without "type": "module". When you don't have "type": "module" any .js file will be interpreted as CJS. The fix is either to add "type": "module" as you noted or to rename the file to use the .mjs extension, which is generally the much easier thing to do.

I'll let you figure out how to proceed and take it from here. One suggestion would be to push a release that does a check for export default in vite.config.js and the absence of "type": "module" in package.json. If those conditions are both true then you could suggest to the user that they need to rename their config file to vite.config.mjs or better yet take the long-term but more expensive fix of adding "type": "module" and converting their whole project to ESM.

@timacdonald
Copy link
Member

Something to keep in mind is that any change to the Laravel skeleton will not roll out to existing applications, as that code all becomes user-land code.

I think this will actually be alright, though - although not ideal.

When Vite 5 is released we would tag version a major version of the plugin.

We would note in our upgrade guide that Vite 5 is now required and that "type": "module" should be placed in the package.json file.

So I think we can probably move ahead with this as it is. I'm going to discuss it with the team today internally.

Going to mark this one as a draft until we know how we want to proceed.

Thanks, Ben!

@timacdonald timacdonald marked this pull request as draft August 7, 2023 22:58
@timacdonald timacdonald changed the base branch from main to 1.x August 8, 2023 04:20
@timacdonald timacdonald marked this pull request as ready for review August 9, 2023 03:06
Copy link
Member

@timacdonald timacdonald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to get this one moving into our yet to be released 1.x branch.

This 1.x branch won't be released / tagged until Vite 5 comes out.

We will document the upgrade path in the upgrade guide once Vite 5 is released and we have an understanding of any additional things we need to document for Laravel projects.

Thanks again, Ben!

@taylorotwell taylorotwell merged commit d3c3869 into laravel:1.x Aug 9, 2023
@benmccann benmccann deleted the main-1 branch August 9, 2023 17:48
tisnamuliarta referenced this pull request in tisnamuliarta/laravel-shadcn Mar 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [laravel-vite-plugin](https://togithub.com/laravel/vite-plugin) |
[`^0.8.0` ->
`^1.0.0`](https://renovatebot.com/diffs/npm/laravel-vite-plugin/0.7.8/1.0.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>laravel/vite-plugin (laravel-vite-plugin)</summary>

###
[`v1.0.2`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v102---2024-02-28)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.1...v1.0.2)

- \[1.x] Fix HMR issue when `resources/lang` directory doesn't exist and
a symlink is present in the root directory by
[@&#8203;jessarcher](https://togithub.com/jessarcher) in
[https://github.com/laravel/vite-plugin/pull/285](https://togithub.com/laravel/vite-plugin/pull/285)

###
[`v1.0.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v101---2023-12-27)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.0...v1.0.1)

- \[1.x] Simpler conditional by
[@&#8203;Jubeki](https://togithub.com/Jubeki) in
[https://github.com/laravel/vite-plugin/pull/273](https://togithub.com/laravel/vite-plugin/pull/273)
- \[1.x] Account for imported CSS files while cleaning by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/275](https://togithub.com/laravel/vite-plugin/pull/275)
- \[1.x] Fix exit error messages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/276](https://togithub.com/laravel/vite-plugin/pull/276)

###
[`v1.0.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v100---2023-12-19)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.1...v1.0.0)

- \[1.0] Drop CJS build and export types first by
[@&#8203;benmccann](https://togithub.com/benmccann) in
[https://github.com/laravel/vite-plugin/pull/235](https://togithub.com/laravel/vite-plugin/pull/235)
- \[1.x] Introduce `clean-orphaned-assets` binary by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/251](https://togithub.com/laravel/vite-plugin/pull/251)
- \[0.8.x] Respect vite server.origin in viteDevServerUrl by
[@&#8203;nurdism](https://togithub.com/nurdism) in
[https://github.com/laravel/vite-plugin/pull/255](https://togithub.com/laravel/vite-plugin/pull/255)
- \[1.x] Vite 5 by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/269](https://togithub.com/laravel/vite-plugin/pull/269)
- \[0.8.x] Fallback pages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/271](https://togithub.com/laravel/vite-plugin/pull/271)
- \[1.x] Auto detect Valet / Herd TLS certificates by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/180](https://togithub.com/laravel/vite-plugin/pull/180)

###
[`v0.8.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v081---2023-09-26)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.0...v0.8.1)

- \[0.8] Fix issue with `0.0.0.0` network resolution by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/241](https://togithub.com/laravel/vite-plugin/pull/241)
- Upgrade vitest by
[@&#8203;sapphi-red](https://togithub.com/sapphi-red) in
[https://github.com/laravel/vite-plugin/pull/246](https://togithub.com/laravel/vite-plugin/pull/246)

###
[`v0.8.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v080---2023-08-08)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.7.8...v0.8.0)

- fix: compile error following upgrade.md's vite to mix guide by
[@&#8203;AshboDev](https://togithub.com/AshboDev) in
[https://github.com/laravel/vite-plugin/pull/231](https://togithub.com/laravel/vite-plugin/pull/231)
- Support Laravel Herd by
[@&#8203;claudiodekker](https://togithub.com/claudiodekker) in
[https://github.com/laravel/vite-plugin/pull/233](https://togithub.com/laravel/vite-plugin/pull/233)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tisnamuliarta/laravel-shadcn).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjIzMC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
tisnamuliarta referenced this pull request in tisnamuliarta/laravel-shadcn Mar 22, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [laravel-vite-plugin](https://togithub.com/laravel/vite-plugin) |
[`^0.7.5` ->
`^1.0.0`](https://renovatebot.com/diffs/npm/laravel-vite-plugin/0.7.8/1.0.2)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/laravel-vite-plugin/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/laravel-vite-plugin/0.7.8/1.0.2?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>laravel/vite-plugin (laravel-vite-plugin)</summary>

###
[`v1.0.2`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v102---2024-02-28)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.1...v1.0.2)

- \[1.x] Fix HMR issue when `resources/lang` directory doesn't exist and
a symlink is present in the root directory by
[@&#8203;jessarcher](https://togithub.com/jessarcher) in
[https://github.com/laravel/vite-plugin/pull/285](https://togithub.com/laravel/vite-plugin/pull/285)

###
[`v1.0.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v101---2023-12-27)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v1.0.0...v1.0.1)

- \[1.x] Simpler conditional by
[@&#8203;Jubeki](https://togithub.com/Jubeki) in
[https://github.com/laravel/vite-plugin/pull/273](https://togithub.com/laravel/vite-plugin/pull/273)
- \[1.x] Account for imported CSS files while cleaning by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/275](https://togithub.com/laravel/vite-plugin/pull/275)
- \[1.x] Fix exit error messages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/276](https://togithub.com/laravel/vite-plugin/pull/276)

###
[`v1.0.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v100---2023-12-19)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.1...v1.0.0)

- \[1.0] Drop CJS build and export types first by
[@&#8203;benmccann](https://togithub.com/benmccann) in
[https://github.com/laravel/vite-plugin/pull/235](https://togithub.com/laravel/vite-plugin/pull/235)
- \[1.x] Introduce `clean-orphaned-assets` binary by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/251](https://togithub.com/laravel/vite-plugin/pull/251)
- \[0.8.x] Respect vite server.origin in viteDevServerUrl by
[@&#8203;nurdism](https://togithub.com/nurdism) in
[https://github.com/laravel/vite-plugin/pull/255](https://togithub.com/laravel/vite-plugin/pull/255)
- \[1.x] Vite 5 by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/269](https://togithub.com/laravel/vite-plugin/pull/269)
- \[0.8.x] Fallback pages by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/271](https://togithub.com/laravel/vite-plugin/pull/271)
- \[1.x] Auto detect Valet / Herd TLS certificates by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/180](https://togithub.com/laravel/vite-plugin/pull/180)

###
[`v0.8.1`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v081---2023-09-26)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.8.0...v0.8.1)

- \[0.8] Fix issue with `0.0.0.0` network resolution by
[@&#8203;timacdonald](https://togithub.com/timacdonald) in
[https://github.com/laravel/vite-plugin/pull/241](https://togithub.com/laravel/vite-plugin/pull/241)
- Upgrade vitest by
[@&#8203;sapphi-red](https://togithub.com/sapphi-red) in
[https://github.com/laravel/vite-plugin/pull/246](https://togithub.com/laravel/vite-plugin/pull/246)

###
[`v0.8.0`](https://togithub.com/laravel/vite-plugin/blob/HEAD/CHANGELOG.md#v080---2023-08-08)

[Compare
Source](https://togithub.com/laravel/vite-plugin/compare/v0.7.8...v0.8.0)

- fix: compile error following upgrade.md's vite to mix guide by
[@&#8203;AshboDev](https://togithub.com/AshboDev) in
[https://github.com/laravel/vite-plugin/pull/231](https://togithub.com/laravel/vite-plugin/pull/231)
- Support Laravel Herd by
[@&#8203;claudiodekker](https://togithub.com/claudiodekker) in
[https://github.com/laravel/vite-plugin/pull/233](https://togithub.com/laravel/vite-plugin/pull/233)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/tisnamuliarta/laravel-shadcn).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yMzguMSIsInVwZGF0ZWRJblZlciI6IjM3LjIzOC4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dropping CJS support
3 participants