dc7290 / template-ejs-loader

ejs-loader with webpack5 support. Chain it to html-loader and use it with html-webpack-plugin.
MIT License
24 stars 3 forks source link

fix: originalIncluderで名前解決を行うときにDependencyに追加されない不具合 #28

Closed kajikentaro closed 2 years ago

kajikentaro commented 2 years ago

Description

webpack.config.jsのオプションでfilenameを書き換えて返却した場合にエラーが起きる。 addDependencyがoriginalIncluderの名前解決結果を行うより先に、parsedPathによって実行されているため

Reproduce

https://github.com/kajikentaro/webpack-ejs-html/releases/tag/PR-template-ejs-loader 上記をクローンし、npm i, npm run devを順に実行する 該当コード

          {

            loader: "template-ejs-loader",
            options: {
              includer: (originalPath, parsedPath) => {
                let filename = "";
                if (/^\./.test(originalPath)) {
                  // includeでパスが'.'から始まる場合
                  filename = parsedPath;
                } else if (/^\//.test(originalPath)) {
                  // includeでパスが'/'から始まる場合
                  filename = path.resolve(__dirname, "src", "." + originalPath);
                } else {
                  console.log(originalPath, parsedPath);
                  filename = path.resolve(__dirname, "src", originalPath);
                }

                if (filename && fs.existsSync(filename)) {
                  return { filename };
                }
                // ファイルが存在しない場合
                throw new Error("Not Found: could not resolve " + originalPath);
              }
            }
          }

エラー

  Child HtmlWebpackCompiler compiled with 1 warning

WARNING in ./src/sub/index.ejs
Invalid dependencies have been reported by plugins or loaders for this module. All reported dependencies need to be absolute paths.
Invalid dependencies may lead to broken watching and caching.
As best effort we try to convert all invalid values to absolute paths and converting globs into context dependencies, but this is deprecated behavior.
Loaders: Pass absolute paths to this.addDependency (existing files), this.addMissingDependency (not existing files), and this.addContextDependency (directories).
Plugins: Pass absolute paths to fileDependencies (existing files), missingDependencies (not existing files), and contextDependencies (directories).
Globs: They are not supported. Pass absolute path to the directory as context dependencies.
The following invalid values have been reported:
 * undefined

Type of change

How Has This Been Tested?

ローカル環境でエラーが消えることを確認

Checklist:

dc7290 commented 2 years ago

@kajikentaro ありがとうございます! LGTMです!