Skip to content

Commit d6f9334

Browse files
authored
Normalize path imports for Windows (#5)
* add windows to build CI * try path.normalize * attempt to remove file extension * try and use posix paths * try to normalize to POSIX paths * revert validation changes * try using .posix everywhere * add normalizeAbsolutePath * fix path require * undo CI changes (for now)
1 parent aa09ab9 commit d6f9334

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/loader.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ 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+
715
async function gatherPartials(ast, schemaDir) {
816
let partials = {};
917

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

@@ -46,7 +54,7 @@ async function load(source) {
4654
const {mode = 'static', schemaPath = DEFAULT_SCHEMA_PATH} =
4755
this.getOptions() || {};
4856

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

5159
// Grabs the path of the file relative to the `/pages` directory
5260
// to pass into the app props later.
@@ -113,7 +121,7 @@ async function load(source) {
113121
const partials = await gatherPartials.call(
114122
this,
115123
ast,
116-
path.resolve(schemaDir, 'partials')
124+
path.posix.resolve(schemaDir, 'partials')
117125
);
118126

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

127135
async function readDir(variable) {
128136
try {
129-
const module = await resolve(schemaDir, variable);
137+
const module = normalizeAbsolutePath(
138+
await resolve(schemaDir, variable)
139+
);
130140
return `import * as ${variable} from '${module}'`;
131141
} catch (error) {
132142
return `const ${variable} = {};`;
@@ -161,7 +171,9 @@ import yaml from 'js-yaml';
161171
// renderers is imported separately so Markdoc isn't sent to the client
162172
import Markdoc, {renderers} from '@markdoc/markdoc'
163173
164-
import {getSchema} from '${await resolve(__dirname, './runtime')}';
174+
import {getSchema} from '${normalizeAbsolutePath(
175+
await resolve(__dirname, './runtime')
176+
)}';
165177
/**
166178
* Schema is imported like this so end-user's code is compiled using build-in babel/webpack configs.
167179
* This enables typescript/ESnext support

0 commit comments

Comments
 (0)