Skip to content

Commit

Permalink
fix: require files relative to the source file
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Sep 21, 2018
1 parent 134f7f6 commit 1785a0f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"cosmiconfig": "^5.0.5"
"cosmiconfig": "^5.0.5",
"resolve": "^1.8.1"
},
"devDependencies": {
"@babel/core": "^7.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default 'something else'
↓ ↓ ↓ ↓ ↓ ↓
Error: Cannot find module '<PROJECT_ROOT>/src/__tests__/some-macros-that-doesnt-even-need-to-exist.macro' from 'index.js'
Error: Cannot find module './some-macros-that-doesnt-even-need-to-exist.macro' from '<PROJECT_ROOT>/src/__tests__'

This comment has been minimized.

Copy link
@Andarist

Andarist Nov 10, 2018

Contributor

hi @kentcdodds , what's the exact use case that this has solved? it broke one of my tests because it doesn't try to use aliased modules (using https://github.com/ilearnio/module-alias)

This comment has been minimized.

Copy link
@kentcdodds

kentcdodds Nov 10, 2018

Owner

Huh, that package looks like a bit of a hack. I honestly can't remember what the problem was exactly, but it was definitely an issue. Perhaps we could do both? If this doesn't find a module, try the old way?

This comment has been minimized.

Copy link
@Andarist

Andarist Nov 10, 2018

Contributor

Thats an option - although would be good to know the original issue to decide whats the best solution is

For now ive gone with self-linking but thats harder to do right and i always have to search for bash if else syntax to do that 😉

`;
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const p = require('path')
const resolve = require('resolve')
// const printAST = require('ast-pretty-print')

const macrosRegex = /[./]macro(\.js)?$/
Expand Down Expand Up @@ -149,11 +150,10 @@ function applyMacros({path, imports, source, state, babel, interopRequire}) {
{},
)

let requirePath = source
const isRelative = source.indexOf('.') === 0
if (isRelative) {
requirePath = p.join(p.dirname(getFullFilename(filename)), source)
}
const requirePath = resolve.sync(source, {
basedir: p.dirname(getFullFilename(filename)),
})

const macro = interopRequire(requirePath)
if (!macro.isBabelMacro) {
Expand Down

0 comments on commit 1785a0f

Please sign in to comment.