samrum / vite-plugin-web-extension

A vite plugin for generating cross browser platform, ES module based web extensions.
MIT License
323 stars 33 forks source link

How to deal with defines? #116

Closed s-h-a-d-o-w closed 1 year ago

s-h-a-d-o-w commented 1 year ago

vite doesn't replace defines in dev. Resulting in errors such as ReferenceError: process is not defined.

Presumably, wherever those globals are defined - it can't be reached by e.g. content scripts.

Are you aware of a solution to this?

samrum commented 1 year ago

Should work in the latest release (5.0.0)

s-h-a-d-o-w commented 1 year ago

What a coincidence, thanks for releasing it! Unfortunately, it doesn't seem to work.

I hope the following context can shed some light on the problem:

main.js has changed from

(async()=>{await import('http://localhost:5173/./src/main.js')})();

to

(async()=>{
    await import("http://localhost:5173/@vite/client");
    await import("http://localhost:5173/./src/main.js")
}
)();

@vite/client contains:

import "/node_modules/vite/dist/client/env.mjs";

// ...

node_modules/vite/dist/client/env.mjs contains:

const context = (() => {
    if (typeof globalThis !== 'undefined') {
        return globalThis;
    }
    else if (typeof self !== 'undefined') {
        return self;
    }
    else if (typeof window !== 'undefined') {
        return window;
    }
    else {
        return Function('return this')();
    }
})();
// assign defines
const defines = {};
Object.keys(defines).forEach((key) => {
    const segments = key.split('.');
    let target = context;
    for (let i = 0; i < segments.length; i++) {
        const segment = segments[i];
        if (i === segments.length - 1) {
            target[segment] = defines[key];
        }
        else {
            target = target[segment] || (target[segment] = {});
        }
    }
});

// sourcemap

Should that defines variable there maybe contain the values I defined instead of an empty object?

s-h-a-d-o-w commented 1 year ago

Actually, this time, it was my fault. 😅 For some reason, I had define within the rollup options. 🤦