Skip to content

Commit

Permalink
Allow setting the current working directory fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Ackerman committed Dec 6, 2017
1 parent eacc709 commit 990d765
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
67 changes: 35 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
let path = require('path')
let extend = require('util')._extend
let cwd = process.cwd()
let BASE_ERROR = 'Circular dependency detected:\r\n'

class CircularDependencyPlugin {
constructor(options) {
this.options = extend({
exclude: new RegExp('$^'),
failOnError: false,
onDetected: false
onDetected: false,
cwd: process.cwd()
}, options)
}

apply(compiler) {
let plugin = this
let cwd = this.options.cwd

compiler.plugin('compilation', (compilation) => {
compilation.plugin('optimize-modules', (modules) => {
for (let module of modules) {
if (module.resource === undefined) { continue }
let maybeCyclicalPathsList = isCyclic(module, module, {})
let maybeCyclicalPathsList = this.isCyclic(module, module, {})
if (maybeCyclicalPathsList) {
// allow consumers to override all behavior with onDetected
if (plugin.options.onDetected) {
Expand Down Expand Up @@ -52,43 +53,45 @@ class CircularDependencyPlugin {
})
})
}
}

function isCyclic(initialModule, currentModule, seenModules) {
// Add the current module to the seen modules cache
seenModules[currentModule.debugId] = true
isCyclic(initialModule, currentModule, seenModules) {
let cwd = this.options.cwd

// If the modules aren't associated to resources
// it's not possible to display how they are cyclical
if (!currentModule.resource || !initialModule.resource) {
return false
}
// Add the current module to the seen modules cache
seenModules[currentModule.debugId] = true

// Iterate over the current modules dependencies
for (let dependency of currentModule.dependencies) {
let depModule = dependency.module
if (!depModule) { continue }
// If the modules aren't associated to resources
// it's not possible to display how they are cyclical
if (!currentModule.resource || !initialModule.resource) {
return false
}

// Iterate over the current modules dependencies
for (let dependency of currentModule.dependencies) {
let depModule = dependency.module
if (!depModule) { continue }

if (depModule.debugId in seenModules) {
if (depModule.debugId === initialModule.debugId) {
// Initial module has a circular dependency
return [
path.relative(cwd, currentModule.resource),
path.relative(cwd, depModule.resource)
]
if (depModule.debugId in seenModules) {
if (depModule.debugId === initialModule.debugId) {
// Initial module has a circular dependency
return [
path.relative(cwd, currentModule.resource),
path.relative(cwd, depModule.resource)
]
}
// Found a cycle, but not for this module
continue
}
// Found a cycle, but not for this module
continue
}

let maybeCyclicalPathsList = isCyclic(initialModule, depModule, seenModules)
if (maybeCyclicalPathsList) {
maybeCyclicalPathsList.unshift(path.relative(cwd, currentModule.resource))
return maybeCyclicalPathsList
let maybeCyclicalPathsList = this.isCyclic(initialModule, depModule, seenModules)
if (maybeCyclicalPathsList) {
maybeCyclicalPathsList.unshift(path.relative(cwd, currentModule.resource))
return maybeCyclicalPathsList
}
}
}

return false
return false
}
}

module.exports = CircularDependencyPlugin
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"webpack": "^3.5.5"
},
"description": "Detect modules with circular dependencies when bundling with webpack.",
"version": "4.2.0",
"version": "4.3.0",
"engines": {
"node": ">=6.0.0"
},
Expand Down

0 comments on commit 990d765

Please sign in to comment.