Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefinePlugin / add support for watch mode #7717

Closed
crazyx13th opened this issue Jul 13, 2018 · 4 comments · Fixed by #8267
Closed

DefinePlugin / add support for watch mode #7717

crazyx13th opened this issue Jul 13, 2018 · 4 comments · Fixed by #8267

Comments

@crazyx13th
Copy link

crazyx13th commented Jul 13, 2018

Feature request

What is the expected behavior?
For Example: I have a build nr, which increments each "runWatch" hook. But DefinePlugin only define variables at start, not on re-compile.

What is motivation or use case for adding/changing the behavior?
Now you can use a Function as a value (#6749). DefinePlugin only should update value with this defined function.

How should this be implemented in your opinion?
Only update values with the user defined function. Many other Plugins use a function for updates values on re-compile.

Are you willing to work on this yourself?
I will support this as good as I can :-)

@sokra
Copy link
Member

sokra commented Jul 17, 2018

The module need to marked as non-cacheable to support this behavior. The DefinePlugin.runtimeValue support a list of dependencies which cause a rebuild of the module. You can send a PR adding the ability to pass true as fileDependencies which flags the module as non-cacheable and reevaulate the expression every-build. This is bad for performance (because module is no longer cached), so avoid using the defined variable in too many modules.

@crazyx13th
Copy link
Author

thx!
if someone need's it, here is an example:

this.plugins.push(new webpack.DefinePlugin({
	'app.env': {
		'appVersion': `"${this.settings.appVersion}"`,
		'appHash': `"${this.settings.appHash}"`,
		'buildNr': webpack.DefinePlugin.runtimeValue(() => `"${this.settings.buildNr}"`, []),
		'apiPath': `"${process.env.APP_APIPATH}"`,
		'stage': `"${this.configuration.mode}"`,
	}
}));

@ZuBB
Copy link

ZuBB commented Jan 18, 2020

Thanks @crazyx13th it does work!

@kettanaito
Copy link

For anybody wondering, this is how you provide a runtime define variable:

// webpack.config.js
const webpack = require('webpack')

module.exports = {
  plugins: [
    new webpack.DefinePlugin({
      MY_VARIABLE_NAME: webpack.DefinePlugin.runtimeValue(
        () => JSON.stringify('ANY_VALUE'),
        // Passing the second argument as `true` reevaluates the defined expression.
        // Beware that this disables module caching and must be used with caution.
        true,
      ),
    })
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants