简体   繁体   English

Webpack / NPM:使用已安装模块的构建版本,而不是从源代码重新构建

[英]Webpack / NPM: Use build version of installed module instead of re-building from source

I would like to use the dat.GUI library for a project that's build with Webpack 2. If I install the module via npm -install --save-dev dat.gui and then try to import it using import * as DAT from 'dat.gui'; 我想将dat.GUI库用于使用Webpack 2构建的项目。如果我通过npm -install --save-dev dat.gui安装模块,然后尝试使用import * as DAT from 'dat.gui';导入它import * as DAT from 'dat.gui'; I get the following error when Webpack is trying to compile my project: 当Webpack尝试编译我的项目时,我收到以下错误:

ERROR in ./~/dat.gui/src/dat/controllers/NumberControllerSlider.js
Module not found: Error: Can't resolve 'style' in 
'/home/me/myProject/node_modules/dat.gui/src/dat/controllers'
BREAKING CHANGE: It's no longer allowed to omit the '-loader' suffix 
when using loaders.

I know this error occurs when using Webpack 2 to build Webpack 1 based projects. 我知道使用Webpack 2构建基于Webpack 1的项目时会发生此错误。 But why is Webpack even trying to build the module if there already is a build version inside node_modules/dat.gui/build'; 但是,如果在node_modules/dat.gui/build';已经存在构建版本,为什么Webpack甚至会尝试构建模块node_modules/dat.gui/build'; ? Is there a way to tell Webpack or NPM to use the existing build version without trying to re-build it? 有没有办法告诉Webpack或NPM使用现有的构建版本而不尝试重新构建它?

When importing a node module, webpack looks into its package.json and uses the main field as entry of the module, similar to what Node.js does (webpack looks for more fields by default, see resolve.mainFields ). 导入节点模块时,webpack会查看其package.json并使用main字段作为模块的条目,类似于Node.js的操作(webpack默认查找更多字段,请参阅resolve.mainFields )。

Since for dat.gui the main field does not point to the built version but to the source, which actually inlines loaders as seen in dat.gui@0.6.1 - NumberControllerSlider.js for the styleSheet import, and that is not a good idea in general and certainly not to publish. 由于对dat.gui的主要领域没有指向内置版本,但该人士介绍,在看到这实际上内联装载机dat.gui@0.6.1 - NumberControllerSlider.jsstyleSheet进口,这是不是一个好主意一般而言肯定不会发表。

But you can import the built version by specifying the corresponding path. 但您可以通过指定相应的路径来导入构建的版本。 So your import would be: 所以你的导入将是:

import * as DAT from 'dat.gui/build/dat.gui.js';

If you'd like to still import just dat.gui you can configure resolve.alias to point to the built version as follows: 如果您想还是只导入dat.gui可以配置resolve.alias指向内置的版本如下:

resolve: {
  alias: {
    'dat.gui': 'dat.gui/build/dat.gui.js'
  }
}

With that you can use your original import statement: 有了它,您可以使用原始的import语句:

import * as DAT from 'dat.gui';

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

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