代码之家  ›  专栏  ›  技术社区  ›  xkeshav

即使安装了webpack和babel,export/import关键字的浏览器抛出错误

  •  0
  • xkeshav  · 技术社区  · 6 年前

    我正在开发一个非常基本和简单的应用程序,核心是javascript和html 我已经安装了webpack v 4并使用

    npm run webpack

    项目编译成功;下面是输出

    > canvas@1.0.0 webpack /opt/parixan/canvas
    > webpack-dev-server --inline --hot
    
    ℹ 「wds」: Project is running at http://localhost:8080/
    ℹ 「wds」: webpack output is served from /
    ℹ 「wdm」: Hash: 99985e9b8632c783a375
    Version: webpack 4.12.1
    Time: 819ms
    Built at: 2018-06-30 18:45:48
            Asset     Size  Chunks             Chunk Names
        bundle.js  354 KiB    main  [emitted]  main
    bundle.js.map  406 KiB    main  [emitted]  main
    Entrypoint main = bundle.js bundle.js.map
    [./js/color.js] 359 bytes {main} [built]
    [./js/index.js] 628 bytes {main} [built]
    [./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built]
    [./node_modules/sockjs-client/dist/sockjs.js] 176 KiB {main} [built]
    [./node_modules/url/url.js] 22.8 KiB {main} [built]
    [./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 7.75 KiB {main} [built]
    [./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.58 KiB {main} [built]
    [./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.05 KiB {main} [built]
    [./node_modules/webpack-dev-server/node_modules/strip-ansi/index.js] (webpack)-dev-server/node_modules/strip-ansi/index.js 161 bytes {main} [built]
    [0] multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./js/index.js 52 bytes {main} [built]
    [./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
    [./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.6 KiB {main} [built]
    [./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 77 bytes {main} [built]
    [./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]
    [./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1010 bytes {main} [built]
        + 14 hidden modules
    ℹ 「wdm」: Compiled successfully.
    

    但是当我导航到 本地主机:8080/alphabet.html 它在控制台中抛出一个错误

    未捕获的语法错误:意外的令牌导出。字母.js:30

    我也注意到没有 bundle.js包 在下面归档 建造 文件夹,如中所述 webpack.config.js文件

    这是我的文件夹结构

    ├── README.md
    ├── alphabet.css
    ├── alphabet.html
    ├── build
    ├── fruits
    ├── js
          ├── alphabet.js
          ├── color.js
          └── index.js
    ├── .babelrc
    ├── package.json
    └── webpack.config.js
    
    • 节点-V 8.9.2条
    • 净现值 5.5.1条
    • MacOS-V型 10.13.3条
    • 巴别塔——版本
      6.26.0(巴别塔核心6.26.3)

    巴贝尔

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

    包.json

     "scripts": {
        "webpack": "webpack-dev-server --inline --hot",
        "start": "http-server"
      },
      "main": "./js.index.js",
    
      "devDependencies": {
        "@babel/core": "^7.0.0-beta.51",
        "babel-loader": "^8.0.0-beta.4",
        "babel-register": "^6.26.0",
        "jsxobj": "^1.1.0",
        "webpack": "^4.12.1",
        "webpack-cli": "^3.0.8",
        "webpack-dev-server": "^3.1.4",
        "webpack-node-externals": "^1.7.2"
      },
      "dependencies": {
        "@babel/preset-env": "^7.0.0-beta.51",
        "babel-eslint": "^7.2.3",
        "babel-preset-es2015": "^6.24.1",
        "path": "^0.12.7"
      }
    

    webpack.config.js文件

    module.exports = {
      context: __dirname,
      entry: "./js/index.js",
      mode: "development",
      output: {
        path: __dirname + "/build",
        filename: "bundle.js"
      },
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: "babel-loader",
              options: {
                presets: ["@babel/preset-env"]
              }
            }
          }
        ]
      },
      devtool: "source-map",
      resolve: {
        extensions: [".html", ".js", ".json", ".css"]
      }
    };
    

    /js/索引.js

    import { fetchImage } from "./alphabet.js";
    import { color } from "./color.js";
    
    
    const colorBox = color;
    console.log("colorBox", colorBox);
    
    const len = Object.keys(colorBox).length;
    
    fetchImage(colorBox);
    

    js/字母.js

    export const fetchImage = letter => {
      const fruit = fruits[letter] || "tamarindo";
      const request = new Request(`./fruits/${fruit}.png`, myInit);
      fetch(request)
        .then(response => response.blob())
        .then(blob => {
          const objectURL = URL.createObjectURL(blob);
          const img = new Image(200);
          img.src = objectURL;
          let element = document.getElementById("figure");
          while (element.firstChild) {
            element.removeChild(element.firstChild);
          }
          element.appendChild(img);
        });
    };
    

    我对这件事的主要怀疑 字母表.html 文件

    字母表.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Alphabet</title>
       <link rel="stylesheet" href="./alphabet.css">
    </head>
    <body>
        <div class="content" >
        <section class="alphabet"></section>
        <figure id="figure"></figure>
    </div>
    <script  src="./js/alphabet.js"></script>
    </body>
    </html>
    

    如果我改变 <script src="./js/index.js"> 比它抛出的错误

    未捕获的语法错误:意外的标记{

    进口 出口 无法识别关键字

    如何解决这个问题?

    2 回复  |  直到 6 年前
        1
  •  0
  •   somethinghere    6 年前

    如果您的目标是启用 import export 声明,您应该使用 module 在脚本标记上键入:

    <script type="module" ... />
    

    这将被不支持它的旧浏览器忽略,这意味着您可以确保旧浏览器甚至不尝试解析它。为了确保新的浏览器不会解析旧代码,可以使用 nomodule 以下内容:

    <script type="text/javascript" nomodule ... />
    

    有关使用 <script> 以这种方式标记: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

        2
  •  -1
  •   PlayMa256    6 年前

    1:webpack dev server直接将bundle构建到内存中,这就是为什么我们认为最好使用html webpack插件,这样bundle就会自动插入到html中。

    2:出现此错误是因为您引用的是ES6文件,而不是实际的包。这可能不适用于所有浏览器。


    使用 html-webpack-plugin 只需将此添加到生成中。ps:这要求您有一个基本的html,它将把资产插入其中。

    https://github.com/jantimon/html-webpack-plugin

    plugins: [
        new HtmlWebpackPlugin({
          template: 'path/to/my/index.html'
        })
      ]