question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack.ProvidePlugin not working with preact.config.js

See original GitHub issue

EDIT: this is a preact issue, I messed up.

Do you want to request a feature or report a bug? bug or wrong configuration on my side

What is the current behaviour? Trying to provide a plugin with webpack.ProvidePlugin though the preact.config.js I get an error on runtime: ReferenceError: PIXI is not defined even though the package is installed correctly.

If the current behaviour is a bug, please provide the steps to reproduce. Here’s the configuration:

webpack(config, env, helpers, options) {
  //...
  options = {
    plugins: [
      new webpack.ProvidePlugin({
        PIXI: 'pixi.js',
      }),
    ],
  }
}

Install both pixi.js and pixi-layersdependencies, pixi-layers depends on pixi.js to be available to work:

import * as PIXI from "pixi.js";
import "pixi-layers";

What is the expected behaviour? I expect this config to work the same as an earlier version of my project (before switching to preact):

module.exports = {
  webpack: {
    plugins: [
      new webpack.ProvidePlugin({
        PIXI: 'pixi.js',
      }),
    ],
  },
};

This worked fine and provided pixi as expected.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rschristiancommented, May 29, 2020

I’m still wondering why I can’t simply provide the plugin like in my other webpack config but I guess this will have to do for now.

I’m not sure what your config file looks like, but this can help provide context if you’re missing parameter descriptions, and this can help by showing some basic configurations.

You can’t provide the plugin like you can in a Webpack config because this isn’t a Webpack config. In your preact.config.js, you write rules that will overwrite the actual Webpack config file that’s hidden away. Most people do not like manually dealing with Webpack, as it can be quite the monster, so it is hidden away until when you want to make changes to it.

Had you targeted the right item (helpers.getPluginsByName(config, 'ProvidePlugin')[0].plugin.definitions) you would have overwritten the existing configs, likely breaking many things. The CLI needs its own skew of plugins to operate correctly, so that’s why we instead just appended to a new value to the ProvidePlugin, rather than reassigning it.

Glad you got it working though. Hope those warnings go away.

1reaction
rschristiancommented, May 29, 2020

This should work:

const providePlugin = helpers.getPluginsByName(config, 'ProvidePlugin')[0];
providePlugin.plugin.definitions['PIXI'] = 'pixi.js';

I’m not sure what the rendering should look like, but what I’m seeing is a V shape with the left half being of one image I provided, the right half being the other.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ProvidePlugin with preact config - webpack - Stack Overflow
This returns ReferenceError: PIXI is not defined at runtime, which is the error I got in my early project when I wasn't providing...
Read more >
ProvidePlugin - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
Consuming Packages - SurviveJS
Webpack can consume most npm packages without a problem. Sometimes, though, patching is required using webpack's resolution mechanism. To recap: Use webpack's ......
Read more >
Switching to Preact (from React)
Simply add the following resolve.alias configuration to your webpack.config.js : { "resolve": { "alias": { "react": "preact-compat", "react-dom": ...
Read more >
Custom Webpack Config - Next.js
Note: changes to webpack config are not covered by semver so proceed at your own risk. Before continuing to add custom webpack configuration...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found