Skip to content

Commit 9c52ee8

Browse files
docs(plugins) define plugin runtimeValue update (#3925)
1 parent 31b93ca commit 9c52ee8

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/content/plugins/define-plugin.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contributors:
1111

1212
The `DefinePlugin` allows you to create global constants which can be configured at __compile__ time. This can be useful for allowing different behavior between development builds and production builds. If you perform logging in your development build but not in the production build you might use a global constant to determine whether logging takes place. That's where `DefinePlugin` shines, set it and forget it rules for development and production builds.
1313

14-
``` javascript
14+
```javascript
1515
new webpack.DefinePlugin({
1616
// Definitions...
1717
});
@@ -29,7 +29,7 @@ Each key passed into `DefinePlugin` is an identifier or multiple identifiers joi
2929

3030
The values will be inlined into the code allowing a minification pass to remove the redundant conditional.
3131

32-
``` javascript
32+
```javascript
3333
new webpack.DefinePlugin({
3434
PRODUCTION: JSON.stringify(true),
3535
VERSION: JSON.stringify('5fa3b9'),
@@ -40,7 +40,7 @@ new webpack.DefinePlugin({
4040
});
4141
```
4242

43-
``` javascript
43+
```javascript
4444
console.log('Running App version ' + VERSION);
4545
if(!BROWSER_SUPPORTS_HTML5) require('html5shiv');
4646
```
@@ -50,9 +50,7 @@ W> When defining values for `process` prefer `'process.env.NODE_ENV': JSON.strin
5050

5151
T> Note that because the plugin does a direct text replacement, the value given to it must include __actual quotes__ inside of the string itself. Typically, this is done either with alternate quotes, such as `'"production"'`, or by using `JSON.stringify('production')`.
5252

53-
__index.js__
54-
55-
``` javascript
53+
```javascript
5654
if (!PRODUCTION) {
5755
console.log('Debug info');
5856
}
@@ -64,7 +62,7 @@ if (PRODUCTION) {
6462

6563
After passing through webpack with no minification results in:
6664

67-
``` javascript
65+
```javascript
6866
if (!true) {
6967
console.log('Debug info');
7068
}
@@ -75,7 +73,7 @@ if (true) {
7573

7674
and then after a minification pass results in:
7775

78-
``` javascript
76+
```javascript
7977
console.log('Production log');
8078
```
8179

@@ -102,23 +100,23 @@ new webpack.DefinePlugin({
102100
});
103101
```
104102

105-
### Rebuild
103+
## Runtime values via `runtimeValue`
106104

107-
Rebuild where there are changes in runtimeValue.
105+
`function (getterFunction, [string]) => getterFunction()`
108106

109-
__webpack.app.config.js__
107+
It is possible to define variables with values that rely on files and will be re-evaluated when such files change in the file system. This means webpack will rebuild when such watched files change.
110108

111-
```javascript
109+
Arguments:
110+
111+
- The first argument of the `webpack.DefinePlugin.runtimeValue` is a `function` that should return the value to be assigned to the definition.
112+
- The second argument is an array of file paths to watch for. Pass `true` instead of `[string]` here to flag the module as uncacheable.
112113

114+
```javascript
113115
const fileDep = path.resolve(__dirname, 'sample.txt');
114116

115117
new webpack.DefinePlugin({
116118
BUILT_AT: webpack.DefinePlugin.runtimeValue(Date.now, [fileDep])
117119
});
118120
```
119121

120-
The first argument to `runtimeValue` is a `function` that returns the value to be assigned and the second argument is an array of `fileDependencies` (what files to be watch for).
121-
122-
In the previous example, the value of `BUILD_AT` would be the time at which the file passed as `fileDep` was last updated in the file system.
123-
124-
T> Passing `true` as second argument instead of `fileDependencies` will flag the module as uncacheable.
122+
The value of `BUILT_AT` would be the time at which the `'sample.txt'` was last updated in the file system, e.g. `1597953013291`.

0 commit comments

Comments
 (0)