首页 > 解决方案 > Webpack 和 Phaser 3 配置

问题描述

我正在尝试使用书籍教程中的 Phaser 3 创建游戏,我决定包含 webpack 以用于学习目的。我只是处于游戏创建的初始阶段,但是当我运行npm start脚本时,我遇到了很多错误,我一一修复了。我没有更多错误,但是在运行脚本时,我得到了一个空白页面,并且在我的 dist 文件夹中没有创建任何内容。这是我的 webpack.config.js 文件内容:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
  // https://webpack.js.org/concepts/entry-points/#multi-page-application
  entry: ['babel-polyfill', './src/index.js'],

  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },

  // https://webpack.js.org/configuration/dev-server/
  devServer: {
    contentBase: './dist',
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: path.resolve(__dirname, 'src/'),
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
      },
      {
        test: /\.css$/i,
        use: [
          'style-loader',
          'css-loader',
        ],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader',
        ],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          'file-loader',
        ],
      },
    ],
  },

  // https://webpack.js.org/concepts/plugins/
  plugins: [
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, 'src/assets', '**', '*'),
          to: path.resolve(__dirname, 'dist'),
        },
      ],
    }),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: './src/index.html',
    }),
    new webpack.DefinePlugin({
      'typeof CANVAS_RENDERER': JSON.stringify(true),
      'typeof WEBGL_RENDERER': JSON.stringify(true),
    }),
  ],
};


其余文件位于我的repo中,请随时查看。提前感谢您的任何帮助。

标签: javascriptwebpackphaser-framework

解决方案


  1. CopyWebpackPlugin 中的更改行

from: path.resolve(__dirname, 'src/assets', '**', '*'),

from: 'src/assets',

  1. 然后npm run build

推荐阅读