Необязательный не разбирать в одних каталогах, работает в других - PullRequest
0 голосов
/ 20 января 2020
Опции

My JS (?.) вызывают синтаксические ошибки в некоторых каталогах, но не в других. Я создал простой тестовый пример:

// MyService.js - contains an optional
import { PeopleCounter } from "MyLibrary";
export const handler = async event => {
  const foo = { cars: 2 };
  console.log(`cars: ${JSON.stringify(foo?.cars)}`);
  PeopleCounter();
};

// MyLibrary.js - contains an optional
export const PeopleCounter = () => {
  const foo = { people: 1 };
  console.log(`people: ${JSON.stringify(foo?.people)}`);
};

Webpack выдает ошибку компиляции при обработке опций в MyLibrary. js, но скомпилирует их в MyService. js.

ERROR in ./src/lib/MyLibrary.js 3:44
Module parse failed: Unexpected token (3:44)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| export const PeopleCounter = () => {
|   const foo = { people: 1 };
>   console.log(`people: ${JSON.stringify(foo?.people)}`);
| };
| 
 @ ./src/services/MyService.js 1:0-37 7:2-15

Что может заставить Webpack или Babel скомпилировать MyService. js, но не MyLibrary. js, когда они оба содержат опциональные значения?

Я использую рабочие области Yarn, Webpack, Serverless (с serverless-webpack плагин) и Babel со следующей структурой каталогов.

src
  - lib
    - MyLibrary.js
  - services
    - MyService.js

Моя конфигурация:

// webpack.config.js
const path = require("path");
const webpack = require("webpack");
const slsw = require("serverless-webpack");
const TerserPlugin = require("terser-webpack-plugin");
const ProgressBarPlugin = require("progress-bar-webpack-plugin");

module.exports = {
  mode: "development",
  entry: slsw.lib.entries,
  target: "node",
  devtool: "none",
  externals: [
    {
      "aws-sdk": "commonjs aws-sdk",
    }
  ],
  plugins: [
    new ProgressBarPlugin(),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    new webpack.DefinePlugin({
      "process.env.FLUENTFFMPEG_COV": false
    })
  ],
  optimization: {
    minimizer: [new TerserPlugin({ parallel: true, sourceMap: false })], // sourceMap: true
    minimize: false,
    usedExports: true
  },
  performance: {
    hints: false
  },
  stats: {
    maxModules: Infinity,
    optimizationBailout: false
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        include: path.resolve(__dirname, "src/services"),
        sideEffects: false,
        options: {
          babelrc: false,
          plugins: [],
          presets: [
            [
              "@babel/preset-env",
              {
                targets: {
                  node: "current"
                },
                shippedProposals: true,
                modules: false,
                debug: false
              }
            ]
          ]
        }
      }
    ]
  }
};
"@babel/core": "^7.8.3",
"@babel/preset-env": "^7.8.3",
"aws-sdk": "^2.606.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.4",
"babel-plugin-source-map-support": "^2.1.1",
"babel-polyfill": "^6.26.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-import": "^2.20.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.0",
"prettier": "^1.19.1",
"progress-bar-webpack-plugin": "^2.1.0",
"serverless-appsync-plugin": "^1.2.0",
"serverless-plugin-aws-alerts": "^1.4.0",
"serverless-plugin-custom-roles": "^1.1.1",
"serverless-plugin-split-stacks": "^1.9.3",
"serverless-webpack": "^5.3.1",
"terser-webpack-plugin": "^2.3.2",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
...