posthtml / posthtml-modules

Modules Plugin
MIT License
84 stars 11 forks source link

Root options is ignored #81

Closed thewebartisan7 closed 2 years ago

thewebartisan7 commented 2 years ago

In my webpack I have set root, but it's completely ignored, and only adding full path works fine. The root path is set also for extends and includes module and works fine.

This is my webpack:

{
            loader: "posthtml-loader",
            options: {
              //ident: 'posthtml',
              //parser: 'PostHTML Parser',
              plugins: [
                /* PostHTML Plugins */
                require('posthtml-extend')({
                  encoding: 'utf8', // Parent template encoding
                  root: './src/templates/', // Path to parent template directory
                  strict: false, // Required for nested extends to works
                  //skipParse: false,
                }),
                require('posthtml-include')({
                  encoding: 'utf8',
                  root: './src/templates/', // Path to parent template directory
                }),
                require('posthtml-modules')({
                  //encoding: 'utf8',
                  root: './src/templates/', // Path to parent template directory
                  //tag: 'component',
                  //attribute: 'path',
                  attributeAsLocals: true
                })
              ]
            }
thewebartisan7 commented 2 years ago

Checking source code seem that here is the problem:

  const filePath = path.join(path.isAbsolute(href) ? options.root : path.dirname(options.from), href);

The href must have a starting slash like /path/to/module.html otherwise path.isAbsolute(href) will return false.

So root must not have slash, example:

root: './src/templates',

When I try to set from like:

from: __dirname + '/src/templates/',

Then root become ./src/ and not ./src/templates/ then I need to include module using also templates example templates/path/to/module.html

Root in this case is ignored.

Would be to review below, like other extends and include. I am not sure why there is both root and from.

  const filePath = path.join(path.isAbsolute(href) ? options.root : options.from, href);

Anyway solved.