Repository navigation
Closed as not planned
Milestone
Description
Describe the bug
In a content script entrypoint, if you import a variable, lets call it MATCHES, and try to use that variable to for the matches option, the build fails with the following error message:
import { MATCHES } from "@/data/matches";
import { sayHello } from "@/helpers/dev";
export default defineContentScript({
matches: MATCHES.allUrl,
async main() {
sayHello("_document-idle.content");
},
});ERROR: MATCHES is not defined
But import { sayHello } from "@/helpers/dev" is okey.
To Reproduce
Steps to reproduce the bug using the reproduction:
pnpm i --ignore-scriptspnpm build --debug
Here's the output:
> [email protected] build /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro
> wxt build "--debug"
WXT 0.13.5 11:12:19 AM
ℹ Building chrome-mv3 for production with Vite 5.0.11 11:12:20 AM
[11:12:20 AM] ⚙ Loading file metadata: /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/src/entrypoints/_document-idle.content/index.ts
[11:12:20 AM] ⚙ Loading file metadata: /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/src/entrypoints/background.ts
⚙ Text: 11:12:20 AM
export default defineBackground(() => {
console.log('Hello background!', { id: browser.runtime.id });
});
No imports:
import { defineUnlistedScript, defineContentScript, defineBackground } from 'wxt/sandbox';
export default defineBackground(() => {
console.log('Hello background!', { id: browser.runtime.id });
});
Code:
import { browser } from 'wxt/browser';
import { defineUnlistedScript, defineContentScript, defineBackground } from 'wxt/sandbox';
export default defineBackground(() => {
console.log('Hello background!', { id: browser.runtime.id });
});
[jiti] [transpile] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/src/entrypoints/background.ts (40.18ms)
[jiti] [transpile] [esm] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/[email protected]/node_modules/wxt/dist/browser.js (0.85ms)
[jiti] [transpile] [esm] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/[email protected]/node_modules/wxt/dist/chunk-FNTE2L27.js (0.791ms)
[jiti] [transpile] [esm] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/[email protected]/node_modules/wxt/dist/virtual/mock-browser.js (1.293ms)
[jiti] [native] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/[email protected]/node_modules/wxt/dist/testing.cjs
[jiti] [transpile] [esm] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/[email protected]/node_modules/wxt/dist/sandbox.js (0.935ms)
[jiti] [native] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/node_modules/.pnpm/@[email protected]/node_modules/@webext-core/match-patterns/lib/index.cjs
⚙ Text: 11:12:20 AM
import { MATCHES } from "@/data/matches";
import { sayHello } from "@/helpers/dev";
export default defineContentScript({
matches: MATCHES.allUrl,
// matches: ["<all-url>"],
async main() {
sayHello("_document-idle.content");
},
});
No imports:
import { defineUnlistedScript, defineContentScript, defineBackground } from 'wxt/sandbox';
export default defineContentScript({
matches: MATCHES.allUrl,
// matches: ["<all-url>"],
async main() {
sayHello("_document-idle.content");
},
});
Code:
import { defineUnlistedScript, defineContentScript, defineBackground } from 'wxt/sandbox';
export default defineContentScript({
matches: MATCHES.allUrl,
// matches: ["<all-url>"],
async main() {
sayHello("_document-idle.content");
},
});
[jiti] [transpile] /Users/aklinker1/Development/local/wxt-entrypoint-imports-repro/src/entrypoints/_document-idle.content/index.ts (1.392ms)
ERROR MATCHES is not defined 11:12:20 AM
at src/entrypoints/_document-idle.content/index.ts:25:12
at evalModule (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:256443)
at jiti (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:254371)
at importEntrypointFile (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:1786:23)
at async getContentScriptEntrypoint (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:451:35)
at async node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:230:18
at async Promise.all (index 0)
at async findEntrypoints (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:218:23)
at async internalBuild (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:2503:23)
at async build (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:2538:10)
✖ Command failed after 367 ms 11:12:20 AM
ERROR MATCHES is not defined 11:12:20 AM
at src/entrypoints/_document-idle.content/index.ts:25:12
at evalModule (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:256443)
at jiti (node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:254371)
at importEntrypointFile (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:1786:23)
at async getContentScriptEntrypoint (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:451:35)
at async node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:230:18
at async Promise.all (index 0)
at async findEntrypoints (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:218:23)
at async internalBuild (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:2503:23)
at async build (node_modules/.pnpm/[email protected]/node_modules/wxt/dist/cli.js:2538:10)
ELIFECYCLE Command failed with exit code 1.
Expected behavior
Variable matches works with defineContentScript intuitively
Environment
System:
OS: Linux 5.15 Ubuntu 20.04.6 LTS (Focal Fossa)
CPU: (16) x64 AMD Ryzen 7 5700G with Radeon Graphics
Memory: 27.67 GB / 31.35 GB
Container: Yes
Shell: 5.8 - /bin/zsh
Binaries:
Node: 18.4.0 - ~/.nvm/versions/node/v18.4.0/bin/node
npm: 8.12.1 - ~/.nvm/versions/node/v18.4.0/bin/npm
pnpm: 8.14.0 - ~/.nvm/versions/node/v18.4.0/bin/pnpm
Browsers:
Chrome: 115.0.5790.98
npmPackages:
wxt: ^0.13.5 => 0.13.5
Metadata
Metadata
Assignees
Labels
No labels

