Javascript Webpack、React JSX、Babel:模块构建失败;意外标记“;空的?

Javascript Webpack、React JSX、Babel:模块构建失败;意外标记“;空的?,javascript,reactjs,webpack,jsx,babeljs,Javascript,Reactjs,Webpack,Jsx,Babeljs,我在使用Web包上的Babel编译React JSX代码时遇到问题。当我试图在我的代码上运行webpack时,一个语法错误会出现“意外令牌” B.法律改革委员会 { "plugins": ["transform-react-jsx"], "presets": [ "react", "es2015", "stage-0" ] } 不管怎样,我都会感谢你的帮助!谢谢 编辑: 因此,我将一些代码分离到一个模块中,尝试对所有内容进行分类,并将一些代码移动到一个新文件中,Grid.js。这将通过

我在使用Web包上的Babel编译React JSX代码时遇到问题。当我试图在我的代码上运行webpack时,一个语法错误会出现“意外令牌”

B.法律改革委员会

{
  "plugins": ["transform-react-jsx"],
  "presets": [ "react", "es2015", "stage-0" ]
}
不管怎样,我都会感谢你的帮助!谢谢

编辑:

因此,我将一些代码分离到一个模块中,尝试对所有内容进行分类,并将一些代码移动到一个新文件中,
Grid.js
。这将通过
import./Grid.js'导入到
main.jsx
放置在文件顶部。现在,当我尝试运行Webpack时,基本上会出现相同的错误,但它现在指向
Grid.js
文件的末尾,而不是
main.jsx
的末尾

ERROR in ./src/Grid.js
Module build failed: SyntaxError: Unexpected token (208:2)

  206 |   // Loop back from the target square and return the path
  207 |   return closed;
> 208 | };
      |   ^
这似乎不是密码。在重新安装依赖项之前,我已尝试删除项目目录中的
node\u modules
文件夹,并使用
npm init
重新初始化。不起作用。:/

编辑2:以下是
Grid.js
的代码

// Declare a Grid namespace
var Grid = {}; 

Grid.getAdjacentCells = (x, y, grid, noDiagonal) => {
  // returns an array of adjacent cells.
  var adjacents = [];

  if (grid[y - 1] && grid[y - 1][x] != null) { // northern cell
    adjacents.push({
      x: x,
      y: y - 1,
      cell: grid[y - 1][x],
      direction: "n"
    });
  }

  if (grid[y] && grid[y][x + 1] != null) { // eastern cell
    adjacents.push({
      x: x + 1,
      y: y,
      cell: grid[y][x + 1],
      direction: "e"
    });
  }

  if (grid[y + 1] && grid[y + 1][x] != null) { // southern cell
    adjacents.push({
      x: x,
      y: y + 1,
      cell: grid[y + 1][x],
      direction: "s"
    });
  }

  if (grid[y] && grid[y][x - 1] != null) { // western cell
    adjacents.push({
      x: x - 1,
      y: y,
      cell: grid[y][x - 1],
      direction: "w"
    });
  }

  // if noDiagonal is false, grab diagonal cells
  if (!noDiagonal) {
    if (grid[y - 1] && grid[y - 1][x - 1] != null) { // north-west cell
      adjacents.push({
        x: x - 1,
        y: y - 1,
        cell: grid[y - 1][x - 1],
        direction: "nw"
      });
    }

    if (grid[y - 1] && grid[y - 1][x + 1] != null) { // north-east
      adjacents.push({
        x: x + 1,
        y: y - 1,
        cell: grid[y - 1][x + 1],
        direction: "ne"
      });
    }

    if (grid[y + 1] && grid[y + 1][x + 1] != null) { // south-east
      adjacents.push({
        x: x + 1,
        y: y + 1,
        cell: grid[y + 1][x + 1],
        direction: "se"
      });
    }

    if (grid[y + 1] && grid[y + 1][x - 1] != null) {
      adjacents.push({
        x: x - 1,
        y: y + 1,
        cell: grid[y + 1][x - 1],
        direction: "sw"
      });
    }
  }

  return adjacents;
};

Grid.getRandomPointWithin = (x1, x2, y1, y2) => {
  return {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2)
  };
};

Grid.getRandomMatchingCellWithin = (x1, x2, y1, y2, type, grid) => {
  let cell = {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2)
  };

  while (grid[cell.y][cell.x].type != type) {
    cell = {
      x: Math.randomBetween(x1, x2),
      y: Math.randomBetween(y1, y2)
    };
  }

  return cell;
};

Grid.randomDirection = () => {
  return (Math.randomBetween(0,1) ? "x" : "y");
};

