Skip to content

feat!: Individual exports and introduce the #imports module - #1258

Merged
aklinker1 merged 31 commits into
0.20.0-breaking-changesfrom
individual-exports
Dec 11, 2024
Merged

aklinker1 merged 31 commits into
0.20.0-breaking-changesfrom
individual-exports

Conversation

@aklinker1

@aklinker1 aklinker1 commented Dec 7, 2024 •

Copy link
Copy Markdown
Member

BREAKING CHANGE: APIs exported by wxt/client and wxt/sandbox submodules have been moved to wxt/utils/*. Each API now resides in it's own submodule.

To ease the process of importing these APIs, WXT is introducing the new #imports module. To upgrade, replace any manual imports from wxt/client or wxt/sandbox with the new #imports module:

- import { createShadowRootUi } from 'wxt/client';
- import { defineContentScript } from 'wxt/sandbox';
+ import { createShadowRootUi, defineContentScript } from '#imports';

This results in a slight decrease in bundle size due to better tree-shaking, but mainly it enables WXT to provide more environment specific utilities in the future (e.g. functions that require service worker globals).

Read more about the #imports module in the blog post.

If you use auto-imports, you may have manually imported things. Update them to import from #imports as well.

It's rare, but if you are mocking any API from wxt/client or wxt/sandbox in your unit tests, you'll need to update the mock paths to the new wxt/utils/* paths (not #imports):

- vi.mock("wxt/client", async () => {
+ vi.mock("wxt/utils/createShadowRoot", async () => {
    cosnt { vi } = await import("vitest");
    return {
      createShadowRootUi: vi.fn(),
    };
  });

To learn more about mocking APIs from the #imports module, read the Unit Testing docs.


BREAKING CHANGE: To override types in <srcDir>/app.config.ts, update your module-augmentation:

- import { defineAppConfig } from 'wxt/sandbox';
+ import { defineAppConfig } from 'wxt/utils/define-app-config';

- declare module 'wxt/sandbox' {
+ declare module 'wxt/utils/define-app-config' {
    export interface WxtAppConfig {
      theme?: 'light' | 'dark';
    }
  }

BREAKING CHANGE: wxt.config.imports type was changed from false | WxtResolvedUnimportOptions to just WxtResolvedUnimportOptions. You no longer need to check if auto-imports are enabled before modifying the config.

- if (wxt.config.imports === false) {
+ if (!wxt.config.imports) {
    // ...
  } else {
    // ...
  }

This addresses the issue discovered in #1210 (comment). Instead of importing the entire wxt/client module, it will now auto-import the individual API, useAppConfig, from wxt/utils/app-config instead of wxt/client. This PR also adds individual exports for every other runtime API.

Additional, a new virtual modules has been added: #imports. Used the same way as with Nuxt. You can import all APIs from it and WXT will automatically convert each import to their auto-imported module:

import { ContentScriptContext, useAppConfig } from '#imports';

Would become:

import { ContentScriptContext } from 'wxt/utils/content-script-context';
import { useAppConfig } from 'wxt/utils/app-config';

TODO

  • Add individual exports
  • Add the new #imports module, making sure editors give it priority when manually importing an API.
  • Update paths to wxt/utils/*
  • Add new unit testing docs
  • Rebase off v0.20 branch

@netlify

netlify Bot commented Dec 7, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 86aeb20
🔍 Latest deploy log https://app.netlify.com/sites/creative-fairy-df92c4/deploys/6753e0f08df2580008bc87d8
😎 Deploy Preview https://deploy-preview-1258--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@aklinker1 aklinker1 changed the title Individual exports feat: Add individual exports and the #imports virtual module Dec 7, 2024
@pkg-pr-new

pkg-pr-new Bot commented Dec 7, 2024 •

Copy link
Copy Markdown

Open in Stackblitz

@wxt-dev/auto-icons

npm i https://pkg.pr.new/@wxt-dev/auto-icons@1258

@wxt-dev/i18n

npm i https://pkg.pr.new/@wxt-dev/i18n@1258

@wxt-dev/module-react

npm i https://pkg.pr.new/@wxt-dev/module-react@1258

@wxt-dev/module-solid

npm i https://pkg.pr.new/@wxt-dev/module-solid@1258

@wxt-dev/module-svelte

npm i https://pkg.pr.new/@wxt-dev/module-svelte@1258

@wxt-dev/module-vue

npm i https://pkg.pr.new/@wxt-dev/module-vue@1258

@wxt-dev/storage

npm i https://pkg.pr.new/@wxt-dev/storage@1258

@wxt-dev/unocss

npm i https://pkg.pr.new/@wxt-dev/unocss@1258

wxt

npm i https://pkg.pr.new/wxt@1258

commit: 86aeb20

@codecov

codecov Bot commented Dec 7, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 88.96104% with 34 lines in your changes missing coverage. Please review.

Project coverage is 80.29%. Comparing base (236e099) to head (3ba0c63).
Report is 20 commits behind head on 0.20.0-breaking-changes.

Files with missing lines Patch % Lines
...ges/wxt/src/utils/content-script-ui/shadow-root.ts 71.42% 18 Missing ⚠️
packages/wxt/src/testing/wxt-vitest-plugin.ts 0.00% 3 Missing ⚠️
packages/wxt/src/utils/match-patterns.ts 0.00% 1 Missing and 1 partial ⚠️
packages/wxt/src/virtual/background-entrypoint.ts 0.00% 2 Missing ⚠️
...irtual/content-script-isolated-world-entrypoint.ts 0.00% 2 Missing ⚠️
packages/wxt/src/virtual/reload-html.ts 0.00% 1 Missing and 1 partial ⚠️
packages/wxt/src/utils/app-config.ts 0.00% 0 Missing and 1 partial ⚠️
packages/wxt/src/utils/inject-script.ts 0.00% 0 Missing and 1 partial ⚠️
...ges/wxt/src/utils/internal/dev-server-websocket.ts 0.00% 0 Missing and 1 partial ⚠️
...rc/virtual/content-script-main-world-entrypoint.ts 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@                     Coverage Diff                     @@
##           0.20.0-breaking-changes    #1258      +/-   ##
===========================================================
+ Coverage                    80.24%   80.29%   +0.04%     
===========================================================
  Files                          127      126       -1     
  Lines                         6070     5958     -112     
  Branches                      1020      999      -21     
===========================================================
- Hits                          4871     4784      -87     
+ Misses                        1184     1157      -27     
- Partials                        15       17       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread packages/wxt/package.json
},
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved all this stuff down since I look at scripts and dependencies more often. Now that there are way more exports, I had to scroll way further.

Comment on lines -15 to -16
const options = wxt.config.imports;
if (options === false) return;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now, instead of disabling the entire module, we only set a flag to false in the vite plugin to prevent adding auto-imports, but still replace #imports.

const injected = await unimport.injectImports(code, id);
const injected = await unimport.injectImports(code, id, {
// Only auto-import modules if enabled
autoImport: autoImportsEnabled,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Here is where auto-imports are disabled from now.

@aklinker1 aklinker1 changed the title feat: Add individual exports and the #imports virtual module feat!: Add individual exports and the #imports virtual module Dec 7, 2024
@aklinker1 aklinker1 changed the title feat!: Add individual exports and the #imports virtual module feat!: Individual exports and introduce the #imports virtual module Dec 7, 2024
@aklinker1 aklinker1 changed the title feat!: Individual exports and introduce the #imports virtual module feat!: Individual exports and introduce the #imports module Dec 7, 2024
@aklinker1
aklinker1 marked this pull request as ready for review December 7, 2024 05:47
@aklinker1
aklinker1 marked this pull request as draft December 7, 2024 05:47
@aklinker1
aklinker1 changed the base branch from main to 0.20.0-breaking-changes December 7, 2024 05:47
Comment thread packages/wxt/src/core/resolve-config.ts Resolved
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.

1 participant