Webpack无法解析/index.(js| jsx)文件,当包类型为模块时

i7uaboj4  于 10个月前  发布在  Webpack
关注(0)|答案(1)|浏览(137)

我对Webpack配置和cjs,mjs等之间的差异很陌生。当试图构建Webpack捆绑的组件库时,我在构建时遇到了这个错误(webpack命令)

BREAKING CHANGE: The request './components' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

字符串
当将index.js添加到./components/时,它工作得很好,但我希望我的webpack能够自己解决它。请注意,我使用package.json type:module来使用import syntex我的webpack配置:

import path from "path";
/**
 * @type {import('webpack').Configuration}
 */
export default {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(process.cwd(), "./dist"),
  },
  resolve: {
    extensions: [".js", ".jsx"],
    fullySpecified: false,
  },
  mode: "development",
  devtool: "source-map",
  module: {
    rules: [
      {
        type: "javascript/auto",
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env", "@babel/preset-react"],
            },
          },
        ],
      },
    ],
  },
};

kiz8lqtg

kiz8lqtg1#

通过将resolve: { fullySpecified: false },添加到rules部分中的babel规则来解决它

相关问题