webdiscus / pug-loader

Pug loader for Webpack renders pug to HTML or template function
https://webdiscus.github.io/pug-loader/pug-filters
ISC License
72 stars 5 forks source link

Support Webpack defined modules #18

Closed m10 closed 2 years ago

m10 commented 2 years ago

Modules defined in the Webpack configuration don't get resolved properly.

Folder structure:

src
  ├─ img
  |   ├─ image.jpg
  ├─ css
  ├─ js
  ├─ pug

Webpack config:

resolve: {
  modules: [
    'src',
    'node_modules',
  ],
},

Pug:

img(src=require('img/image.jpg') alt)

Error:

ERROR in   Error: Child compilation failed:
  Module build failed (from ./node_modules/@webdiscus/pug-loader/src/index.js):
  NonErrorEmittedError: (Emitted value instead of an instance of Error)
  [pug-loader] Pug compilation failed.
  PugLoaderException:
  [pug-loader] the file 'img/image.jpg' can't be resolved in the pug template:
  /path/to/project/src/pug/index.pug
  Error: Can't resolve 'img/image.jpg' in '/path/to/project/src/pug/'
      at processResult (/path/to/project/node_modules/webpack/lib/NormalModule.js:755:12)
      at /path/to/project/node_modules/webpack/lib/NormalModule.js:860:5
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:400:11
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:252:18
      at context.callback (/path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:124:13)
      at /path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:284:21
      at Object.compile (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:244:5)
      at Object.module.exports (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:283:11)
  ModuleBuildError: Module build failed (from ./node_modules/@webdiscus/pug-loader/src/index.js):
  NonErrorEmittedError: (Emitted value instead of an instance of Error)
  [pug-loader] Pug compilation failed.
  PugLoaderException:
  [pug-loader] the file 'img/image.jpg' can't be resolved in the pug template:
  /path/to/project/src/pug/index.pug
  Error: Can't resolve 'img/image.jpg' in '/path/to/project/src/pug/'
      at processResult (/path/to/project/node_modules/webpack/lib/NormalModule.js:755:12)
      at /path/to/project/node_modules/webpack/lib/NormalModule.js:860:5
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:400:11
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:252:18
      at context.callback (/path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:124:13)
      at /path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:284:21
      at Object.compile (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:244:5)
      at Object.module.exports (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:283:11)
      at processResult (/path/to/project/node_modules/webpack/lib/NormalModule.js:758:19)
      at /path/to/project/node_modules/webpack/lib/NormalModule.js:860:5
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:400:11
      at /path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:252:18
      at context.callback (/path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:124:13)
      at /path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:284:21
      at Object.compile (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:244:5)
      at Object.module.exports (/path/to/project/node_modules/@webdiscus/pug-loader/src/index.js:283:11)
      at LOADER_EXECUTION (/path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:132:14)
      at runSyncOrAsync (/path/to/project/node_modules/loader-runner/lib/LoaderRunner.js:133:4)

  - NormalModule.js:755 processResult
    [project-name]/[webpack]/lib/NormalModule.js:755:12

  - NormalModule.js:860
    [project-name]/[webpack]/lib/NormalModule.js:860:5

  - LoaderRunner.js:400
    [project-name]/[loader-runner]/lib/LoaderRunner.js:400:11

  - LoaderRunner.js:252
    [project-name]/[loader-runner]/lib/LoaderRunner.js:252:18

  - LoaderRunner.js:124 context.callback
    [project-name]/[loader-runner]/lib/LoaderRunner.js:124:13

  - index.js:284
    [project-name]/[@webdiscus]/pug-loader/src/index.js:284:21

  - index.js:244 Object.compile
    [project-name]/[@webdiscus]/pug-loader/src/index.js:244:5

  - index.js:283 Object.module.exports
    [project-name]/[@webdiscus]/pug-loader/src/index.js:283:11

  - ModuleBuildError: Module build failed (from ./node_modules/@webdiscus/pug-loader/src/index.js):

  - NonErrorEmittedError: (Emitted value instead of an instance of Error)

  - [pug-loader] Pug compilation failed.

  - PugLoaderException:

  - [pug-loader] the file 'img/image.jpg' can't be resolved in the pug template:

  - /path/to/project/src/pug/index.pug

  - Error: Can't resolve 'img/image.jpg' in '/path/to/project/src/pug/'

  - NormalModule.js:755 processResult
    [project-name]/[webpack]/lib/NormalModule.js:755:12

  - NormalModule.js:860
    [project-name]/[webpack]/lib/NormalModule.js:860:5

  - LoaderRunner.js:400
    [project-name]/[loader-runner]/lib/LoaderRunner.js:400:11

  - LoaderRunner.js:252
    [project-name]/[loader-runner]/lib/LoaderRunner.js:252:18

  - LoaderRunner.js:124 context.callback
    [project-name]/[loader-runner]/lib/LoaderRunner.js:124:13

  - index.js:284
    [project-name]/[@webdiscus]/pug-loader/src/index.js:284:21

  - index.js:244 Object.compile
    [project-name]/[@webdiscus]/pug-loader/src/index.js:244:5

  - index.js:283 Object.module.exports
    [project-name]/[@webdiscus]/pug-loader/src/index.js:283:11

  - NormalModule.js:758 processResult
    [project-name]/[webpack]/lib/NormalModule.js:758:19

  - NormalModule.js:860
    [project-name]/[webpack]/lib/NormalModule.js:860:5

  - LoaderRunner.js:400
    [project-name]/[loader-runner]/lib/LoaderRunner.js:400:11

  - LoaderRunner.js:252
    [project-name]/[loader-runner]/lib/LoaderRunner.js:252:18

  - LoaderRunner.js:124 context.callback
    [project-name]/[loader-runner]/lib/LoaderRunner.js:124:13

  - index.js:284
    [project-name]/[@webdiscus]/pug-loader/src/index.js:284:21

  - index.js:244 Object.compile
    [project-name]/[@webdiscus]/pug-loader/src/index.js:244:5

  - index.js:283 Object.module.exports
    [project-name]/[@webdiscus]/pug-loader/src/index.js:283:11

  - LoaderRunner.js:132 LOADER_EXECUTION
    [project-name]/[loader-runner]/lib/LoaderRunner.js:132:14

  - LoaderRunner.js:133 runSyncOrAsync
    [project-name]/[loader-runner]/lib/LoaderRunner.js:133:4

  - child-compiler.js:169
    [project-name]/[html-webpack-plugin]/lib/child-compiler.js:169:18

  - Compiler.js:551 finalCallback
    [project-name]/[webpack]/lib/Compiler.js:551:5

  - Compiler.js:577
    [project-name]/[webpack]/lib/Compiler.js:577:11

  - Compiler.js:1196
    [project-name]/[webpack]/lib/Compiler.js:1196:17

  - Hook.js:18 Hook.CALL_ASYNC_DELEGATE [as _callAsync]
    [project-name]/[tapable]/lib/Hook.js:18:14

  - Compiler.js:1192
    [project-name]/[webpack]/lib/Compiler.js:1192:33

  - Compilation.js:2787 finalCallback
    [project-name]/[webpack]/lib/Compilation.js:2787:11

  - Compilation.js:3092
    [project-name]/[webpack]/lib/Compilation.js:3092:11