Grid.calculateApproxDistance = (x1, y1, x2, y2) => {
  return Math.abs((x2 - x1) + (y2 - y1));
};

Grid.determinePath = (startX, startY, targetX, targetY, grid) => {
  let closed = [],
      open = [];

  if (startX == targetX && startY == targetY)
    return [];

  let getCellFromList = (x, y, list) => {
    for (let cell of list) {
      console.log("Checking cell: ", cell, "of list against x:", x, "and y:", y);
      if (cell.x == x && cell.y == y) {
        return cell;
      }
    }
    return false;
  };

  let addCellToList = (cell, list) => {
    for (let i in list) {
      // check whether cell already exists in the list
      if (list[i].x == cell.x && list[i].y == cell.y) {
        // if so, check whether the cell in list has a higher score.
        if (list[i].f > cell.f) {
          // update cell to the lower score if so.
          list[i].g = cell.g;
          list[i].h = cell.h;
          list[i].f = cell.f;
          list[i].parent = cell.parent;
          return list;
        }
        // and if it the newer cell has a higher score, return the list as it is.
        return list;
      }
    }

    // The cell doesn't exist in the list. Push it in.
    list.push(cell);
    return list;
  };

  let start = {
    x: startX,
    y: startY,
    g: 0,
    h: Grid.calculateApproxDistance(startX, startY, targetX, targetY) * 10
  };

  start.f = start.g + start.h;

  open.push(start);

  let searching = true;
  while (searching) {
    // Set the current cell to one with the lowest score in the open list.
    let curr = open.reduce(function lowestScoreInOpenList(prev, curr) {
      if (!prev)
        return curr;
      if (curr.f < prev.f)
        return curr;
      return prev;
    }, null);

    // Transfer it to the closed list
    open.splice(open.indexOf(curr), 1);
    closed = addCellToList(curr, closed);

    // Check adjacent cells
    let adjacentCells = Grid.getAdjacentCells(curr.x, curr.y, grid);

    // Filter through adjacent cells
    adjacentCells = adjacentCells.filter(function adjacentCellFilter(a) {
      // Check whether cell is in the closed list
      if (getCellFromList(a.x, a.y, closed)) {
        // If so, skip it.
        return false;
      }
      // If cell is not a room cell, skip it.
      else if (a.cell.type != "corridor" ||
               a.cell.type != "room")
        return false;
      return true;
    });
    console.log(adjacentCells);

    // Transform each returned adjacent object into a path object


    searching = false;

  // Loop back from the target square and return the path
  return closed;
};
//声明一个网格名称空间
var-Grid={};
Grid.getAdjacentCells=(x,y,Grid,noDiagonal)=>{
//返回相邻单元格的数组。
var邻接项=[];
if(grid[y-1]&&grid[y-1][x]!=null){//northern单元格
邻接物({
x:x,
y:y-1,
单元格:网格[y-1][x],
方向:“n”
});
}
if(grid[y]&&grid[y][x+1]!=null){//
邻接物({
x:x+1,
y:y,
单元格:网格[y][x+1],
方向:“e”
});
}
if(grid[y+1]&&grid[y+1][x]!=null){//southern cell
邻接物({
x:x,
y:y+1,
单元格:网格[y+1][x],
方向:“s”
});
}
if(grid[y]&&grid[y][x-1]!=null){//western单元格
邻接物({
x:x-1,
y:y,
单元格:网格[y][x-1],
方向:“w”
});
}
//如果noDiagonal为false,则抓取对角单元格
if(!noDiagonal){
if(grid[y-1]&&grid[y-1][x-1]!=null){//西北单元
邻接物({
x:x-1,
y:y-1,
单元格:网格[y-1][x-1],
方向:“西北”
});
}
if(grid[y-1]&&grid[y-1][x+1]!=null){//东北
邻接物({
x:x+1,
y:y-1,
单元格:网格[y-1][x+1],
方向:“东北”
});
}
if(网格[y+1]&&grid[y+1][x+1]!=null){//东南
邻接物({
x:x+1,
y:y+1,
单元格:网格[y+1][x+1],
方向:“se”
});
}
if(网格[y+1]&&grid[y+1][x-1]!=null){
邻接物({
x:x-1,
y:y+1,
单元格:网格[y+1][x-1],
方向:“西南”
});
}
}
返回邻接;
};
Grid.getRandomPointWithin=(x1、x2、y1、y2)=>{
返回{
x:数学随机数(x1,x2),
y:数学,随机性(y1,y2)
};
};
Grid.getRandomMatchingCellWithin=(x1,x2,y1,y2,类型,网格)=>{
设单元格={
x:数学随机数(x1,x2),
y:数学,随机性(y1,y2)
};
while(网格[cell.y][cell.x].type!=type){
单元格={
x:数学随机数(x1,x2),
y:数学,随机性(y1,y2)
};
}
返回单元;
};
Grid.randomDirection=()=>{
返回值(数学随机数介于(0,1)-“x”:“y”之间);
};
Grid.calculateApproxDistance=(x1,y1,x2,y2)=>{
返回Math.abs((x2-x1)+(y2-y1));
};
Grid.determinePath=(startX、startY、targetX、targetY、Grid)=>{
设closed=[],
开放=[];
如果(startX==targetX&&startY==targetY)
返回[];
让getCellFromList=(x,y,list)=>{
for(让列表中的单元格){
log(“对照x:,x,“和y:,y:”检查列表的单元格:,单元格,”;
if(cell.x==x&&cell.y==y){
返回单元;
}
}
返回false;
};
让addCellToList=(单元格,列表)=>{
对于(让我进入列表){
//检查列表中是否已存在单元格
if(list[i].x==cell.x&&list[i].y==cell.y){
//如果是,请检查列表中的单元格是否得分较高。
if(列表[i].f>cell.f){
//如果是,则将单元格更新为较低的分数。
list[i].g=cell.g;
列表[i].h=cell.h;
列表[i].f=cell.f;
列表[i].parent=cell.parent;
退货清单;
}
//如果新的单元格得分较高,则按原样返回列表。
退货清单;
}
}
//列表中不存在该单元格。请将其推入。
列表推送(单元);
退货清单;
};
让我们开始={
x:startX,
y:斯塔蒂,
g:0,
h:网格。计算ProxDistance(startX、startY、targetX、targetY)*10
};
start.f=start.g+start.h;
打开。按下(启动);
让搜索=真;
同时(搜索){
//将当前单元格设置为开放列表中得分最低的单元格。
让curr=open.reduce(函数lowestScoreInOpenList(prev,curr){
如果(!prev)
返回货币;
中频(当前频率<先前频率)
返回货币;
返回上一个;
},空);
//将其转移到已关闭列表
开放式拼接(开放式索引of(curr),1);
关闭=添加大提琴手(当前,关闭);
//检查相邻单元格
设adjacentCells=Grid.getAdjacentCells(curr.x,curr.y,Grid);
//过滤相邻单元
adjacentCells=adjacentCells.filter(函数adjacentCellFilter(a){
//检查单元格是否在关闭列表中
if(getCellFromList(a.x,a.y,关闭)){
//如果是,跳过它。
返回false;
}
//如果单元格不是房间单元格,请跳过它。
否则,如果(a.cell.type!=“走廊”||
a、 cell.type!=“房间”)
返回false;
返回true;
});
console.log(相邻单元格);
//将每个返回的相邻对象转换为路径对象
搜索=假;
//从目标方块返回并返回路径
返回关闭;
};

您在列表末尾缺少一个“}”
ERROR in ./src/Grid.js
Module build failed: SyntaxError: Unexpected token (208:2)

  206 |   // Loop back from the target square and return the path
  207 |   return closed;
> 208 | };
      |   ^
// Declare a Grid namespace
var Grid = {}; 

Grid.getAdjacentCells = (x, y, grid, noDiagonal) => {
  // returns an array of adjacent cells.
  var adjacents = [];

  if (grid[y - 1] && grid[y - 1][x] != null) { // northern cell
    adjacents.push({
      x: x,
      y: y - 1,
      cell: grid[y - 1][x],
      direction: "n"
    });
  }

  if (grid[y] && grid[y][x + 1] != null) { // eastern cell
    adjacents.push({
      x: x + 1,
      y: y,
      cell: grid[y][x + 1],
      direction: "e"
    });
  }

  if (grid[y + 1] && grid[y + 1][x] != null) { // southern cell
    adjacents.push({
      x: x,
      y: y + 1,
      cell: grid[y + 1][x],
      direction: "s"
    });
  }

  if (grid[y] && grid[y][x - 1] != null) { // western cell
    adjacents.push({
      x: x - 1,
      y: y,
      cell: grid[y][x - 1],
      direction: "w"
    });
  }

  // if noDiagonal is false, grab diagonal cells
  if (!noDiagonal) {
    if (grid[y - 1] && grid[y - 1][x - 1] != null) { // north-west cell
      adjacents.push({
        x: x - 1,
        y: y - 1,
        cell: grid[y - 1][x - 1],
        direction: "nw"
      });
    }

    if (grid[y - 1] && grid[y - 1][x + 1] != null) { // north-east
      adjacents.push({
        x: x + 1,
        y: y - 1,
        cell: grid[y - 1][x + 1],
        direction: "ne"
      });
    }

    if (grid[y + 1] && grid[y + 1][x + 1] != null) { // south-east
      adjacents.push({
        x: x + 1,
        y: y + 1,
        cell: grid[y + 1][x + 1],
        direction: "se"
      });
    }

    if (grid[y + 1] && grid[y + 1][x - 1] != null) {
      adjacents.push({
        x: x - 1,
        y: y + 1,
        cell: grid[y + 1][x - 1],
        direction: "sw"
      });
    }
  }

  return adjacents;
};

Grid.getRandomPointWithin = (x1, x2, y1, y2) => {
  return {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2)
  };
};

