Skip to content

Commit a5ecea3

Browse files
authored
fix resolve issues (#4)
markdoc/markdoc#41
1 parent b50221c commit a5ecea3

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/loader.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ async function gatherPartials(ast, schemaDir) {
3737
// Returning a JSX object is what allows fast refresh to work
3838
async function load(source) {
3939
const logger = this.getLogger('@markdoc/next.js');
40+
const resolve = this.getResolve({
41+
// https://webpack.js.org/api/loaders/#thisgetresolve
42+
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx', '...'],
43+
preferRelative: true,
44+
});
45+
4046
const {mode = 'static', schemaPath = DEFAULT_SCHEMA_PATH} =
4147
this.getOptions() || {};
4248

@@ -120,7 +126,7 @@ async function load(source) {
120126

121127
async function readDir(variable) {
122128
try {
123-
const module = require.resolve(path.join(schemaDir, variable));
129+
const module = await resolve(schemaDir, variable);
124130
return `import * as ${variable} from '${module}'`;
125131
} catch (error) {
126132
return `const ${variable} = {};`;
@@ -155,7 +161,7 @@ import yaml from 'js-yaml';
155161
// renderers is imported separately so Markdoc isn't sent to the client
156162
import Markdoc, {renderers} from '@markdoc/markdoc'
157163
158-
import {getSchema} from '${require.resolve('./runtime')}';
164+
import {getSchema} from '${await resolve(__dirname, './runtime')}';
159165
/**
160166
* Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
161167
* This enables typescript/ESnext support

tests/index.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ function evaluate(output) {
4242
}
4343

4444
function options(config = {}) {
45+
const resolve = async (context, file) =>
46+
require.resolve(path.join(context, file));
4547
const webpackThis = {
4648
context: __dirname,
4749
getOptions() {
@@ -52,14 +54,8 @@ function options(config = {}) {
5254
},
5355
addDependency() {},
5456
addContextDependency() {},
55-
resolve: (context, file, callback) => {
56-
try {
57-
const module = require.resolve(path.join(context, file));
58-
callback(null, module);
59-
} catch (error) {
60-
callback(error);
61-
}
62-
},
57+
getResolve: () => resolve,
58+
resolve,
6359
resourcePath: '/Users/someone/a-next-js-repo/pages/test/index.md',
6460
};
6561

0 commit comments

Comments
 (0)