javascript - 未捕获的语法错误 : Unexpected token ) not sure what am i doing wrong?

标签 javascript webpack babeljs babel-loader

我正在尝试设置一个 javascript(Es6 +) 项目,我已经使用 Babel 设置了 Webpack,它似乎正在将我的代码转换为 Es5,但由于某种原因,导入语句无法在需要的地方工作。在构建期间没有任何失败,但在运行应用程序时它会打印此错误,捆绑的输出似乎略有偏差

index.js:14 Uncaught SyntaxError: Unexpected token )
    at Object../src/index.js (soem.2a9b32189bc67cc82c15.js:1189)
    at __webpack_require__ (soem.2a9b32189bc67cc82c15.js:727)
    at fn (soem.2a9b32189bc67cc82c15.js:101)
    at Object.0 (soem.2a9b32189bc67cc82c15.js:1225)
    at __webpack_require__ (soem.2a9b32189bc67cc82c15.js:727)
    at soem.2a9b32189bc67cc82c15.js:794
    at soem.2a9b32189bc67cc82c15.js:797

捆绑输出

__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _someModule_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./someModule.js */ "./src/someModule.js");
/* harmony import */ var _styles_scss_main_scss__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./styles/scss/main.scss */ "./src/styles/scss/main.scss");
/* harmony import */ var _styles_scss_main_scss__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_styles_scss_main_scss__WEBPACK_IMPORTED_MODULE_1__);
// const { dk } = require('./otherModule.js');


var tt = 'hdh';
var tk = {
  tt: 'hdhd',
  replace: '""'
};
dkk();
soObject(_someModule_js__WEBPACK_IMPORTED_MODULE_0__["dkk"])); /Object(_someModule_js__WEBPACK_IMPORTED_MODULE_0__["some"])nsole.log(`jdjdjdj ${tt} ${tk}`);//# sourceURL=[module]

["dkk"] 之后有一个额外的右括号

package.json

{
  "name": "sdk-v10-web",
  "version": "10.0.9",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --config build-utils/webpack.config.js --env.env=dev --hot",
    "build": "npm version patch && webpack --config build-utils/webpack.config.js --env.env=prod",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "autoprefixer": "^9.6.1",
    "babel-loader": "^8.0.6",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.2.0",
    "dotenv-webpack": "^1.7.0",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.12.0",
    "postcss-loader": "^3.0.0",
    "sass-loader": "^7.3.1",
    "style-loader": "^1.0.0",
    "webpack": "^4.39.2",
    "webpack-cli": "^3.3.7",
    "webpack-dev-server": "^3.8.0",
    "webpack-merge": "^4.2.1",
    "webpack-plugin-replace": "^1.2.0"
  },
  "browserslist": [
    "last 10 version",
    "> 1%",
    "not ie < 8"
  ]
}

webpack.common.js

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ReplacePlugin = require('webpack-plugin-replace');
const fs = require("fs");

const REPLACEABLES = {
  PROMPT_TEMPLATE: ''//fs.readFileSync(path.join(__dirname, '../src') + '/prompt-templates/master-template.html', "utf8"),
}

module.exports = {
  entry: './src/index.js',
  module: {
    rules: [
        {
            test: /\.(js)$/,
            exclude: /node_modules/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env']
              }
            }
        },
        {
            test: [/.css$|.scss$/],
            use: [
                MiniCssExtractPlugin.loader,
                // 'style-loader',
                'css-loader',
                'sass-loader',
                'postcss-loader'
            ]
        },
    ]
  },
  resolve: {
    extensions: ['*', '.js']
    // alias: {
    //   // Utilities: path.resolve(__dirname, 'src/utilities/'),
    //   // Templates: path.resolve(__dirname, 'src/templates/')
    // }
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: './src/index.html'
    }),
    new MiniCssExtractPlugin({
        filename: 'hansel.[contenthash].css'
    }),
    new ReplacePlugin({
      exclude: [
        /node_modules/,
        /build-utils/,
        /dist/,
      ],
      values: {
        '%%REPLACE%%': JSON.stringify(REPLACEABLES.PROMPT_TEMPLATE)
      }
    }),
  ],
  output: {
    path: path.resolve(__dirname, '../', 'dist'),
    publicPath: '/',
    filename: 'soem.[hash].js'
  },
  devServer: {
    contentBase: './dist',
    host: "localhost.soem.io",
    open: true,
    port: 8080,
    // https: true,
  },
};

webpack.dev.js

const { DefinePlugin } = require('webpack');
const Dotenv = require('dotenv-webpack');

module.exports = {
  mode: 'development',
  devtool: 'eval-source-map',
  plugins: [
    new DefinePlugin({
      'process.env': {    // console.log(process.env.NODE_ENV); in source code
        'NODE_ENV': JSON.stringify('development'),
      }
    }),
    new Dotenv({
      path: './.env.development',
    }),
  ],
};

webpack.prod.js

const { DefinePlugin } = require('webpack');
const Dotenv = require('dotenv-webpack');

module.exports = {
  mode: 'production',
  devtool: 'source-map',
  plugins: [
    new DefinePlugin({
      'process.env': {  // console.log(process.env.NODE_ENV); in source code
        'NODE_ENV': JSON.stringify('production'),
      }
    }),
    new Dotenv({
      path: './.env.production',
    }),
  ],
};

webpack.config.js

const webpackMerge = require('webpack-merge');

const commonConfig = require('./webpack.common.js');

module.exports = ({ env }) => {
  const envConfig = require(`./webpack.${env}.js`);

  return webpackMerge(commonConfig, envConfig);
};

someModule.js

export function some() {
    // function someMethod() {
    //     console.log('someMethod');
    // }
    console.log('fsf');
}

export function dkk() {
    // function otherMethod() {
    //     console.log('otherMethod')
    // }
    console.log('hshs');
    return 'tt';
}
// some();

// export default some;

index.js

// const { dk } = require('./otherModule.js');
import {some, dkk } from './someModule.js';
import './styles/scss/main.scss';

const tt = 'hdh';
const tk = {
    tt: 'hdhd',
    replace: '%%REPLACE%%',
}
dkk();
some();

console.log(`jdjdjdj ${tt} ${tk}`);

最佳答案

问题是 webpack-plugin-replace 插件,由于某种原因破坏了输出,使用 string-replace-loader 来完成这项工作

关于javascript - 未捕获的语法错误 : Unexpected token ) not sure what am i doing wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650150/

相关文章:

javascript - 使用 React 和 SCSS 实现无样式内容的 Flash

javascript - 从另一个更新一个 React 的组件状态

reactjs - 语法错误: Unexpected identifier {import React from 'react' ;

javascript - 'babel-core/register' vs 'babel-register'

javascript - jquery中的一个div两个函数

javascript - 无法让折叠装订线以 xml 模式显示在 CodeMirror 中

Javascript字符串到数组(如果字符串没有任何空格)

javascript - JSON 数据文本到 href

vue.js - 在vue-cli 3.0中如何生成完整的webpack.config.js

babeljs - babel 7 - 如何防止添加 "strict mode"