Skip to content

Commit

Permalink
Enable hot reloading of the functional mixins (#146)
Browse files Browse the repository at this point in the history
* Enable hot reloading of the functional mixins

* Update tests: add dependency case
  • Loading branch information
andgra authored Jun 20, 2022
1 parent e6fb492 commit 807b588
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
21 changes: 20 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ function addMixin(helpers, mixins, rule, file) {
rule.remove()
}

function processModulesForHotReloadRecursively(module, helpers) {
let moduleId = module.id
module.children.forEach(childModule => {
helpers.result.messages.push({
type: 'dependency',
file: childModule.id,
parent: moduleId
})
processModulesForHotReloadRecursively(childModule, helpers)
})
delete require.cache[moduleId]
}

function loadGlobalMixin(helpers, globs) {
let cwd = process.cwd()
let files = glob.sync(globs, { caseSensitiveMatch: IS_WIN })
Expand All @@ -53,7 +66,13 @@ function loadGlobalMixin(helpers, globs) {
addMixin(helpers, mixins, atrule, path)
})
} else {
mixins[name] = { mixin: require(path), file: path }
try {
mixins[name] = { mixin: require(path), file: path }
let module = require.cache[require.resolve(path)]
if (module) {
processModulesForHotReloadRecursively(module, helpers)
}
} catch {}
}
})
return mixins
Expand Down
1 change: 1 addition & 0 deletions test/deps/f.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./g')
3 changes: 3 additions & 0 deletions test/deps/g.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
g: '5'
}
25 changes: 25 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,31 @@ test('loads mixins from file globs', async () => {
})
})

test('loads mixins with dependencies', async () => {
let result = await run(
'a { @mixin f; }',
'a { g: 5; }',
{
mixinsFiles: join(__dirname, 'deps', 'f.js')
}
)
equal(
result.messages.sort((a, b) => a.file && a.file.localeCompare(b.file)),
[
{
file: join(__dirname, 'deps/f.js'),
type: 'dependency',
parent: ''
},
{
file: join(__dirname, 'deps/g.js'),
type: 'dependency',
parent: join(__dirname, 'deps/f.js')
}
]
)
})

test('coverts mixins values', async () => {
let processor = postcss(
mixins({
Expand Down

0 comments on commit 807b588

Please sign in to comment.