Activity
yunsii commentedon Jan 9, 2024
I have to use literal value instead.
[-]Path alias with defineContentScript matches throw error[/-][+]Use defineContentScript matches with variable encounter error[/+]aklinker1 commentedon Jan 9, 2024
Yes, you must use literal values when defining options for entrypoints.
Why? When importing your entrypoints to get it's options during the build process, the file is imported in a node environment. It doesn't have access to
windowor other DOM globals. It also doesn't have access to thechromeorbrowserglobals an extension normally has access to.So to get around this, wxt strips all imports from your file, and then imports it.
This makes sure any imported modules with side-effects doesn't cause a build failure when accessing one of these global variables.
The only solution to solve this problem is to setup a full browser environment while loading the entrypoints files, which I decided would be too error prone initially. So the initial implementation removed imports.
I want to allow imported variables to be used for the entrypoint definition, but haven't had the time to enable that yet. Would be open to contributions for this.
yunsii commentedon Jan 9, 2024
It is inconvient to arrange matches with logic, and is it significant impact of build entrypoints if setup a full browser environment?
aklinker1 commentedon Jan 9, 2024
Sorry, I don't understand the question.
aklinker1 commentedon Jan 9, 2024
For future reference, here is the file responsible for importing the entrypoints during the build process to grab their options.
https://github.com/wxt-dev/wxt/blob/8c834835c453dcb49f8b44fa432203d83b902a81/src/core/utils/building/import-entrypoint.ts
[-]Use defineContentScript matches with variable encounter error[/-][+]Allow imports in entrypoints[/+][-]Allow imports in entrypoints[/-][+]Allow using imported variables in entrypoint options[/+]yunsii commentedon Jan 10, 2024
Variable
matchescan be used in business logic too, if must use literal values, developers have to maintain two copies ofmatchesdata sometime.aklinker1 commentedon Jan 10, 2024
Since WXT doesn't run the
mainfunction during the build, it only grabs the object passed intodefineContentScript, any imported variables used inside themainfunction doesn't throw any errors because that code is never executed.It's the same reason why this code runs without throwing an error. The example function is never called.
Yes. That's why I'd accept a PR to fix this. Eventually, I'll get to this, but not until after v1.0 is out.
aklinker1 commentedon May 3, 2024
I'm gonna close this issue, it will be fixed in #506, follow that one for updates.
aklinker1 commentedon Jul 25, 2024
For future reference: working-repro.zip
Removing the
experimental.entrypointImporteroption will cause the original error to occur.