计算的属性不适用于Babel预设环境

时间:2019-11-05 16:34:59

标签: reactjs webpack babeljs

我正在尝试在我的Material UI样式对象中使用计算属性,但是出现了意外的令牌错误。

下面是代码段,错误和babel-webpack配置。

const useStyles = makeStyles(theme => {
  root: {
    [desktop]: {
      width: "80%",
    }
  }
});
//webpack.config.babel.js
...

const config = {
  entry: ['@babel/polyfill', './src/components/index.js'],

  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx'],
  },

  devServer: {
    host: '0.0.0.0',
    historyApiFallback: {
      index: 'index.dev.html',
    },
  },

  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'main.js',
    chunkFilename: '[name].js',
    publicPath: '/build/',
  },

  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true,
        extractComments: true,
      }),
      new OptimizeCSSAssetsPlugin({}),
    ],
    splitChunks: {
      cacheGroups: {
        default: false,
        vendors: {
          // name of the chunk
          name: 'vendor',
          // sync + async chunks
          chunks: 'all',
          // import file path containing node_modules
          test: /node_modules/,
          priority: 20,
        },
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: "all",
          enforce: true,
        },
        common: {
          name: 'common',
          minChunks: 2,
          chunks: 'all',
          priority: 10,
          reuseExistingChunk: true,
          enforce: true,
        },
      },
    },
  },

  module: {
    rules: [
      {
        test: /\.(js|ts)x?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: [
            '@babel/typescript',
            "@babel/preset-env",
            '@babel/preset-react',
            '@babel/preset-flow',
          ],
          plugins: [
            '@babel/plugin-transform-runtime',
            '@babel/plugin-proposal-object-rest-spread',
            ['@babel/plugin-proposal-decorators', {legacy: true}],
            ['@babel/plugin-proposal-class-properties', {loose: true}],
            ['@babel/plugin-transform-computed-properties',  {loose: true}],
          ],
        },
      },
      {
        test: /\.s?[ac]ss$/,
        use: [
          devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
          'css-loader',
          'sass-loader',
        ],
      },
    ],
  },

  node: {
    fs: 'empty',
  },

  // First array is dev only, second is production
  plugins: devMode 
  ? [
  ] : [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production'),
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css'
    }),
  ],
};

// Exports
module.exports = config;

  

at Object.raise(/node_modules/@babel/parser/lib/index.js:6931:17)
  在Object.unexpected(/node_modules/@babel/parser/lib/index.js:8324:16)
  在Object.semicolon(/node_modules/@babel/parser/lib/index.js:8306:40)
  在Object.parseExpressionStatement(/node_modules/@babel/parser/lib/index.js:11147:10)
  在Object.parseExpressionStatement(/node_modules/@babel/parser/lib/index.js:2072:18)
  at Object.parseStatementContent(/node_modules/@babel/parser/lib/index.js:10746:19)
  在Object.parseStatement(/node_modules/@babel/parser/lib/index.js:10612:17)
  在Object.parseStatement(/node_modules/@babel/parser/lib/index.js:2045:26)
  在Object.parseBlockOrModuleBlockBody(/node_modules/@babel/parser/lib/index.js:11188:25)
  在Object.parseBlockBody(/node_modules/@babel/parser/lib/index.js:11175:10)
  在Object.parseBlock(/node_modules/@babel/parser/lib/index.js:11159:10)
  at Object.parseStatementContent(/node_modules/@babel/parser/lib/index.js:10688:21)
  在Object.parseStatement(/node_modules/@babel/parser/lib/index.js:10612:17)
  在Object.parseStatement(/node_modules/@babel/parser/lib/index.js:2045:26)
  在Object.parseLabeledStatement(/node_modules/@babel/parser/lib/index.js:11139:22)
  在Object.parseStatementContent(/node_modules/@babel/parser/lib/index.js:10744:19)

1 个答案:

答案 0 :(得分:0)

这是一个简单的错误。我没有将对象括在括号中(缺少返回)。

const useStyles = makeStyles((theme) => ({
  root: {
    [desktop]: {
      ...
    }
  }
}));