Skip to content

Commit 0ed2bfd

Browse files
authored
Attempt windows fix (#9)
* attempt fix windows issues * try enhanced-resolve-jest * attempt to fix resolution * undo some changes
1 parent b1dc0f5 commit 0ed2bfd

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/loader.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ const Markdoc = require('@markdoc/markdoc');
44

55
const DEFAULT_SCHEMA_PATH = './markdoc';
66

7-
// https://stackoverflow.com/questions/53799385/how-can-i-convert-a-windows-path-to-posix-path-using-node-path
8-
function normalizeAbsolutePath(s) {
9-
return s
10-
.replace(/^[a-zA-Z]:/, '') // replace C: for Windows
11-
.split(path.sep)
12-
.join(path.posix.sep);
13-
}
14-
157
async function gatherPartials(ast, schemaDir) {
168
let partials = {};
179

@@ -24,7 +16,7 @@ async function gatherPartials(ast, schemaDir) {
2416
typeof file === 'string' &&
2517
!partials[file]
2618
) {
27-
const filepath = path.posix.join(schemaDir, file);
19+
const filepath = path.join(schemaDir, file);
2820
// parsing is not done here because then we have to serialize and reload from JSON at runtime
2921
const content = await fs.promises.readFile(filepath, {encoding: 'utf8'});
3022

@@ -45,6 +37,7 @@ async function gatherPartials(ast, schemaDir) {
4537
// Returning a JSX object is what allows fast refresh to work
4638
async function load(source) {
4739
const logger = this.getLogger('@markdoc/next.js');
40+
// https://webpack.js.org/concepts/module-resolution/
4841
const resolve = this.getResolve({
4942
// https://webpack.js.org/api/loaders/#thisgetresolve
5043
extensions: ['.js', '.jsx', '.json', '.ts', '.tsx', '...'],
@@ -54,7 +47,7 @@ async function load(source) {
5447
const {mode = 'static', schemaPath = DEFAULT_SCHEMA_PATH} =
5548
this.getOptions() || {};
5649

57-
const schemaDir = path.posix.resolve(schemaPath || DEFAULT_SCHEMA_PATH);
50+
const schemaDir = path.resolve(schemaPath || DEFAULT_SCHEMA_PATH);
5851

5952
// Grabs the path of the file relative to the `/pages` directory
6053
// to pass into the app props later.
@@ -121,7 +114,7 @@ async function load(source) {
121114
const partials = await gatherPartials.call(
122115
this,
123116
ast,
124-
path.posix.resolve(schemaDir, 'partials')
117+
path.resolve(schemaDir, 'partials')
125118
);
126119

127120
// IDEA: consider making this an option per-page
@@ -134,9 +127,7 @@ async function load(source) {
134127

135128
async function readDir(variable) {
136129
try {
137-
const module = normalizeAbsolutePath(
138-
await resolve(schemaDir, variable)
139-
);
130+
const module = await resolve(schemaDir, variable);
140131
return `import * as ${variable} from '${module}'`;
141132
} catch (error) {
142133
return `const ${variable} = {};`;
@@ -171,9 +162,7 @@ import yaml from 'js-yaml';
171162
// renderers is imported separately so Markdoc isn't sent to the client
172163
import Markdoc, {renderers} from '@markdoc/markdoc'
173164
174-
import {getSchema} from '${normalizeAbsolutePath(
175-
await resolve(__dirname, './runtime')
176-
)}';
165+
import {getSchema} from '${await resolve(__dirname, './runtime')}';
177166
/**
178167
* Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
179168
* This enables typescript/ESnext support

tests/index.test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ const loader = require('../src/loader');
77

88
const source = fs.readFileSync(require.resolve('./fixture.md'), 'utf-8');
99

10-
function normalizeOperatingSystemPaths(s) {
11-
const cwd = process
12-
.cwd()
10+
// https://stackoverflow.com/questions/53799385/how-can-i-convert-a-windows-path-to-posix-path-using-node-path
11+
function normalizeAbsolutePath(s) {
12+
return s
1313
.replace(/^[a-zA-Z]:/, '') // replace C: for Windows
1414
.split(path.sep)
1515
.join(path.posix.sep);
16+
}
1617

17-
return s.replace(cwd, '.').replace(/\\r\\n/g, '\\n');
18+
function normalizeOperatingSystemPaths(s) {
19+
return s
20+
.replace(normalizeAbsolutePath(process.cwd()), '.')
21+
.split(path.sep)
22+
.join(path.posix.sep)
23+
.replace(/\/r\/n/g, '\\n');
1824
}
1925

2026
function evaluate(output) {
@@ -53,7 +59,7 @@ function evaluate(output) {
5359

5460
function options(config = {}) {
5561
const resolve = async (context, file) =>
56-
require.resolve(path.join(context, file));
62+
normalizeAbsolutePath(require.resolve(path.posix.join(context, file)));
5763
const webpackThis = {
5864
context: __dirname,
5965
getOptions() {

0 commit comments

Comments
 (0)