Grid.getRandomMatchingCellWithin = (x1, x2, y1, y2, type, grid) => {
  let cell = {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2)
  };

  while (grid[cell.y][cell.x].type != type) {
    cell = {
      x: Math.randomBetween(x1, x2),
      y: Math.randomBetween(y1, y2)
    };
  }

  return cell;
};

Grid.randomDirection = () => {
  return (Math.randomBetween(0,1) ? "x" : "y");
};

Grid.calculateApproxDistance = (x1, y1, x2, y2) => {
  return Math.abs((x2 - x1) + (y2 - y1));
};

Grid.determinePath = (startX, startY, targetX, targetY, grid) => {
  let closed = [],
      open = [];

  if (startX == targetX && startY == targetY)
    return [];

  let getCellFromList = (x, y, list) => {
    for (let cell of list) {
      console.log("Checking cell: ", cell, "of list against x:", x, "and y:", y);
      if (cell.x == x && cell.y == y) {
        return cell;
      }
    }
    return false;
  };

  let addCellToList = (cell, list) => {
    for (let i in list) {
      // check whether cell already exists in the list
      if (list[i].x == cell.x && list[i].y == cell.y) {
        // if so, check whether the cell in list has a higher score.
        if (list[i].f > cell.f) {
          // update cell to the lower score if so.
          list[i].g = cell.g;
          list[i].h = cell.h;
          list[i].f = cell.f;
          list[i].parent = cell.parent;
          return list;
        }
        // and if it the newer cell has a higher score, return the list as it is.
        return list;
      }
    }

    // The cell doesn't exist in the list. Push it in.
    list.push(cell);
    return list;
  };

  let start = {
    x: startX,
    y: startY,
    g: 0,
    h: Grid.calculateApproxDistance(startX, startY, targetX, targetY) * 10
  };

  start.f = start.g + start.h;

  open.push(start);

  let searching = true;
  while (searching) {
    // Set the current cell to one with the lowest score in the open list.
    let curr = open.reduce(function lowestScoreInOpenList(prev, curr) {
      if (!prev)
        return curr;
      if (curr.f < prev.f)
        return curr;
      return prev;
    }, null);

    // Transfer it to the closed list
    open.splice(open.indexOf(curr), 1);
    closed = addCellToList(curr, closed);

    // Check adjacent cells
    let adjacentCells = Grid.getAdjacentCells(curr.x, curr.y, grid);

    // Filter through adjacent cells
    adjacentCells = adjacentCells.filter(function adjacentCellFilter(a) {
      // Check whether cell is in the closed list
      if (getCellFromList(a.x, a.y, closed)) {
        // If so, skip it.
        return false;
      }
      // If cell is not a room cell, skip it.
      else if (a.cell.type != "corridor" ||
               a.cell.type != "room")
        return false;
      return true;
    });
    console.log(adjacentCells);

    // Transform each returned adjacent object into a path object


    searching = false;

  // Loop back from the target square and return the path
  return closed;
};
// Declare a Grid namespace
var Grid = {};

Grid.getAdjacentCells = (x, y, grid, noDiagonal) => {
  // returns an array of adjacent cells.
  var adjacents = [];

  if (grid[y - 1] && grid[y - 1][x] != null) {
    // northern cell
    adjacents.push({
      x: x,
      y: y - 1,
      cell: grid[y - 1][x],
      direction: 'n',
    });
  }

  if (grid[y] && grid[y][x + 1] != null) {
    // eastern cell
    adjacents.push({
      x: x + 1,
      y: y,
      cell: grid[y][x + 1],
      direction: 'e',
    });
  }

  if (grid[y + 1] && grid[y + 1][x] != null) {
    // southern cell
    adjacents.push({
      x: x,
      y: y + 1,
      cell: grid[y + 1][x],
      direction: 's',
    });
  }

  if (grid[y] && grid[y][x - 1] != null) {
    // western cell
    adjacents.push({
      x: x - 1,
      y: y,
      cell: grid[y][x - 1],
      direction: 'w',
    });
  }

  // if noDiagonal is false, grab diagonal cells
  if (!noDiagonal) {
    if (grid[y - 1] && grid[y - 1][x - 1] != null) {
      // north-west cell
      adjacents.push({
        x: x - 1,
        y: y - 1,
        cell: grid[y - 1][x - 1],
        direction: 'nw',
      });
    }

    if (grid[y - 1] && grid[y - 1][x + 1] != null) {
      // north-east
      adjacents.push({
        x: x + 1,
        y: y - 1,
        cell: grid[y - 1][x + 1],
        direction: 'ne',
      });
    }

    if (grid[y + 1] && grid[y + 1][x + 1] != null) {
      // south-east
      adjacents.push({
        x: x + 1,
        y: y + 1,
        cell: grid[y + 1][x + 1],
        direction: 'se',
      });
    }

    if (grid[y + 1] && grid[y + 1][x - 1] != null) {
      adjacents.push({
        x: x - 1,
        y: y + 1,
        cell: grid[y + 1][x - 1],
        direction: 'sw',
      });
    }
  }

  return adjacents;
};

Grid.getRandomPointWithin = (x1, x2, y1, y2) => {
  return {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2),
  };
};