webpack compiled with 1 error

in simple-pug-loader and scss (sass-loader / css-loader) this does work correctly

related: https://github.com/webdiscus/pug-loader/issues/15

webdiscus commented 2 years ago

@m10

you can use Webpack aliases, e.g.:

  resolve: {
    alias: {
      Views: path.join(__dirname, 'src/views/'),
      Images: path.join(__dirname, 'src/assets/images/'),
      Fonts: path.join(__dirname, 'src/assets/fonts/'),
      Styles: path.join(__dirname, 'src/assets/styles/'),
      Scripts: path.join(__dirname, 'src/assets/scripts/'),
    },
  },

Usage of aliases in Pug

extends Views/layouts/default
include Views/mixins
img(src=require('Images/image.jpg'))
- const data = require('Scripts/data.json')

Usage of aliases in SCSS

@font-face {
  font-family: 'OpenSans';
  src:
    url('Fonts/OpenSans/open-sans-regular.svg') format('svg'),
    url('Fonts/OpenSans/open-sans-regular.woff2') format('woff2');
  font-style: normal;
}

.header {
  background-image: url('Images/image.jpg');
}

P.S. I'm not sure if supporting resolve.modules is important. I have set this enhancement to a low priority and do it in my free time.

m10 commented 2 years ago

thanks for taking the time to look into it. I appreciate your effort!

I know that one can use aliases too but when adding/removing folders one would have to add/remove aliases too.. It is more dynamic and intuitive with modules.

Just IMHO: Dependency resolution should work consistently across Webpack. It's what users expect. Any difference should at least be documented.

We have legacy projects where this is used. Migrating them to this loader would be more difficult without this support. On new projects it would be possible to work around this, even though it would be nice to keep the workflow

webdiscus commented 2 years ago

@m10

from readme > features:

...

... and here are many examples of resolving Currently is supported what is documented ;-)

Many of my and many client projects have never never used the resolve.module. If support is required now, then well, I will do it.

webdiscus commented 2 years ago

@m10

In v2.6.4 is enabled supports for resolve.modules options defined in webpack config.

P.S. all performance optimised pre-settings of Webpack resolver was removed. Now you can change every options of Webpack resolver.