next.js icon indicating copy to clipboard operation
next.js copied to clipboard

webpack.DefinePlugin is not working on next.config.js

Open hank7444 opened this issue 9 years ago • 7 comments

next.config.js

image

pages/index.js

image

result

image

Does it mean I lost something @@?

hank7444 avatar Jul 05 '17 07:07 hank7444

Please see https://github.com/zeit/next.js/tree/canary/examples/with-universal-configuration

timneutkens avatar Jul 05 '17 07:07 timneutkens

I wanna submit that this issue gets reopened.

While you have solved the underlining issue for @hank7444 there may be other use-cases and workflows that still depend on DefinePlugin.

All to say I believe the issue still stands that the variables defined in webpack.DefinePlugin do not exist in the bundled files.

mrsteele avatar Aug 02 '17 14:08 mrsteele

Completely agree, this is still not possible. ReferenceError is always thrown. @timneutkens your solution has caveats as says the readme

tgdn avatar Oct 13 '17 18:10 tgdn

why did you close this issue,what's the solution? i also met this problem too recently!

Deval520 avatar Nov 21 '17 07:11 Deval520

what's the solution? i also met this problem too recently! @timneutkens

Deval520 avatar Nov 21 '17 07:11 Deval520

it works for me

const webpack = require('webpack'); // eslint-disable-line
const FRONTEND_ENV_KEYS = [
	'NODE_ENV',
	'IS_STATIC',
	'API_URL',
	'CONTENTFUL_SPACE',
	'CONTENTFUL_ACCESS_TOKEN'
];

const envPlugin = FRONTEND_ENV_KEYS.reduce(
	(result, key) =>
		Object.assign({}, result, {
			[`process.env.${key}`]: JSON.stringify(process.env[key])
		}),
	{}
);

module.exports = {
	webpack(cfg) {
		// make selected env vars avail on client
		cfg.plugins.push(new webpack.DefinePlugin(envPlugin));
		return cfg;
	}
	// exportPathMap: require('./next.exportPathMap').exportPathMap
};

makstr avatar Nov 22 '17 05:11 makstr

^ not sure why everyone downvoted this. it works fine and it's something I've been using in production for at least a year now.

matthewmueller avatar Oct 26 '18 12:10 matthewmueller