crxjs / chrome-extension-tools

Bundling Chrome Extensions can be pretty complex. It doesn't have to be.
https://crxjs.dev/vite-plugin
2.71k stars 181 forks source link

[Bug] build error when specifying '<all_urls>' in 'matches' field of 'content_scripts' in the manifest #511

Open bruceeewong opened 1 year ago

bruceeewong commented 1 year ago

Build tool

Vite

Where do you see the problem?

Describe the bug

when setting in matches field of content script in the manifest json

// manifest.json
{
  content_scripts: [
    {
      matches: ['<all_urls>'],
      js: ['src/scripts/content/index.ts'],
    },
  ],
}

it triggers errors during building:

[crx:content-script-resources] TypeError: Cannot read properties of undefined (reading 'split')

// error stack at @crxjs/vite-plugin/dist/index.mjs:104:35)

const stubMatchPattern = (pattern) => {
  const [schema, rest] = pattern.split("://");  // ! <- the split logic is the problem
  const [origin, pathname] = rest.split("/");
  const root = `${schema}://${origin}`;
  return pathname ? `${root}/*` : root;
};

Reproduction

set manifest like bellow, and run vite build

// manifest.json
{
  content_scripts: [
    {
      matches: ['<all_urls>'],  // <- set all_urls would trigger the building error
      js: ['src/scripts/content/index.ts'],
    },
  ],
}

Logs

[crx:content-script-resources] TypeError: Cannot read properties of undefined (reading 'split')
    at stubMatchPattern (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/@crxjs/vite-plugin/dist/index.mjs:104:35)
    at Array.map (<anonymous>)
    at Object.renderCrxManifest (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/@crxjs/vite-plugin/dist/index.mjs:3322:59)
    at Object.generateBundle (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/@crxjs/vite-plugin/dist/index.mjs:2919:60)
    at async Bundle.generate (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/rollup/dist/es/shared/rollup.js:15990:9)
    at async file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/rollup/dist/es/shared/rollup.js:23754:27
    at async catchUnfinishedHookActions (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/rollup/dist/es/shared/rollup.js:23088:20)
    at async doBuild (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/vite/dist/node/chunks/dep-665b0112.js:45739:20)
    at async build (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/vite/dist/node/chunks/dep-665b0112.js:45570:16)
    at async CAC.<anonymous> (file:///Users/bruski/workspace/projects/suiet/suiet/node_modules/vite/dist/node/cli.js:748:9)
[crx:manifest-post] Error in crx:content-script-resources.renderCrxManifest

### System Info

"@crxjs/vite-plugin": "^1.0.13"

```shell
System:
    OS: macOS 12.3.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 105.06 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  Browsers:
    Chrome: 105.0.5195.102
    Firefox: 101.0
    Safari: 15.4


### Severity

annoyance
kaulshashank commented 1 year ago

Facing the same issue. @crxjs/vite-plugin @ 1.0.12 and vite @ 2.9.X

If it helps anybody, I got it working with https://*/* as the match pattern.

More on patterns here.

cymptom commented 1 year ago

Same problem here. *://*/* does work, but that's not quite at parity with <all_urls>.

The urn:* pattern doesn't seem to be supported, either.

I think this is a duplicate of this issue, which looks to have been closed without a fix 🤔