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

Delegate types generation to extension package #162

Merged
merged 5 commits into from
Sep 2, 2024
Merged

Conversation

cezaraugusto
Copy link
Member

@cezaraugusto cezaraugusto commented Sep 1, 2024

This adds support to pnpm. Fix #147.

How to test

Run the create command via pnpx, then the `dev1 command.

pnpx extension@latest create my-extension
cd my-extension
pnpm dev

cc @karlhorky this is in v2.0.0-alpha.8 already. Feel free to test/review and let me know if this doesn't work somehow.

Copy link

codecov bot commented Sep 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

@cezaraugusto cezaraugusto force-pushed the pnpm-support branch 4 times, most recently from 3d94d37 to c2c4fa5 Compare September 1, 2024 14:36
@cezaraugusto cezaraugusto marked this pull request as ready for review September 1, 2024 15:26
@cezaraugusto cezaraugusto self-assigned this Sep 1, 2024
@cezaraugusto cezaraugusto force-pushed the pnpm-support branch 2 times, most recently from 1371507 to 1bfdfc7 Compare September 1, 2024 23:16
Copy link
Member

@OSpoon OSpoon left a comment

Choose a reason for hiding this comment

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

Seems to be working well.

Copy link

pkg-pr-new bot commented Sep 2, 2024

Open in Stackblitz

yarn add https://pkg.pr.new/extension-js/extension.js/extension@162.tgz
yarn add https://pkg.pr.new/extension-js/extension.js/extension-create@162.tgz
yarn add https://pkg.pr.new/extension-js/extension.js/extension-develop@162.tgz

commit: bd81814

@cezaraugusto cezaraugusto merged commit 166254d into main Sep 2, 2024
14 checks passed
@cezaraugusto cezaraugusto deleted the pnpm-support branch September 2, 2024 11:57
@karlhorky
Copy link

karlhorky commented Sep 2, 2024

I tried out extension@2.0.0-alpha.8 with --template content-react and there are 3 problems:

  1. Corepack is setting packageManager to Yarn v1 for some reason, which will cause Yarn v1 to be used for installing packages, and also throw an error out of the box when running pnpm dev:

    $ pnpx extension@latest create my-extension --template content-react
    
    Packages: +819
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Progress: resolved 828, reused 796, downloaded 23, added 819, done
    Library/Caches/pnpm/dlx/nnuvrqu4ldr24declcbxs25maq/191b37c0948-8e38/node_modules/.pnpm/core-js-pure@3.38.1/node_modules/core-js-pure: Running postinstall script, done in 44ms
    Library/Caches/pnpm/dlx/nnuvrqu4ldr24declcbxs25maq/191b37c0948-8e38/node_modules/.pnpm/@swc+core@1.7.22/node_modules/@swc/core: Running postinstall script, done in 42ms
    🐣 - Starting a new browser extension named my-extension...
    🤝 - Ensuring my-extension folder exists...
    🤞 - Checking if destination path is writeable...
    🔎 - Scanning for potential conflicting files...
    🧰 - Installing my-extension...
    📝 - Writing package.json metadata...
    🛠  - Installing dependencies... (takes a moment)
    📄 - Writing README.md metadata...
    📜 - Writing manifest.json metadata...
    🌲 - Initializing git repository for my-extension...
    🙈 - Writing .gitignore lines...
    🧩 - Success! Extension my-extension created.
    
    Now cd my-extension and yarn dev to open a new browser instance
    with your extension installed, loaded, and enabled for development.
    
    You are ready. Time to hack on your extension!
    
    $ cd my-extension
    $ pnpm dev
    
    /opt/homebrew/Cellar/node@20/20.15.0/lib/node_modules/corepack/dist/lib/corepack.cjs:23512
                  throw new UsageError(`This project is configured to use ${result.spec.name} because ${result.target} has a "packageManager" field`);
                        ^
    UsageError: This project is configured to use yarn because /Users/k/p/my-extension/package.json has a "packageManager" field
        at Engine.findProjectSpec (/opt/homebrew/Cellar/node@20/20.15.0/lib/node_modules/corepack/dist/lib/corepack.cjs:23512:21)
        at async Engine.executePackageManagerRequest (/opt/homebrew/Cellar/node@20/20.15.0/lib/node_modules/corepack/dist/lib/corepack.cjs:23542:24)
        at async Object.runMain (/opt/homebrew/Cellar/node@20/20.15.0/lib/node_modules/corepack/dist/lib/corepack.cjs:24235:5) {
      clipanion: { type: 'usage' }
    }
    
    Node.js v20.15.0
    
    $ cat package.json
    {
    
    ...
    
      "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
    }
    
    $ ls -al
    total 584
    drwxr-xr-x    9 k  staff     288 Sep  2 18:08 .
    drwxr-xr-x   79 k  staff    2528 Sep  2 18:05 ..
    ...
    -rw-r--r--    1 k  staff  300263 Sep  2 18:08 yarn.lock
  2. If I fix the packageManager in package.json using rm and yq and pnpm i, then run pnpm tsc (to type check all files), then I receive errors (probably "noEmit": true shouuld be set in tsconfig.json):

    $ rm yarn.lock
    
    $ yq --inplace 'del(.packageManager)' package.json
    
    $ pnpm i
    ! The local project doesn't define a 'packageManager' field. Corepack will now add one referencing pnpm@9.9.0+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1.
    ! For more details about this field, consult the documentation at https://nodejs.org/api/packages.html#packagemanager
    
     WARN  Moving @types/react-dom that was installed by a different package manager to "node_modules/.ignored"
     WARN  Moving @types/react that was installed by a different package manager to "node_modules/.ignored"
     WARN  Moving typescript that was installed by a different package manager to "node_modules/.ignored"
     WARN  Moving extension that was installed by a different package manager to "node_modules/.ignored"
     WARN  Moving react that was installed by a different package manager to "node_modules/.ignored"
     WARN  2 other warnings
    Packages: +827
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Progress: resolved 836, reused 827, downloaded 0, added 827, done
    
    dependencies:
    + react 18.3.1
    + react-dom 18.3.1
    + tailwindcss 3.4.10
    
    devDependencies:
    + @types/react 18.3.5
    + @types/react-dom 18.3.0
    + extension 2.0.0-alpha.8
    + typescript 5.3.3 (5.5.4 is available)
    
    Done in 7.9s
    
    $ pnpm tsc
    error TS5055: Cannot write file '/Users/k/p/my-extension/postcss.config.js' because it would overwrite input file.
    
    error TS5055: Cannot write file '/Users/k/p/my-extension/tailwind.config.js' because it would overwrite input file.
    
    
    Found 2 errors
  3. If I fix the noEmit in tsconfig.json using yq, then tsc passes... but adding a usage of the chrome global (from @types/chrome) to background.ts then fails:

    $ yq --inplace '.compilerOptions.noEmit = true' tsconfig.json
    $ pnpm tsc
    $ echo "chrome.action.onClicked.addListener(() => {})" >> background.ts
    $ pnpm tsc                                                                
    background.ts:2:1 - error TS2304: Cannot find name 'chrome'.
    
    2 chrome.action.onClicked.addListener(() => {});
      ~~~~~~
    
    Found 1 error in background.ts:2

    Seems like @types/chrome is not in the top-level node_modules:

    Everything that the extension types use should be direct dependencies of the user's top-level package.json, that applies to both @types/node and @types/chrome:

    /// <reference types="node" />
    
    /// <reference types="chrome" />

    Screenshot 2024-09-02 at 19 10 31

    With npm and pnpm you can mark these as peerDependencies, because both of these package managers install peerDependencies by default. With Yarn v2+ IIRC there's something else going on, but maybe peerDependencies is ok for it too...

So a few things still open for pnpm + tsc support, but getting closer!

@cezaraugusto
Copy link
Member Author

@karlhorky thanks for the follow-up! Hopefully fixed in #165

cezaraugusto added a commit that referenced this pull request Sep 5, 2024
* Ensure all extension types use top-level type dependencies

* Ensure pnpm is properly handled

* Completely replace Yarn with pnpm

* Update link to extension types
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.

Error TS2307: Cannot find module after scaffolding with pnpm
3 participants