javascript 如何使用 webpack 和 babel 导入 highcharts

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33241358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:15:57  来源:igfitidea点击:

how to import highcharts with webpack and babel

javascripthighchartsecmascript-6webpackes6-module-loader

提问by Shai M.

I use ES6, Babel and webpack stack.

我使用 ES6、Babel 和 webpack 堆栈。

I have installed highcharts by npm (I prefer to use the official highcharts npm repo):

我已经通过 npm 安装了 highcharts(我更喜欢使用官方的 highcharts npm repo):

npm install highcharts-release --save

But, regular import (ES6) doesn't work as expected:

但是,常规导入 (ES6) 无法按预期工作:

import highcharts from 'highcharts';

How can I import Highcharts via webpack import? Can you post a webpack.config.js example (or other way to config the plugins)?

如何通过 webpack 导入导入 Highcharts?您可以发布 webpack.config.js 示例(或其他配置插件的方式)吗?

Thanks.

谢谢。

EDIT

编辑

The error is:

错误是:

Uncaught Error: Cannot find module "highcharts" webpackMissingModule @ review-chart.js:2(anonymous function) ....

采纳答案by Andrey Bukati

There is a NPM called commonjs-highcharts that solves it. Just run npm i commonjs-highchartsand import it:

有一个名为 commonjs-highcharts 的 NPM 可以解决这个问题。只需运行npm i commonjs-highcharts并导入它:

import highcharts from "commonjs-highcharts"

Worked for me.

对我来说有效。

回答by Amith M S

Try this:

试试这个:

npm install highcharts

npm install highcharts

The issue I faced with this approach is using other modules in highcharts, such as highcharts-more, map, etc., To overcome this I imported highcharts and the other required modules like this:

我遇到这种方法的问题是在 highcharts 中使用其他模块,例如 highcharts-more、map 等,为了克服这个问题,我导入了 highcharts 和其他所需的模块,如下所示:

import highcharts from 'highcharts';
import highchartsMore from 'highcharts-more';
highchartsMore(highcharts);

回答by geoffberger

Try doing an npm install highcharts-release. Then in your JavaScript file do import Highcharts from 'highcharts-release/highcharts';. There may be a better way, but that worked for me.

尝试做一个npm install highcharts-release. 然后在您的 JavaScript 文件中执行import Highcharts from 'highcharts-release/highcharts';. 可能有更好的方法,但这对我有用。

回答by John Mee

Try variations of:

尝试以下变体:

require('expose?Highcharts!highcharts');
require('highcharts/modules/map')(Highcharts);
require('highcharts/highcharts-more')(Highcharts);
require('expose?Highcharts!highcharts/highstock');

If you wander around in ./node_modules/highcharts/...you might be able to trial-and-error your way into the modules and/or libs you need.

如果您四处游荡,./node_modules/highcharts/...您可能可以通过反复试验进入您需要的模块和/或库。

I don't have any joy using the form

使用表格我没有任何乐趣

$('myselector').highcharts(...)

Replacing them with

将它们替换为

Highcharts.chart('myselector', ...)

works for me.

对我来说有效。

回答by kylo

This is how I solved it, using Webpack v4.16.5 and Highcharts v5.0.11.

我就是这样解决的,使用 Webpack v4.16.5 和 Highcharts v5.0.11。

webpack.config

配置文件

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      use: [{
        loader: 'babel-loader'
      }]
    },
    {
      test: /highcharts.*/,
      loader: 'imports-loader?window=>global&window.jQuery=>$'
    }
    // ...
  ],
  alias: {
    jquery: 'jquery/jquery'
    // ...
  }
},
externals: {
  jQuery: 'jQuery'
},
plugins: [
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery',
    'window.$': 'jquery',
    Highcharts: 'highcharts/highcharts'
    // ...
  })
]

main.js 1st option

main.js 第一个选项

