Skip to content

mdx.macro generating invalid documentPath on windows #67

@Zemnmez

Description

@Zemnmez

Using mdx.macro on Windows gets me:

ERROR in C:/Users/thoma/OneDrive/Documents/devel/linear/node_modules/.cache/mdx.macro/icloud.c3ec31aebc.mdx.js
Module not found: Error: Can't resolve 'C:Users homaOneDriveDocumentsdevellinearpackagesarticleicloud.mdx' in 'C:\Users\thoma\OneDrive\Documents\devel\linear\node_modules\.cache\mdx.macro'

I tracked this down to a generated line that looks like this (in the cache):

import React from 'react';
import { MDXTag } from '@mdx-js/tag';
import "C:Users\thomaOneDriveDocumentsdevellinearpackagesarticlehow-to-hack-icloudicloud.mdx"; // here
export default (function (_ref) {
  var components = _ref.components,
      props = _objectWithoutProperties(_ref, ["components"]);

It looks like some part of mdx.macro is incorrectly processing windows slashes as escapes.

I'm guessing that what is happening here is that mdx.macro generates some code via concatenation, rather than via AST manipulation, so this line is generated by:

https://github.com/frontarm/mdx-util/blob/master/packages/mdx.macro/mdx.macro.js#L123

    imports += `import '${documentPath}'\n`

then converted to an AST and transformed here, like this:
https://github.com/frontarm/mdx-util/blob/master/packages/mdx.macro/mdx.macro.js#L129

  let transformedSource =
    babel.transformSync(
      imports+mdx.sync(source),
      {
        presets: [babelPresetReactApp],
        filename: documentPath,
      },
    ).code

I imagine what's going on here is that injecting the import via string concatenation on Windows is giving us something like:

import "c:\path\to\file"

which, after transformation by babel ends up being:

import "c:path\tofile"

The absolute quickest and dirtiest we can do here is just to escape all \.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions