webpack增强溶解插件不起作用

发布于 2025-01-29 03:39:38 字数 3602 浏览 4 评论 0 原文

我正在尝试遵循开发人员主题的说明,请 这行不通。我要解释这个问题,但首先让我看看我的项目结构。

我的文件结构如下:

---dist
---mylib
    index.ts
    lib.ts
---mylib1
    index.ts
    lib.ts
---src
    index.html
    index.ts
package.json
tsconfig.json
webpack.config.js

mylib/index.ts

export * from './lib';

mylib/lib.ts

export function sayHi() {
  alert("hi");
}

mylib1/index.ts

export * from './lib';

mylib1/lib.ts

export function sayHi1() {
  alert("hi1");
}

src/index.ts

import {sayHi1} from '@mylibs';

console.log("Hello World!");


sayHi1();

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "baseUrl": ".",
    "allowJs": true,
    "paths": {
      "@mylibs": ["mylib", "mylib1"]
    }
  },
  "files": ["src/index.ts"]
}

webpack.config.json package.json:

// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const AliasPlugin = require("enhanced-resolve/lib/AliasPlugin");

const isProduction = process.env.NODE_ENV == "production";

const stylesHandler = "style-loader";

const config = {
  entry: "./src/index.ts",
  output: {
    path: path.resolve(__dirname, "dist"),
  },
  devServer: {
    open: true,
    host: "localhost",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "index.html",
    }),
  ],
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/i,
        loader: "ts-loader",
        exclude: ["/node_modules/"],
        options: {
          transpileOnly: true,
        },
      },
      {
        test: /\.css$/i,
        use: [stylesHandler, "css-loader"],
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
        type: "asset",
      },

      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    plugins: [
      new AliasPlugin(
        "described-resolve",
        [{ name: "@mylibs", alias: ["../mylib", "../mylib1"] }],
        "resolve"
      ),
    ]
    // alias: {
    //  "@mylibs": ["../mylib", "../mylib1"] // also didn't work
    // },
  },
};

module.exports = () => {
  if (isProduction) {
    config.mode = "production";
  } else {
    config.mode = "development";
  }
  return config;
};

错误消息是:

export 'sayHi1' (imported as 'sayHi1') was not found in '@mylibs' (possible exports: sayHi)

如果我尝试导入 sayhi 它将起作用,或者即使我交换了 [“ ../ mylib”,“ ../mylib1”的位置] Aliasplugin中它也将起作用。看来它只会搜索第一个路径。 任何帮助都将不胜感激,因为我正在执行文档中所说的一切。

完整的WebPack输出消息,如果有帮助:

asset main.js 243 bytes [compared for emit] [minimized] (name: main)
asset index.html 201 bytes [compared for emit]
orphan modules 72 bytes [orphan] 2 modules
runtime modules 274 bytes 1 module
./src/index.ts + 1 modules 100 bytes [built] [code generated]

WARNING in ./src/index.ts 3:0-6
export 'sayHi1' (imported as 'sayHi1') was not found in '@mylibs' (possible exports: sayHi)

webpack 5.72.1 compiled with 1 warning in 755 ms

I'm trying to follow these instructions by the developers themesevles at https://github.com/webpack/webpack/issues/6817#issuecomment-542448438
which doesn't work. I'm going to explain the problem but first lets see my project structure.

my file structure is as follows:

---dist
---mylib
    index.ts
    lib.ts
---mylib1
    index.ts
    lib.ts
---src
    index.html
    index.ts
package.json
tsconfig.json
webpack.config.js

mylib/index.ts

export * from './lib';

mylib/lib.ts

export function sayHi() {
  alert("hi");
}

mylib1/index.ts

export * from './lib';

mylib1/lib.ts

export function sayHi1() {
  alert("hi1");
}

src/index.ts

import {sayHi1} from '@mylibs';

console.log("Hello World!");


sayHi1();

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "module": "es6",
    "target": "es5",
    "baseUrl": ".",
    "allowJs": true,
    "paths": {
      "@mylibs": ["mylib", "mylib1"]
    }
  },
  "files": ["src/index.ts"]
}

webpack.config.json
package.json:

// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const AliasPlugin = require("enhanced-resolve/lib/AliasPlugin");

const isProduction = process.env.NODE_ENV == "production";

const stylesHandler = "style-loader";

const config = {
  entry: "./src/index.ts",
  output: {
    path: path.resolve(__dirname, "dist"),
  },
  devServer: {
    open: true,
    host: "localhost",
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "index.html",
    }),
  ],
  module: {
    rules: [
      {
        test: /\.(ts|tsx)$/i,
        loader: "ts-loader",
        exclude: ["/node_modules/"],
        options: {
          transpileOnly: true,
        },
      },
      {
        test: /\.css$/i,
        use: [stylesHandler, "css-loader"],
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
        type: "asset",
      },

      // Add your rules for custom modules here
      // Learn more about loaders from https://webpack.js.org/loaders/
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    plugins: [
      new AliasPlugin(
        "described-resolve",
        [{ name: "@mylibs", alias: ["../mylib", "../mylib1"] }],
        "resolve"
      ),
    ]
    // alias: {
    //  "@mylibs": ["../mylib", "../mylib1"] // also didn't work
    // },
  },
};

module.exports = () => {
  if (isProduction) {
    config.mode = "production";
  } else {
    config.mode = "development";
  }
  return config;
};

the error message is:

export 'sayHi1' (imported as 'sayHi1') was not found in '@mylibs' (possible exports: sayHi)

If I try to import sayHi it will work or even if I swap the places of ["../mylib", "../mylib1"] in AliasPlugin it will work as well. It seems that it will search into the first path only.
Any helps would be appreciated because I'm doing everything as said in the docs.

full webpack output message if it helps:

asset main.js 243 bytes [compared for emit] [minimized] (name: main)
asset index.html 201 bytes [compared for emit]
orphan modules 72 bytes [orphan] 2 modules
runtime modules 274 bytes 1 module
./src/index.ts + 1 modules 100 bytes [built] [code generated]

WARNING in ./src/index.ts 3:0-6
export 'sayHi1' (imported as 'sayHi1') was not found in '@mylibs' (possible exports: sayHi)

webpack 5.72.1 compiled with 1 warning in 755 ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文