Can't make sense of this. Been fighting webpack to get it to ignore the server-side sdks 😦
Any ideas where I should be looking?
You can add module aliasing to your webpack config, let me get an example code
https://github.com/payloadcms/plugin-stripe/blob/main/src/extendWebpackConfig.ts
here is how this plugin does it
Essentially the same in for your own config
You have a mocks module similar to this
https://github.com/payloadcms/plugin-stripe/blob/main/src/mocks/serverModule.jsimport path from 'path'
import type { Config } from 'payload/config'
import type { Configuration as WebpackConfig } from 'webpack'
const mockModulePath = path.resolve(__dirname, './../mocks/emptyObject')
export const extendWebpackConfig =
(config: Config): ((webpackConfig: WebpackConfig) => WebpackConfig) =>
webpackConfig => {
const existingWebpackConfig =
typeof config.admin?.webpack === 'function'
? config.admin.webpack(webpackConfig)
: webpackConfig
return {
...existingWebpackConfig,
resolve: {
...(existingWebpackConfig.resolve || {}),
cache: false,
alias: {
...(existingWebpackConfig.resolve?.alias ? existingWebpackConfig.resolve.alias : {}),
[path.resolve(__dirname, './firebaseNotifications')]: mockModulePath,
[path.resolve(__dirname, './firebase')]: mockModulePath,
},
fallback: {
...(existingWebpackConfig.resolve?.fallback ? existingWebpackConfig.resolve.fallback : {}),
stream: false,
crypto: false,
tls: false,
fs: false,
zlib: false,
os: false,
child_process: false,
net: false,
}
},
}
}
Though your mocks can be just an empty object like
{}
Check! Got the mocks module as well
what does your payload config look like?
Webpack looks like it compiles just fine, api works. It's just in the browser where the admin route isn't working
Gimme a sec
Your original error tells me that there's code being shipped to the frontend which is then trying to use the firebaseNotifications default function
// The config from my plugin
const config: Config = {
...incomingConfig,
admin: {
...incomingConfig.admin,
webpack: extendWebpackConfig(incomingConfig),
},
collections: incomingConfig.collections.map((collection) => {
if (collection.slug !== options.messageCollection.slug) {
return collection;
}
return {
...collection,
hooks: {
...collection.hooks,
afterChange: [afterChangeHook],
},
};
}),
};
// payload.config.ts
admin: {
user: Users.slug,
webpack: (config) => ({
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
jsonwebtoken: mockPath,
[verifyTokenMockPath]: mockPath,
},
fallback: {
...config.resolve.fallback,
util: false,
"safe-buffer": false,
crypto: false,
},
},
}),
},
Are you aliasing all files importing firebase?
Yup. At this point it's only
firebaseNotifications.ts
, a
firebase.ts
(essentially just importing admin in node, client in browser) and
extendWebpackConfig.ts
No other occurrences or places I import the firebase sdk
theres no
node_modules/.cache/webpack
to clear right?
Cleared that again for good measure
I'll see if I can move them onto a repository, easier to spot obvious errors when it's not just bits of code 🙂
Yeah that'd be really helpful!
Haven't gotten to the point setting it up as a separate package, so I just dumped the files in question. Wasn't planning on showing it until I had a working prototype 🙂
So, playing around with the mock - changed it from {} to for example
export default () => {}
and at least I get a different error. Could it be that a different type of mock could be the answer?
My mocks is
module.exports = {};
maybe try that? 👀
otherwise ill take your repo for a spin locally
At least I get a different error, so its for sure something with the mock
k let me hoist that code up
Oh okay
got it
So my guess was that I had to mimic the plugin signature
i usually also alias all module imports like
firebase-admin/messaging
firebase-admin/app
since it's like
const plugin = (config) => (config2) => ({});
I tried this and it worked;
module.exports = () => () => ({});
Oh you got it working?
Yup!
Weird! but good its working now
cant wait for the
update to core
Really weird. The api was working just fine, webpack compiled without errors and it's just the admin panel that didn't want to run
Yeah, it's purely for removing out server side code for the frontend bundle
haha, agreed! I feel like
belongs with the
on my resume
at best you'll get errors are modules try to use node.js packages like
fs
and at worst you're leaking server side secrets
at this point for sure
we'll see what the crafty people decide! but there's for sure better alternatives out there 🥲
Okay, back to actually building stuff! I owe you one for the awesome rubber ducking, @paulpopus
I spoke too soon, friend!
Something else is wonky.. Collections are in the config, but isn't handled properly now 😅
Api still works fine though
Does this happen with a fresh db?
I managed to resolve the issue in
https://discord.com/channels/967097582721572934/1119402480804036688. It looked like things were running and that webpack was happy, but it wasnt 😛
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.