Different output files with different configs What's the best way to compile 2 JS files that require different configs? Namely, my server config requires some additional config options from my regular frontend config.
This is basically what I'm trying to do (spoiler, it doesn't work because the 2nd config is applied to the client side as well:
const sharedConfig = {
// Shared config
};
mix
.setPublicPath('./')
.webpackConfig(sharedConfig)
.copy('src/static', './public')
.ts('src/ts/index.tsx', 'public/js/app.js');
mix
.setPublicPath('./')
.webpackConfig({
...sharedConfig,
externals: [nodeExternals()],
node: {
__dirname: false,
},
})
.ts('node/server.tsx', './server.js');
As the next best thing is it possible to get node env variables within the webpack.mix.js?
Something like this would work well enough:
const sharedConfig = {
// Shared config
};
if (!process.env.isserver) {
mix
.setPublicPath('./')
.webpackConfig(sharedConfig)
.copy('src/static', './public')
.ts('src/ts/index.tsx', 'public/js/app.js');
} else {
mix
.setPublicPath('./')
.webpackConfig({
...sharedConfig,
externals: [nodeExternals()],
node: {
__dirname: false,
},
})
.ts('node/server.tsx', './server.js');
}
But I can't get this to work either. It doesn't read any variables I pass to the script
How is your scripts was declared in package.json?
Anyway, you can read the any variables in your .env, you only need put this line top of your mix file. This is the other approach.
require('dotenv').config();
Yeah that's what I ended up doing. Saw that mix uses let argv = require('yargs').argv; to get args passed in through package.json and ended up doing this:
mix
.setPublicPath('./')
.webpackConfig(sharedConfig);
if (argv.isserver) {
mix
.webpackConfig({
externals: [nodeExternals()],
node: {
__dirname: false,
},
})
.ts('node/server.tsx', './server.js');
} else {
mix
.copy('src/static', './public')
.ts('src/ts/index.tsx', 'public/js/app.js');
}
Thanks!
Please sign in or create an account to participate in this conversation.