import addMore from 'highcharts/highcharts-more'
import addExporting from 'highcharts/modules/exporting'
import addOfflineExporting from 'highcharts/modules/offline-exporting'
import addSolidGauge from 'highcharts/modules/solid-gauge'
import addDrilldown from 'highcharts/modules/drilldown'
import addTreemap from 'highcharts/modules/treemap'
import addFunnel from 'highcharts/modules/funnel'

addMore(Highcharts)
addExporting(Highcharts)
addOfflineExporting(Highcharts)
addSolidGauge(Highcharts)
addDrilldown(Highcharts)
addTreemap(Highcharts)
addFunnel(Highcharts)

main.js 2nd option:

main.js 第二个选项:

require('highcharts/highcharts-more')(Highcharts)
require('highcharts/modules/exporting')(Highcharts)
require('highcharts/modules/offline-exporting')(Highcharts)
require('highcharts/modules/solid-gauge')(Highcharts)
require('highcharts/modules/drilldown')(Highcharts)
require('highcharts/modules/treemap')(Highcharts)
require('highcharts/modules/funnel')(Highcharts)

This way, it's both $(..).highcharts()and Highcharts.chart()usable.

这样,它既可用$(..).highcharts()Highcharts.chart()可用。

Hope this helps!

希望这可以帮助!

回答by Umang Khandelwal

You don't need any extra plugin or modification in your webpack config file. Just follow these steps:

您不需要在 webpack 配置文件中添加任何额外的插件或修改。只需按照以下步骤操作:

install typings file for highcharts using this:

使用以下命令为 highcharts 安装typings 文件:

npm install --save @types/highcharts

npm install --save @types/highcharts

change your import statements to following:

将您的导入语句更改为以下内容:

import * as Highcharts from 'highcharts'; import HighchartsMore = require('highcharts/highcharts-more'); HighchartsMore(Highcharts);

import * as Highcharts from 'highcharts'; import HighchartsMore = require('highcharts/highcharts-more'); HighchartsMore(Highcharts);

回答by ViES

For me only this method is working with webpack(and was working with browserify as well):

对我来说,只有这种方法适用于 webpack(并且也适用于 browserify):

global.js

global.js

import $ from 'jquery';

global.$ = global.jQuery = $;

app.js

应用程序.js

import './globals';
import 'angular';
import 'highcharts';
// ...

I don't know why webpack.ProvidePluginworks fine with AngularJSbut not with highcharts.

我不知道为什么webpack.ProvidePlugin与工作正常AngularJS,但不与highcharts

My config was looking like:

我的配置看起来像:

new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery', // for using with AngularJS
    _: 'underscore',
    moment: 'moment'
})

// I've also tried this but without luck:
{
  test: require.resolve('highcharts'),
  loader: 'imports-loader?jQuery=jquery'
}

回答by omer

i am working with AngulrJS, ES6, Webpack and Babel. it took me a while to find it but i ended up using expose on highchart.

我正在使用 AngulrJS、ES6、Webpack 和 Babel。我花了一段时间才找到它,但我最终在 highchart 上使用了 Exposure。

this is what i did:

这就是我所做的:

npm install highcharts --save

import 'expose?highcharts!highcharts/highcharts';

only import as shown, no need for any thing else.

只导入如图所示,不需要任何其他东西。

and then you can simple use highchart in your controller (without adding it as a dependency)

然后您可以在控制器中简单地使用 highchart(无需将其添加为依赖项)

回答by Stone

import Highcharts from 'highcharts';
import 'highcharts-ng'  //add this line if you wish to use highcharts angular directive

And then add a new rule in the webpack.config.js

然后在 webpack.config.js 中添加一条新规则

{
   test: require.resolve('highcharts'),
   use:[{
        loader: 'expose-loader',
        options: 'Highcharts'
   }]
}

回答by Alastair

Try using the package name as used during npm install:

尝试使用在 期间使用的包名称npm install

import highcharts from 'highcharts-release';

import highcharts from 'highcharts-release';