【问题标题】:WebPack and RequireWebPack 和 Require
【发布时间】:2016-09-20 10:49:11
【问题描述】:

我有一个模块A,如下所示,模块A是使用webpack打包的,它包含模块B。这个模块还使用require变量导出Highcharts库

A.js(在modules文件夹下::src/main/webapp/resources/js/modules)

var HighCharts = require("highchart");
var moduleB = require("../common/B.js"); //so that we can call draw() of moduleB

$(document).ready(function() {
    moduleB.print();
}

B.js(公共文件夹下:src/main/webapp/resources/js/common)

var print = function() {
console.log("something");
draw();
}
var chart;
function draw() {
    chart = new Highcharts.Chart({

    });
}

exports.print = print;

注意 ModuleB.js 捆绑在 A.js 中

当我加载 javascript 时,它给我一个错误,Highcharts 未定义。

//如何加载这个

chart = new Highcharts.Chart({

        });

为避免此错误,我执行了以下操作。

在 B.js 中做了以下操作。

var Highcharts = require('highcharts');

还将 B.js 从 common 文件夹移动到 modules 文件夹,因为它现在是一个入口点(从 src/main/webapp/resources/js/common 移动到 src/main/webapp/resources/js/modules)

WebPack 出现以下错误。
./src/main/webapp/resources/js/modules/A.js 中的错误模块未找到:错误:不允许对入口点的依赖 @ ./src/main/webapp/resources/js/modules/A.js 7:14-37

Webpack.config.js

入口点将是模块文件夹下的所有文件。

var path = require('path');
var webpack = require("webpack");
var glob = require("glob");
//var BowerWebpackPlugin = require("bower-webpack-plugin");
var jsSrcPath = 'src/main/webapp/resources/js/modules/**/*.js';
var jsDestPath = 'src/main/webapp/resources/build/js/modules';
var cssPath = '';


var files = glob.sync(jsSrcPath, {});
var entries = {};


for (var i = 0; i < files.length; i++) {
  var file = files[i];
  entries[file.replace(/^.*[\\\/]/, '').replace(/\.[^/.]+$/, "")] = path.join(__dirname, file);
}

var webpackOptions = {
  cache: true,
  watch: false,
  entry: entries,
  output: {
    path: __dirname + "/" + jsDestPath,
    filename: '[name].js',
  },
  module: {
    loaders: [{
        test: /.(?:jsx|js)$/,
        exclude: /node_modules/,
        loader: 'babel-loader?blacklist=useStrict'
      },
      // }, {
      //   test: /\.json/,
      //   exclude: /node_modules/,
      //   loader: 'json-loader'
      // }, {
      //   test: /\.css$/,
      //   exclude: /node_modules/,
      //   loader: "style!css"
      // }, {
      //   test: /\.scss$/,
      //   exclude: /node_modules/,
      //   loader: 'style-loader!css-loader!sass-loader!autoprefixer-loader?browsers=last 10 version'
      // }, {
      //   test: /\.(png|jpg)$/,
      //   exclude: /node_modules/,
      //   loader: 'url-loader?limit=999999'
      // }, {
      {
        test: /vendor\/.+\.(jsx|js)$/,
        exclude: /node_modules/,
        loader: 'imports?jQuery=jquery,$=jquery,this=>window'
      }
    ]
  },
  resolve: {
    root: [path.join(__dirname, "bower_components")],
    extensions: ['', '.js', '.json', '.jsx', '.css']
  },
  plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    }),
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     warnings: false
    //   }
    // })
  ],
  devServer: {
    contentBase: ".",
    noInfo: true, //  --no-info option
    hot: true,
    inline: true
  }
};



module.exports = webpackOptions;

PS:最初 B.js 位于模块文件夹之外,因为它不是入口点。后来它被移动到模块文件夹中,因为我们已经将它作为入口点。

