atomicpages / docusaurus-plugin-module-alias

A tiny docusaurus 2 plugin for easy module alias creation
MIT License
7 stars 2 forks source link

Using aliases in imported markdown files doesn't work(?) #9

Open verlakasalt opened 2 years ago

verlakasalt commented 2 years ago

Hi,

I'm trying to use custom aliases in imported mdx-files, which doesn't seem to work.

Example: I have an mdx file that imports another mdx file, which also imports another mdx file, and I want to replace parts of the path to the imported file or a component inside that file with an alias. Aliases used in the initial mdx file work as expected, but the same alias doesn't work when it is used in an mdx file the initial file includes.

Docusaurus says: Module not found: Error: Can't resolve '@os.compos/os.features.js' in 'C:\Users(...)

Is this something that should work and I need to do something extra in imported files, or was it just missed when developing this plugin? :)

atomicpages commented 2 years ago

👋 under the hood the plugin uses webpack aliases which works by replacing imports with the configured path. Anything valid with webpack aliases is also valid here.

@os.compos needs to map to a specific valid directory:

module.exports = {
  // ...
  plugins: [
    [
      "docusaurus-plugin-module-alias",
      {
        alias: {
          "@os.compos": path.resolve(__dirname, "../src/components/"),
        },
      },
    ],
  ],
};
verlakasalt commented 2 years ago

Thanks for the quick reply!

I tried being clever and did the following:

'@os.compos': path.join('/', '/src/components'),
'@os.comp': path.resolve(__dirname, "../src/components/"),

Which results in:

"@os.compos": "\\src\\components",
"@os.comp": "C:\\Users\\[blah]\\docusaurus\\src\\components",

But even when using the latter alias it results in a "module not found" error ("Module not found: Error: Can't resolve '@os.comp/os.features.js' in 'C:\Users...).

This is my import line: import EditButtonR from '@os.comp/os.features.js' Which sits in a 'partial' _document that is also imported.

Both aliases work in md/mdx files that are not partials!

I'm using the latest version of Docusaurus (2.0.0-beta.13). Are there any logs I could check to figure out what Docusaurus does with those aliases in partials?