Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack4 打包后的 modules 引用报错 #342

Open
zhangwei900808 opened this issue Aug 8, 2019 · 4 comments
Open

webpack4 打包后的 modules 引用报错 #342

zhangwei900808 opened this issue Aug 8, 2019 · 4 comments

Comments

@zhangwei900808
Copy link

zhangwei900808 commented Aug 8, 2019

下面是我hiynn-design 包的源文件:

const path = require("path");
const webpack = require("webpack");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const nodeExternals = require("webpack-node-externals");
const WebpackMd5Hash = require("webpack-md5-hash");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const resolve = dir => path.join(__dirname, ".", dir);
const isProd = process.env.NODE_ENV === "production";

module.exports = {
  entry: { main: "./lib/index.js" },
  output: {
    // path: resolve("dist"), // 输出目录
    path: path.resolve(__dirname, "dist"),
    filename: "[name].js", // 输出文件
    //不加下面几行,被引用会被报错
    libraryTarget: "umd", // 采用通用模块定义
    library: "hiynn-design", // 库名称
    libraryExport: "default", // 兼容 ES6(ES2015) 的模块系统、CommonJS 和 AMD 模块规范
    globalObject: "this" // 兼容node和浏览器运行,避免window is not undefined情况
  },
  devtool: "#source-map",
  module: {
    rules: [
      {
        test: /\.(sa|sc|c)ss$/,
        use: ["style-loader", MiniCssExtractPlugin.loader, "css-loader", "postcss-loader", "sass-loader"]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },
  resolve: {
    extensions: [".js", ".jsx"]
  },
  // 注意:本地预览的时候要注释,否则报 require undefined
  // https://stackoverflow.com/questions/45818937/webpack-uncaught-referenceerror-require-is-not-defined
  externals: [nodeExternals()],
  plugins: [
    new CleanWebpackPlugin(),
    new MiniCssExtractPlugin({
      filename: "[name].css"
    }),
    new WebpackMd5Hash()
  ],
  //压缩js
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true
      }),
      new OptimizeCssAssetsPlugin({
        assetNameRegExp: /\.css\.*(?!.*map)/g, //注意不要写成 /\.css$/g
        cssProcessor: require("cssnano"),
        cssProcessorOptions: {
          discardComments: { removeAll: true },
          // 避免 cssnano 重新计算 z-index
          safe: true,
          // cssnano 集成了autoprefixer的功能
          // 会使用到autoprefixer进行无关前缀的清理
          // 关闭autoprefixer功能
          // 使用postcss的autoprefixer功能
          autoprefixer: false
        },
        canPrint: true
      })
    ]
  }
};

.babelrc

{
  "presets": [
    ["@babel/preset-env", {
      "modules": false,
    }], "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-runtime"
  ]
}


下面是测试的项目引用的配置

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": [
    [
      "import",
      {
        "libraryName": "hiynn-design",
        "libraryDirectory": "lib",
        "style": "css" // `style: true` 会加载 less 文件
      }
    ]
  ]
}

package.json

    "hiynn-design": "^0.1.35",

image

@zhangwei900808
Copy link
Author

有大佬知道是什么原因导致的么,关键是我的@babel-react 也加到.babelrc 里面去了啊

@DearVikki
Copy link

你测试项目加载时没处理node_modules里的文件把?

@shaodahong
Copy link
Contributor

看起来不像是 import 插件的错误, 试试加上 @babel/preset-react

@xs47968224
Copy link

首先
{ "presets": ["@babel/preset-env", "@babel/preset-react"], "plugins": [ [ "import", { "libraryName": "hiynn-design", "libraryDirectory": "lib", "style": "css" // style: true会加载 less 文件 } ] ] } 这里的 “ // style: true 会加载 less 文件” 注释去掉,这是个坑,JSON里面不要写注释,格式会报错,其次,你重新npm install一次,再尝试npm start

yesmeck added a commit that referenced this issue Nov 4, 2020
Close

- #245
- #420
- #423
- #344
- #342
- #432
- #436
- #331
- #324
- #311
- #297
- #296
- #286
- #282
- #280
- #239
- #172
@yesmeck yesmeck mentioned this issue Nov 4, 2020
yesmeck added a commit that referenced this issue Nov 4, 2020
Close

- #245
- #420
- #423
- #344
- #342
- #432
- #436
- #331
- #324
- #311
- #297
- #296
- #286
- #282
- #280
- #239
- #172
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants