Skip to content

Commit 65abae2

Browse files
author
Brad Cornes
committed
use decache when resolving config
1 parent 37bdf74 commit 65abae2

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

packages/tailwindcss-class-names/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tailwindcss-class-names/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "Brad Cornes <hello@bradley.dev>",
1313
"license": "MIT",
1414
"dependencies": {
15+
"callsite": "^1.0.0",
1516
"chokidar": "^3.3.1",
1617
"dlv": "^1.1.3",
1718
"dset": "^2.0.1",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as path from 'path' // if module is locally defined we path.resolve it
2+
import callsite from 'callsite'
3+
import Module from 'module'
4+
5+
function find(moduleName) {
6+
if (moduleName[0] === '.') {
7+
var stack = callsite()
8+
for (var i in stack) {
9+
var filename = stack[i].getFileName()
10+
// if (filename !== module.filename) {
11+
moduleName = path.resolve(path.dirname(filename), moduleName)
12+
break
13+
// }
14+
}
15+
}
16+
try {
17+
return __non_webpack_require__.resolve(moduleName)
18+
} catch (e) {
19+
return
20+
}
21+
}
22+
23+
/**
24+
* Removes a module from the cache. We need this to re-load our http_request !
25+
* see: http://stackoverflow.com/a/14801711/1148249
26+
*/
27+
function decache(moduleName) {
28+
moduleName = find(moduleName)
29+
30+
if (!moduleName) {
31+
return
32+
}
33+
34+
// Run over the cache looking for the files
35+
// loaded by the specified module name
36+
searchCache(moduleName, function(mod) {
37+
delete __non_webpack_require__.cache[mod.id]
38+
})
39+
40+
// Remove cached paths to the module.
41+
// Thanks to @bentael for pointing this out.
42+
Object.keys(Module.prototype.constructor._pathCache).forEach(function(
43+
cacheKey
44+
) {
45+
if (cacheKey.indexOf(moduleName) > -1) {
46+
delete Module.prototype.constructor._pathCache[cacheKey]
47+
}
48+
})
49+
}
50+
51+
/**
52+
* Runs over the cache to search for all the cached
53+
* files
54+
*/
55+
function searchCache(moduleName, callback) {
56+
// Resolve the module identified by the specified name
57+
var mod = __non_webpack_require__.resolve(moduleName)
58+
var visited = {}
59+
60+
// Check if the module has been resolved and found within
61+
// the cache no else so #ignore else http://git.io/vtgMI
62+
/* istanbul ignore else */
63+
if (mod && (mod = __non_webpack_require__.cache[mod]) !== undefined) {
64+
// Recursively go over the results
65+
;(function run(current) {
66+
visited[current.id] = true
67+
// Go over each of the module's children and
68+
// run over it
69+
current.children.forEach(function(child) {
70+
// ignore .node files, decachine native modules throws a
71+
// "module did not self-register" error on second require
72+
if (path.extname(child.filename) !== '.node' && !visited[child.id]) {
73+
run(child)
74+
}
75+
})
76+
77+
// Call the specified callback providing the
78+
// found module
79+
callback(current)
80+
})(mod)
81+
}
82+
}
83+
84+
export default decache

packages/tailwindcss-class-names/src/resolveConfig.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importFrom from 'import-from'
22
import * as path from 'path'
3+
import decache from './decache'
34

45
export default function resolveConfig({ cwd, config }) {
56
let resolve = x => x
@@ -8,6 +9,7 @@ export default function resolveConfig({ cwd, config }) {
89
if (!cwd) {
910
cwd = path.dirname(config)
1011
}
12+
decache(config)
1113
config = __non_webpack_require__(config)
1214
}
1315

0 commit comments

Comments
 (0)