如何从subrepo忽略package.json

时间:2019-03-28 11:39:49

标签: webpack babel package.json babel-loader

我正在开发一个核心库,可以在其中克隆不同的组件并将其自身附加在一起。每个组件目录都有一个package.json文件。我的核心是使用webpack,当我运行build命令时,它会抛出这样的错误

ERROR in ./src/clients/products/src/Products.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/mughees/Desktop/midgard-react/src/clients/products/src/Products.js: Unexpected token (46:17)

如果我从subrepo / module中删除package.json,则可以正常编译。

这是我的webpack。

const path = require("path");
const webpack = require("webpack");

module.exports = {
  entry: ["babel-polyfill", "./src/index.js"] ,
    mode: "development",
    devtool: 'inline-source-map',
    module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: "babel-loader",
        options: { presets: ["@babel/env"] }
      },
      {
        test: /\.(css|scss)$/,
        use: [
          "style-loader",
          "css-loader",
          "sass-loader"
        ]
      },
      { 
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [
          "file-loader",
          {
            loader: "image-webpack-loader",
            options: {
              bypassOnDebug: true,
              disable: true,
            },
          },
        ],
      }
    ]
  },
  resolve: {
    extensions: ["*", ".js", ".jsx"],
    modules: [path.resolve(__dirname, './src'), 'node_modules'],
    alias: {
      store: path.resolve(__dirname, './src/store'),
    }
  },
  output: {
    path: path.resolve(__dirname, "dist/"),
      publicPath: "/dist/",
      filename: "bundle.js"
  },
  devServer: {
    contentBase: path.join(__dirname, "public/"),
      port: 3000,
      publicPath: "http://localhost:3000/dist/",
      historyApiFallback: true,
      hotOnly: true
  },
  plugins: [new webpack.HotModuleReplacementPlugin()]
};

1 个答案:

答案 0 :(得分:0)

问题与 babel 有关,与 webpack 不相关。我有before

这里是对带有package.json文件的子目录的测试,该文件在运行 webpack 后会引发类似以下错误:

ERROR in ./home/three-JSX-main-patterns.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /var/www/html/ir/jsfun/home/three-JSX-main-patterns.js: Unexpected token (5:27)

在我的情况下抱怨 JSX

error when we have nested repository

solution

您的用例是什么?

  • 您要以编程方式创建配置吗?
  • 您要编译node_modules吗?

      

    babel.config.js适合您!

  • 您具有仅适用于简单单个软件包的静态配置吗?

      

    .babelrc适合您!

就我而言,我有.babelrc这样的人:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
}

,然后我用babel.config.js替换了它,如下所示:

module.exports = function (api) {
  api.cache(true);


  const presets = ["@babel/preset-env", "@babel/preset-react"];

  return {
    presets
  };
}

它工作正常。