简体   繁体   English

Webpack Encore:无法导入本地依赖项

[英]Webpack Encore: cannot import local dependencies

I want to add an entry for a JS file which in turn imports several other JS files.我想为一个 JS 文件添加一个条目,该文件又会导入其他几个 JS 文件。

However when I run encore I get a "This dependency was not found" error.但是,当我运行 encore 时,出现“找不到此依赖项”错误。

webpack.config.js webpack.config.js

...

.addEntry('app', './theme/js/scripts.js')

...

./theme/js/scripts.js ./theme/js/scripts.js

import * as Site from 'Site'

$(() => {
  Site.run()
})

./theme/es/Site.js ./theme/es/Site.js

import $ from 'jquery';
import Base from 'Base';
import Menubar from 'Menubar';
import Sidebar from 'Sidebar';
import PageAside from 'PageAside';

... more code

The error that I am getting is我得到的错误是

This dependency was not found:未找到此依赖项:

  • Site in ./theme/js/scripts.js ./theme/js/scripts.js 中的站点

To install it, you can run: npm install --save Site要安装它,您可以运行: npm install --save Site

I don't think it can be "installed" as this is a dependency which is part of a theme that I have.我不认为它可以“安装”,因为这是我拥有的主题的一部分的依赖项。

Unfortunately I cannot change the code in any of the files in ./theme So I am wondering if there a way to load the dependencies in Webpack Encore?不幸的是,我无法更改 ./theme 中任何文件中的代码所以我想知道是否有办法在 Webpack Encore 中加载依赖项?

In the traditional webpack config I would have done the following:在传统的 webpack 配置中,我会执行以下操作:

module.exports = {
  resolve: {
    extensions: ['.js'],
    alias: {
      Base: path.resolve(config.scripts.source, 'Base.js'),
      Site: path.resolve(config.scripts.source, 'Site.js'),
      ....
    }
  },

But how can I import these with Webpack Encore?但是我怎样才能用 Webpack Encore 导入这些呢?

It seems like that at the current moment webpack encore does not offer any syntax for adding helper functions.目前看来 webpack encore 没有提供任何添加辅助函数的语法。

Based on the comments here https://github.com/symfony/webpack-encore/issues/37 the solution would be following:根据这里的评论https://github.com/symfony/webpack-encore/issues/37 ,解决方案如下:

webpack.config.js webpack.config.js

let config = Encore.getWebpackConfig();

config.resolve = {
    extensions: ['.js'],
    alias: {
        Base: path.resolve(__dirname, './remark/es/Base.js'),
        Site: path.resolve(__dirname, './remark/es/Site.js'),
        ...
    }
};

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

In ES6 you can use module.在 ES6 中,您可以使用模块。 The solution is to utilize ES6's syntax, import and export statements, an elegant and maintainable approach that allows us to keep things separated, and only available when we need it.解决方案是利用 ES6 的语法、import 和 export 语句,这是一种优雅且可维护的方法,使我们能够将事物分开,并且仅在需要时才可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM