Skip to content

Commit bf6c11d

Browse files
author
Baitu Huang
committed
fix bug in getDependencies
1 parent c6b1f5f commit bf6c11d

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ npm install vue-flags-webpack-plugin -D
1515
options:
1616
* `flags` ({[k: string]: boolean} | string, required)
1717

18-
a plain object that contains flags value(boolean) or a file path that exports the final flags object.
18+
A plain object that contains flags value(boolean).
19+
20+
Or a file path(will be executed many times!) which exports the final flags object.
1921
```javascript
2022
flags: {
2123
FLAG_A: true,
@@ -38,7 +40,7 @@ options:
3840
`watch` could also be an array including extra files paths which will be watched.
3941
```javascript
4042
watch: process.env.NODE_ENV === 'development'
41-
// or
43+
// or (no need to add extra files in most cases, just set it true)
4244
watch: [ './config/flag1.js', './config/flag2.js' ]
4345
```
4446

lib/resolve-options.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const clearModule = require('clear-module')
55
const validateOptions = require('schema-utils')
66

77
const { RESOLVED_FLAGS_PATH, PLUGIN_NAME } = require('./constants')
8-
const { genError, toFunc } = require('./utils')
8+
const { genError, toFunc, getDependencies } = require('./utils')
99

1010
const flagsSchema = {
1111
type: 'object',
@@ -98,9 +98,9 @@ function updateGetFlags (flagsPaths, errors = 0) {
9898
let newFlags
9999
try {
100100
newFlags = require(flagPath)
101-
} catch (e) {
101+
} catch (err) {
102102
// errors is used for different editor file-writting mechanism
103-
if (errors > 10) { throw e }
103+
if (errors > 10) { throw err } // 😔
104104
return updateGetFlags(flagsPaths, errors + 1)
105105
}
106106
validateOptions(flagsSchema, newFlags, PLUGIN_NAME)
@@ -138,8 +138,9 @@ function setOptions ({ flags, ignoreFiles, namespace, watch }, context, watchOpt
138138
if (!dev) {
139139
throw genError('Make sure only use "watch" in development mode!')
140140
}
141-
pluginOptions.flags = updateGetFlags(flags)
142-
const flagFiles = [flags]
141+
const flagFiles = getDependencies(() => {
142+
pluginOptions.flags = updateGetFlags(flags)
143+
}, [flags])
143144
if (Array.isArray(watch)) {
144145
watch.forEach(file => {
145146
if (!path.isAbsolute(file)) {

lib/utils.js

+28
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,31 @@ exports.toFunc = function toFunc (exp, useWith = true) {
3434
}
3535
return funcsMap[exp]
3636
}
37+
38+
const Module = require('module')
39+
const path = require('path')
40+
const clearModule = require('clear-module')
41+
exports.getDependencies = function getDependencies (callback, deps) {
42+
deps = new Set(deps)
43+
const resolveFilename = Module._resolveFilename
44+
Module._resolveFilename = function _resolveFilename (req, ...args) {
45+
const filename = resolveFilename.call(this, req, ...args)
46+
if (path.isAbsolute(req) && !/node_modules/.test(req)) {
47+
deps.add(filename)
48+
}
49+
if (/^\.{1,2}\//.test(req)) {
50+
deps.add(filename)
51+
}
52+
return filename
53+
}
54+
let size
55+
do { // against node cache
56+
size = deps.size
57+
for (let dep of deps) {
58+
clearModule(dep)
59+
}
60+
callback()
61+
} while (size !== deps.size)
62+
Module._resolveFilename = resolveFilename
63+
return [...deps]
64+
}

0 commit comments

Comments
 (0)