Grid.getRandomMatchingCellWithin = (x1, x2, y1, y2, type, grid) => {
  let cell = {
    x: Math.randomBetween(x1, x2),
    y: Math.randomBetween(y1, y2),
  };

  while (grid[cell.y][cell.x].type != type) {
    cell = {
      x: Math.randomBetween(x1, x2),
      y: Math.randomBetween(y1, y2),
    };
  }

  return cell;
};

Grid.randomDirection = () => {
  return Math.randomBetween(0, 1) ? 'x' : 'y';
};

Grid.calculateApproxDistance = (x1, y1, x2, y2) => {
  return Math.abs(x2 - x1 + (y2 - y1));
};

Grid.determinePath = (startX, startY, targetX, targetY, grid) => {
  let closed = [], open = [];

  if (startX == targetX && startY == targetY) return [];

  let getCellFromList = (x, y, list) => {
    for (let cell of list) {
      console.log(
        'Checking cell: ',
        cell,
        'of list against x:',
        x,
        'and y:',
        y
      );
      if (cell.x == x && cell.y == y) {
        return cell;
      }
    }
    return false;
  };

  let addCellToList = (cell, list) => {
    for (let i in list) {
      // check whether cell already exists in the list
      if (list[i].x == cell.x && list[i].y == cell.y) {
        // if so, check whether the cell in list has a higher score.
        if (list[i].f > cell.f) {
          // update cell to the lower score if so.
          list[i].g = cell.g;
          list[i].h = cell.h;
          list[i].f = cell.f;
          list[i].parent = cell.parent;
          return list;
        }
        // and if it the newer cell has a higher score, return the list as it is.
        return list;
      }
    }

    // The cell doesn't exist in the list. Push it in.
    list.push(cell);
    return list;
  };

  let start = {
    x: startX,
    y: startY,
    g: 0,
    h: Grid.calculateApproxDistance(startX, startY, targetX, targetY) * 10,
  };

  start.f = start.g + start.h;

  open.push(start);

  let searching = true;

  while (searching) {
    // Set the current cell to one with the lowest score in the open list.
    let curr = open.reduce(
      function lowestScoreInOpenList(prev, curr) {
        if (!prev) return curr;
        if (curr.f < prev.f) return curr;
        return prev;
      },
      null
    );

    // Transfer it to the closed list
    open.splice(open.indexOf(curr), 1);
    closed = addCellToList(curr, closed);

    // Check adjacent cells
    let adjacentCells = Grid.getAdjacentCells(curr.x, curr.y, grid);

    // Filter through adjacent cells
    adjacentCells = adjacentCells.filter(function adjacentCellFilter(a) {
      // Check whether cell is in the closed list
      if (getCellFromList(a.x, a.y, closed)) {
        // If so, skip it.
        return false;
      } else if (a.cell.type != 'corridor' || a.cell.type != 'room')
        // If cell is not a room cell, skip it.
        return false;
      return true;
    });
    console.log(adjacentCells);

    // Transform each returned adjacent object into a path object

    searching = false;

    // Loop back from the target square and return the path
    return closed;
  }
};