javascript - 异步代码拆分 Webpack - 意外 token

标签 javascript reactjs webpack-2

我只是在关注这个guide

我有这个代码:

import React, { PropTypes, Component } from 'react';

import('contact-page').then(() => {});

我得到这个输出: webpack output

这是我的 webpack 文件:

var webpack = require('webpack');
var packages = require('./package.json');
var path = require('path');

var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

var filterDependencies = ['normalize.css', 'font-awesome'];
var dependencies = Object.keys(packages.dependencies).filter(f => !filterDependencies.some(fd => fd === f));


module.exports = {
    entry: {
        main: './src/index.js',
        vendor: dependencies
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist')
    },

    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            minChunks: Infinity,
        }),
        new ExtractTextPlugin("styles.css"),
        new HtmlWebpackPlugin({
            template: 'index.html'
        })
    ],

    module: {
        rules: [
            {
                test: /\.js?$/,
                use: [ 'babel-loader', ],
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                }),
                exclude: /node_modules/
            },
            {
                test: /(\.png|\.jpg|\.otf)$/,
                use: ['file-loader?name=[name].[ext]&publicPath=assets/&outputPath=assets/']
            } 
        ]
    },

    performance: {
        hints: "warning", // enum
        maxAssetSize: 200000, // int (in bytes),
        maxEntrypointSize: 400000, // int (in bytes)
        assetFilter: function (assetFilename) {
            // Function predicate that provides asset filenames
            return assetFilename.endsWith('.css') || assetFilename.endsWith('.js');
        }
    },

    devtool: "source-map", // enum

    target: "web", // enum

    stats: "errors-only",

    devServer: {
        proxy: { // proxy URLs to backend development server
            '/api': 'http://localhost:3000'
        },
        contentBase: path.join(__dirname, 'public'), // boolean | string | array, static file location
        compress: true, // enable gzip compression
        historyApiFallback: true, // true for index.html upon 404, object for multiple paths
        hot: true, // hot module replacement. Depends on HotModuleReplacementPlugin
        https: false, // true for self-signed, object for cert authority
        noInfo: true, // only errors & warns on hot reload
        // ...
    }
};

最佳答案

对于 dynamin 导入,我使用 babel-plugin-syntax-dynamic-import 库 - https://www.npmjs.com/package/babel-plugin-syntax-dynamic-import

安装后,您必须将 module.rules 设置扩展为类似的内容(只要您想混合 es2015 和 React):

module: {
  rules: [
  {
    test: /\.js?$/,
    use: {
      loader: 'babel-loader',
      options: {
         presets: [['es2015', "react"]],
         plugins: ['syntax-dynamic-import']
      },
    },
    exclude: /node_modules/
  },
},

教程https://webpack.js.org/guides/code-splitting-async/#usage-with-babel中有描述更详细。

关于javascript - 异步代码拆分 Webpack - 意外 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44672369/

相关文章:

javascript - 如何在多个变量中分离ajax响应以及如何调用它们?

javascript - 为什么 React 警告我不要将组件方法绑定(bind)到对象?

javascript - React安装问题npx-create-react-app无法正常工作

javascript - Hyperstack 中的高阶组件

node.js - 如何修复 "@types/node/index.d.ts is not a module"?

node.js - Webpack2 Node 库浏览器排除?

内部部分属性的javascript条件更新而不删除其他属性

javascript - 外部 ui-grid-menu-button?

javascript - 无法读取云功能中的托管文件

javascript - 是否可以在 promise 中简化 promise 解决?