From 1785a0fab2c374d1505c49cff1bd38fd541819e2 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Fri, 21 Sep 2018 14:32:52 -0600 Subject: [PATCH] fix: require files relative to the source file --- package.json | 3 ++- src/__tests__/__snapshots__/index.js.snap | 2 +- src/index.js | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 16c160c..e924de0 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "author": "Kent C. Dodds (http://kentcdodds.com/)", "license": "MIT", "dependencies": { - "cosmiconfig": "^5.0.5" + "cosmiconfig": "^5.0.5", + "resolve": "^1.8.1" }, "devDependencies": { "@babel/core": "^7.1.0", diff --git a/src/__tests__/__snapshots__/index.js.snap b/src/__tests__/__snapshots__/index.js.snap index 03adb73..dfe08fd 100644 --- a/src/__tests__/__snapshots__/index.js.snap +++ b/src/__tests__/__snapshots__/index.js.snap @@ -149,7 +149,7 @@ export default 'something else' ↓ ↓ ↓ ↓ ↓ ↓ -Error: Cannot find module '/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 '/src/__tests__' `; diff --git a/src/index.js b/src/index.js index 31d8ebb..2faf5eb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const p = require('path') +const resolve = require('resolve') // const printAST = require('ast-pretty-print') const macrosRegex = /[./]macro(\.js)?$/ @@ -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) {