Webpack捆绑库导出为在导入中无法访问的变量

时间:2020-04-11 20:09:53

标签: javascript webpack

我有一个库导出为 var (MyTestLib)

Index.js

module.exports = { Hello: "Herere" };

Webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const sourcePath = path.join(__dirname, "./src");
const outPath = path.join(__dirname, "./dist");
module.exports = {
  context: sourcePath,
  entry: {
    "test": "./index.js",
  },
  target: "web",
  output: {
    path: outPath,
    publicPath: "/",
    filename: "[name].js",
    library: "MyTestLib",
    libraryTarget: "var",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        loaders: ["style-loader", "css-loader?modules"],
      },
      {
        test: /\.(js?|jsx)$/,
        use: ["babel-loader"],
      },
    ],
  },
  resolve: {
    extensions: [".js", ".jsx"],
  },
  devServer: {
    contentBase: path.resolve(__dirname, "dist"),
    port: 9000,
  },
};

现在我正尝试在其他应用程序MyTestApp-Parent中使用相同的Lib,如下所示

import * as tmp from "my-test-app";
console.log("tmp===> ", tmp);

它返回空白对象({})。 我在这里做错了什么?

0 个答案:

没有答案