GithubHelp home page GithubHelp logo

Webpack build: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted about touchsweep HOT 9 CLOSED

scriptex avatar scriptex commented on May 21, 2024
Webpack build: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

from touchsweep.

Comments (9)

dakur avatar dakur commented on May 21, 2024

On reddit, there is this error: https://www.reddit.com/r/typescript/comments/4q09uv/help_webpack_weirdness_require_function_is_used/

According to some comment there, the reason might be that there is module: "umd" is tsconfig.json instead of module: "commonjs". But I'm not quite familiar with js packages export settings, so I don't know what it actually does and what are the consequences. πŸ€·β€β™‚οΈ

from touchsweep.

scriptex avatar scriptex commented on May 21, 2024

Hi, @dakur, is it possible to paste your webpack config or at least provide a minimal reproducible example?

from touchsweep.

dakur avatar dakur commented on May 21, 2024

Hi @scriptex, thank you for your answer. I'm able to reproduce it with this minimal config:

module.exports = {
	entry: './index.js',
}

I've also removed .babelrc – even though there is no babel loader – to be sure that no other lib beside webpack is in effect with some magic. ✨

JS:

import TouchSweep from 'touchsweep';

new TouchSweep(document.getElementById('test'))

from touchsweep.

scriptex avatar scriptex commented on May 21, 2024

πŸ€” I was able to reproduce this and to be honest, I am not sure why this happens.
It looks like Webpack doesn't like UMD produced by the TypeScript compiler...
Can you try adding the following to your Webpack config:

module: {
  unknownContextCritical: false,
},

Webpack's docs do not say much about this configuration setting, though.

from touchsweep.

dakur avatar dakur commented on May 21, 2024

Yes, unknownContextCritical: false does the job, but.. πŸ™‚

The aforementioned reddit thread wasn't helpful? It seems to me that this could be the problem:

You need to tell Typescript to emit CommonJS, not UMD. UMD is doing funky stuff with the require function, passing it as an argument into factory. This means webpack can't look at all usages of the top-level require function and figure out exactly what modules you're loading.

It well corresponds to code in dist/ folder of touchsweep installed into my node_modules/ with v2:

from touchsweep.

scriptex avatar scriptex commented on May 21, 2024

Okay, looks like I need to change the way the module is built. I guess I will try Vite or Rollup but I don’t have the time right now.
How critical is your situation? πŸ˜…

from touchsweep.

dakur avatar dakur commented on May 21, 2024

Not at all, I'll wait. Thank you very much!

from touchsweep.

scriptex avatar scriptex commented on May 21, 2024

@dakur I've released a new version (2.0.1) on NPM and on Github.
The new release uses Rollup to build and (according to my tests) fixes the Webpack warning.
Please check and let me know πŸ™‚
Thank you for your contribution!

from touchsweep.

dakur avatar dakur commented on May 21, 2024

Yes, that works very well now, thank you very much!

from touchsweep.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.