简体   繁体   English

在库名称中使用“@”将依赖项添加到外部

[英]Add dependency to externals with “@” in the library name

I'm using ngx-build-plus to package my angular element with some webpack extra config.我正在使用 ngx-build-plus 到 package 我的 angular 元素和一些 webpack 额外配置。 In my webpack extra config, I want to exclude a dependency whose name starts with the "@", like @angular/core.在我的 webpack 额外配置中,我想排除名称以“@”开头的依赖项,例如 @angular/core。 I know that @angular/core can be exclude by mapping it to ng.core, but for the library I'm trying to use I don't think I can link it using ng since its not an angular dependency.我知道可以通过将 @angular/core 映射到 ng.core 来排除它,但是对于我尝试使用的库,我认为我不能使用 ng 链接它,因为它不是 angular 依赖项。 I get the error below.我收到以下错误。

const webpack = require('webpack');
module.exports = {
  externals: {
    '@somelib/core': '@somelib/core' // Does not work
  }
};

ERROR in main-es2015.js from Terser Unexpected character '@' [main-es2015.js:101,17]来自 Terser 意外字符“@”的 main-es2015.js 中的错误 [main-es2015.js:101,17]

I had the same problem when adding @blueprintjs/core as externals dependency.将 @blueprintjs/core 添加为外部依赖项时,我遇到了同样的问题。 Looking at the file produced by webpack (using minimize: false in optimization options), the problem seems related to the export:查看 webpack 生成的文件(在优化选项中使用 minimize: false ),问题似乎与导出有关:

module.exports = @blueprintjs/core;

Adding the following to my webpack.config:将以下内容添加到我的 webpack.config 中:

    output: {
    // https://github.com/webpack/webpack/issues/1114
    libraryTarget: 'commonjs2'
  },

allows to correctly configure the require produced by webpack:允许正确配置 webpack 产生的需求:

module.exports = require("@blueprintjs/core");

Looking at the github issue did not give me more infos on the how/why but I hope it will help others查看github 问题并没有给我更多关于如何/为什么的信息,但我希望它可以帮助其他人

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

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