javascriptsymfonywebpackphpstormwebpack-encore

PhpStorm complains saying "Module is not installed"


After having upgraded Encore to the version 4.2.0 (from ^3.0), PhpStorm started to complain with the error in the subject.

This is my webpack.config.js file:

/* eslint-disable no-param-reassign */
const Encore = require('@symfony/webpack-encore');
const path = require('path');

const assetsFolder = './assets';

// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
    Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}

Encore
  // directory where compiled assets will be stored
  .setOutputPath('public/build/')
  // public path used by the web server to access the output path
  .setPublicPath('/build')
  // only needed for CDN's or sub-directory deploy
  // .setManifestKeyPrefix('build/')

  /*
   * ENTRY CONFIG
   *
   * Add 1 entry for each "page" of your app
   * (including one that's included on every page - e.g. "app")
   *
   * Each entry will result in one JavaScript file (e.g. app.js)
   * and one CSS file (e.g. app.css) if your JavaScript imports CSS.
   */
  // will output as public/build/App.js
  .addEntry('App', `${assetsFolder}/App.jsx`)
  .addEntry('PaceLoader', `${assetsFolder}/App/Utilities/pace-loader/pace-loader.js`)

  // will output as public/build/styles.css
  .addStyleEntry('styles', `${assetsFolder}/App.scss`)

  // When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
  // ! ! ! DO NOT ACTIVATE THIS: IT BREAKS BOOTSTRAP's JS ! ! !
  // .splitEntryChunks()

  // will require an extra script tag for runtime.js
  // but, you probably want this, unless you're building a single-page app
  .disableSingleRuntimeChunk()

  /*
   * FEATURE CONFIG
   *
   * Enable & configure other features below. For a full
   * list of features, see:
   * https://symfony.com/doc/current/frontend.html#adding-more-features
   */
  .cleanupOutputBeforeBuild()
  .enableBuildNotifications()
  .enableSourceMaps(!Encore.isProduction())
  // enables hashed filenames (e.g. app.abc123.css)
  .enableVersioning(true)

  // enables @babel/preset-env polyfills
  .configureBabelPresetEnv((config) => {
    config.useBuiltIns = 'usage';
    config.corejs = 3;
  })

  // @todo Verify it is actual
  // This resolves images directly to their URL instead of using a ES module for them.
  // https://github.com/symfony/webpack-encore/issues/743#issuecomment-622294667
  //
  // This was then changed during the big update on 2021/02/05
  // https://github.com/symfony/webpack-encore/issues/808#issuecomment-771862097
  .configureManifestPlugin(function(options) {
    options.removeKeyHash = /(?<=\.)([a-f0-9]{8}){1,4}\./;
  })

  // enables Sass/SCSS support
  .enableSassLoader()

  // uncomment if you use TypeScript
  // .enableTypeScriptLoader()

  // uncomment to get integrity="..." attributes on your script & link tags
  // requires WebpackEncoreBundle 1.4 or higher
  // .enableIntegrityHashes(Encore.isProduction())

  // uncomment if you're having problems with a jQuery plugin
  // .autoProvidejQuery()

  // uncomment if you use API Platform Admin (composer req api-admin)
  .enableReactPreset()
  // .addEntry('admin', './assets/js/admin.js')

  .configureImageRule({
    filename: '[path][name].[hash:8].[ext]',
  });

// Now manually modify the generated config
const config = Encore.getWebpackConfig();

// Set the absolute path in which search for files when using "require()"
// https://stackoverflow.com/a/36574982/1399706
config.resolve.modules = [path.resolve(__dirname, 'assets'), 'node_modules'];

// This is required to make Pac.js be importable
// https://github.com/HubSpot/pace/issues/328#issuecomment-304499584
config.resolve.alias = { pace: 'pace-progress' };

config.stats = {
  warnings: true
};

// export the final configuration
module.exports = config;

As you can see, I configure the the folder assets to be added to the modules key in the configuration object.

Before upgrading, this worked and I was able to navigate between files clicking on the import statement.

Now, instead, PhpStorm complains the module is not installed.

There is no full error, but only a complain by PhpStorm.

enter image description here

The problem is only with PhpStorm.

In fact, if I run yarn encore dev the source compiles correctly, without any error.

Any idea about how to make the modules resolution work again?

PS
I also opened an issue on GitHub, but without any luck: https://github.com/symfony/webpack-encore/issues/1189


Solution

  • Solved! The problem was the marking of directories in PhpStorm.

    Marking the assets folder as "source" solved the issue.

    enter image description here