javascript PhpStorm抱怨说“模块未安装”

3lxsmp7m  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(99)

在将Encore升级到4.2.0版本(从^3.0)后,PhpStorm开始抱怨主题中的错误。
这是我的webpack.config.js文件:

/* 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;

如您所见,我配置了要添加到配置对象中的modules键的文件夹assets
在升级之前,这是有效的,我能够通过单击import语句在文件之间导航。
现在,PhpStorm却抱怨模块没有安装。
没有完整的错误,只有PhpStorm的抱怨。

问题只出在PhpStorm上。
事实上,如果我运行yarn encore dev,源代码会正确编译,没有任何错误。
关于如何使模块解析再次工作有什么想法吗?

    • 附言**

我也在GitHub上开了一期,但没有任何运气:https://github.com/symfony/webpack-encore/issues/1189

7qhs6swi

7qhs6swi1#

解决了!问题是在PhpStorm中标记目录。
assets文件夹标记为“源”解决了该问题。

相关问题