【问题讨论】:

    标签: javascript highcharts webpack require


    【解决方案1】:

    “显然我不想在 moduleB 中加载我的高脚椅库,因为它不是 web pack 的入口点”

    实际上并非如此,尽管看起来很不直观!您确实需要在 moduleB 中导入 Highcharts,因为那是您使用它的地方。在 Node 和 Webpack 中,模块导入不是全局的;如果你有一个 moduleC 和另一个图表,你也必须在那里导入 Highcharts。

    Webpack 足够聪明,不会两次运行/包含导入的代码,因此没有理由避免这样做。 This answer I wrote a while back 准确解释了它的工作原理。

    编辑:查看您的配置,我认为您可能对 Webpack 的工作方式有错误的想法。您不会输入整个文件目录,然后返回一个完整的文件目录;您将单个文件设置为入口点,然后 Webpack 跟踪其所有依赖项,将所有内容捆绑到单个输出文件中*。入口点,顾名思义,就是您进入应用程序的点 - 您绝对不应该像现在这样将源文件夹中的每个文件都设置为入口点!

    这是您的配置的(希望是)固定版本:

    var path = require('path');
    var webpack = require("webpack");
    var jsDestPath = 'src/main/webapp/resources/build/js/modules';
    var cssPath = '';
    
    var webpackOptions = {
      cache: true,
      watch: false,
      entry: path.join(__dirname, "src/main/webapp/resources/js/modules/a.js"),
      output: {
        path: __dirname + "/" + jsDestPath,
        filename: '[name].js',
      },
      module: {
        loaders: [{
            test: /.(?:jsx|js)$/,
            exclude: /node_modules/,
            loader: 'babel-loader?blacklist=useStrict'
          },
          // }, {
          //   test: /\.json/,
          //   exclude: /node_modules/,
          //   loader: 'json-loader'
          // }, {
          //   test: /\.css$/,
          //   exclude: /node_modules/,
          //   loader: "style!css"
          // }, {
          //   test: /\.scss$/,
          //   exclude: /node_modules/,
          //   loader: 'style-loader!css-loader!sass-loader!autoprefixer-loader?browsers=last 10 version'
          // }, {
          //   test: /\.(png|jpg)$/,
          //   exclude: /node_modules/,
          //   loader: 'url-loader?limit=999999'
          // }, {
          {
            test: /vendor\/.+\.(jsx|js)$/,
            exclude: /node_modules/,
            loader: 'imports?jQuery=jquery,$=jquery,this=>window'
          }
        ]
      },
      resolve: {
        root: [path.join(__dirname, "bower_components")],
        extensions: ['', '.js', '.json', '.jsx', '.css']
      },
      plugins: [
        new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery"
        }),
        // new webpack.optimize.UglifyJsPlugin({
        //   compress: {
        //     warnings: false
        //   }
        // })
      ],
      devServer: {
        contentBase: ".",
        noInfo: true, //  --no-info option
        hot: true,
        inline: true
      }
    };
    
    
    
    module.exports = webpackOptions;
    

    * 这是一个概括——你可以有多个入口点,但它们必须是独立的——它们绝对不能相互导入。`

    【讨论】:

    • 如果我在模块 B 中包含高脚椅,那么会发生什么我不能在模块 A 中包含模块 B。Web 包会给出这个错误。错误在 ./src/main/webapp/resources/js/ A.js Module not found: Error: a dependency to an entry point is not allowed
    • @Raghu:您可以编辑您的问题并添加您的webpack.config.js 的内容吗?该错误使我认为您的入口点配置错误。
    • 编辑了问题。谢谢。
    • @Raghu:您不应该将 JS 源目录中的每个文件都设置为入口点(这就是您使用 for 循环所做的事情)。完全摆脱循环并将entry 设置为moduleA 的路径。这应该可以消除您的错误并允许您在 moduleB 中导入 Highcharts。
    • @Raghu:如果您不确定我的意思,请告诉我,我会在答案中添加一个示例。
    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 2023-03-13
    • 2016-09-17
    • 2017-05-27
    • 2016-02-29
    • 1970-01-01
    • 2018-03-25
    • 2022-01-14
    相关资源
    最近更新 更多