codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 221 forks source link

karma.conf.js being treated as entrypoint results in many errors from webpack 5 #520

Closed dten closed 10 months ago

dten commented 2 years ago

Expected Behavior

tests run without lots of warning output

Actual Behavior

tests run build includes output such as


ERROR in ./webpack.config.js 3:11-26
Module not found: Error: Can't resolve 'path' in 'C:\Users\David\Documents\Repositories\github.com\karma-webpack-example'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "path": require.resolve("path-browserify") }'
        - install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "path": false }
resolve 'path' in 'C:\Users\David\Documents\Repositories\github.com\karma-webpack-example'
  Parsed request is a module
  using description file: C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules
        single file module
          using description file: C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\package.json (relative path: ./node_modules/path)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules\path doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules\path.js doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules\path.json doesn't exist
            .wasm
              Field 'browser' doesn't contain a valid alias configuration
              C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules\path.wasm doesn't exist
        C:\Users\David\Documents\Repositories\github.com\karma-webpack-example\node_modules\path doesn't exist
      C:\Users\David\Documents\Repositories\github.com\node_modules doesn't exist or is not a directory
      C:\Users\David\Documents\Repositories\node_modules doesn't exist or is not a directory
      C:\Users\David\Documents\node_modules doesn't exist or is not a directory
      C:\Users\David\node_modules doesn't exist or is not a directory
      C:\Users\node_modules doesn't exist or is not a directory
      C:\node_modules doesn't exist or is not a directory
 @ ./test/karma.conf.js 1:20-51

Code

See repro

How Do We Reproduce?

get the repo. npm install && npm run test observe error output

https://github.com/dten/karma-webpack-issue

you can uncommentd stats: 'detailed' in the webpack config to see more output from webpack about the entry points it's passed

Thoughts

This seems to me because karma.conf.js is passed as an entry point to webpack. this means it builds it and then complains that it contains node references that are no longer polyfilled. It suggests adding exclusions but in any complicated webpack config then many plugins are referenced and webpack starts complaining about the whole tree of dependencies. I get the feeling the config file is not supposed to be an entry point

asset runtime.js 6.43 KiB [emitted] (name: runtime)
asset commons.js 3.42 KiB [emitted] (name: commons) (id hint: commons)
asset karma.conf.1792156944.js 1.01 KiB [emitted] (name: karma.conf.1792156944)
asset lib.spec.107182755.js 1.01 KiB [emitted] (name: lib.spec.107182755)
Entrypoint karma.conf.1792156944 10.9 KiB = runtime.js 6.43 KiB commons.js 3.42 KiB karma.conf.1792156944.js 1.01 KiB
Entrypoint lib.spec.107182755 10.9 KiB = runtime.js 6.43 KiB commons.js 3.42 KiB lib.spec.107182755.js 1.01 KiB
dten commented 2 years ago

karma-webpack does not seem to respect the exclude section of the karma config. though I find i can kind of trick it to not being included by setting files as follows files: ['**/!(karma.conf).js'], this resolves the immediate issue, but i think karma-webpack not be including the config file as an entrypoint

dten commented 2 years ago

karma itself always lists the config file in the exclusion list which you can see when passing --log-level debug to karma (in the example below i have not defined exclude but it's there)

01 02 2022 10:24:27.162:DEBUG [karma-server]: Final config Config {
  LOG_DISABLE: 'OFF',
  LOG_ERROR: 'ERROR',
  LOG_WARN: 'WARN',
  LOG_INFO: 'INFO',
  LOG_DEBUG: 'DEBUG',
  frameworks: [ 'jasmine', 'webpack' ],
  protocol: 'http:',
  port: 9876,
  listenAddress: '0.0.0.0',
  hostname: 'localhost',
  httpsServerConfig: {},
  basePath: 'C:/Users/David/Documents/Repositories/github.com/karma-webpack-issue/test',
  files: [
    Pattern {
      pattern: 'C:/Users/David/Documents/Repositories/github.com/karma-webpack-issue/test/**/!(karma.conf).js',
      served: true,
      included: true,
      watched: true,
      nocache: false,
      weight: [ 1, 1, 0, 0, 0, 0 ],
      type: undefined,
      isBinary: undefined
    }
  ],
  browserConsoleLogOptions: { level: 'debug', format: '%b %T: %m', terminal: true },
  customContextFile: null,
  customDebugFile: null,
  customClientContextFile: null,
  exclude: [
    'C:/Users/David/Documents/Repositories/github.com/karma-webpack-issue/test/karma.conf.js'
  ],
  logLevel: 'DEBUG',
  ...