Closed chan-dev closed 8 months ago
Install @rollup/plugin-replace, then modify vite.config.content.ts
:
export default defineConfig({
...
plugins: [
...sharedConfig.plugins || [],
replace({
values: {
'process.env.NODE_ENV': `"${env.NODE_ENV || 'production'}"`,
},
}),
],
})
@TP-O thanks. This solved my issue.
One minor modification is that I have to wrap the value in JSON.stringify
.
I also have this issue; content scripts are not executed.
@chan-dev were you able to get content scripts to respond to messages using onMessage
with @TP-O 's solution?
@ivyraine It seems to work fine on my end. Although i'm just testing on the dev build using pnpm run dev
Here's exactly what i put on my vite.config.content.ts
plugins: [
...sharedConfig.plugins || [],
replace({
values: {
'process.env.NODE_ENV': JSON.stringify(`${process.env.NODE_ENV || 'production'}`),
},
}),
],
Were there any errors on your end? Maybe it has something to do with the webext-bridge
plugin.
https://github.com/zikaari/webext-bridge
@TP-O Just realized that you no longer need to install a separate plugin, but rather simply define it under the sharedConfig
in vite.config.ts
file.
define: {
'__DEV__': isDev,
'process.platform': null,
'process.version': null,
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
},
Have you tried this one?
@chan-dev Yes, it works :D
Hi there 👋
I have tried both methods, but running yarn run dev
and loading the extension into Chrome still returns a Uncaught ReferenceError: process is not defined
.
For the record, I'm using the mv3
branch and the issue comes when I try to display a stored variable into background/index.ts
:
// background/index.ts
import { storageDemo } from '~/logic/storage'
console.log('Current - Storage ::: ', storageDemo);
Will result in
Uncaught ReferenceError: process is not defined
at node_modules/vue/index.js (index.js:3:1)
at __require (index.mjs:11:50)
at index.mjs:1:15
when I try to display a stored variable into background/index.ts:
background/index.ts
is bundled by tsup, so you should add the config in tsup.config.ts
:
// tsup.config.ts
export default defineConfig(() => ({
define: {
__DEV__: JSON.stringify(isDev),
'process.env.NODE_ENV': JSON.stringify(isDev ? 'development' : 'production'),
},
}))
Describe the bug
The build is successful, everything works fine except for the content script. However, it shows an error
Uncaught ReferenceError: process is not defined
on development mode runningpnpm run dev
.Here's the stack trace on the page of the built extension.
Reproduction
Build the newly cloned repo and check the content script
System Info
Used Package Manager
pnpm
Validations