使用WebPack-Dev-Middleware时,如何将图像加载到Phaser应用中



我有WebPack/Phaser/Express项目。当我加载应用程序时,映像不能在Chrome Console中加载以下错误:

GET http://localhost:8080/assets/ui/blue_button03.png 500 (Internal Server Error)
GET http://localhost:8080/assets/ui/blue_button02.png 500 (Internal Server Error)

将这些图像加载到移相器中:

this.load.image('blueButton1', 'assets/ui/blue_button02.png');
this.load.image('blueButton2', 'assets/ui/blue_button03.png');

我在每个静态文件上都遇到了相同的错误。

webpack配置:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// Phaser webpack config
const phaserModule = path.join(__dirname, '/node_modules/phaser/')
const phaser = path.join(phaserModule, 'src/phaser.js')
const definePlugin = new webpack.DefinePlugin({
    __DEV__: JSON.stringify(JSON.parse(process.env.BUILD_DEV || 'true')),
    WEBGL_RENDERER: true, // I did this to make webpack work, but I'm not really sure it should always be true
    CANVAS_RENDERER: true // I did this to make webpack work, but I'm not really sure it should always be true
})
module.exports = {
    entry: {
        app: './src/js/main.js',
        vendor: ['phaser']
    },
    devtool: '#source-map',
    mode: 'development',
    target: 'web',
    output: {
        pathinfo: true,
        path: path.resolve(__dirname, 'dev'),
        publicPath: '/',
        library: '[name]',
        libraryTarget: 'umd',
        filename: '[name].js'
    },
    plugins: [
        definePlugin,
        new HtmlWebpackPlugin({
            filename: './index.html',
            template: './src/html/index.html',
            chunks: ['vendor', 'app'],
            chunksSortMode: 'manual',
            minify: {
                removeAttributeQuotes: false,
                collapseWhitespace: false,
                html5: false,
                minifyCSS: false,
                minifyJS: false,
                minifyURLs: false,
                removeComments: false,
                removeEmptyAttributes: false
            },
            hash: false
        }),
        new webpack.NoEmitOnErrorsPlugin()
    ],
    module: {
        rules: [
            {
                test: /.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                // Loads the javacript into html template provided.
                // Entry point is set below in HtmlWebPackPlugin in Plugins 
                test: /.html$/,
                use: [
                {
                    loader: "html-loader",
                    //options: { minimize: true }
                }
                ]
            },
            {
                test: /.(png|svg|jpg|gif|wav)$/,
                use: ['file-loader']
            },
            {
                test: /.css$/,
                use: [ 'style-loader', 'css-loader' ]
            },
            { test: /phaser-split.js$/, use: ['expose-loader?Phaser'] },
            { test: [/.vert$/, /.frag$/], use: 'raw-loader' }
        ]
    }
}

那就是我在我的服务器中用户用户使用WebPack-dev-Middleware:

const app = express(),
            DIST_DIR = __dirname,
            HTML_FILE = path.join(DIST_DIR, 'index.html'),
            compiler = webpack(config);
var server = http.Server(app);
// binds the serv object we created to socket.io
var io = socketio(server);
app.use(webpackDevMiddleware(compiler, {
  publicPath: config.output.publicPath
}));
app.get('*', (req, res, next) => {
    compiler.outputFileSystem.readFile(HTML_FILE, (err, result) => {
        if (err) {
            return next(err);
        }
        res.set('content-type', 'text/html');
        res.send(result);
        res.end();
    });
});
const PORT = process.env.PORT || 8080;

项目结构:

 +-- dev
 |  |  
 |  +-- assets
 |  +-- index.html
 |  +-- app.js
 |  +-- server.js
 |  +-- vendor.js
 |    
 +-- src
 |  |  
 |  +-- assets
 |  +-- html
 |  |
 |  |  +-- index.html
 |  |
 |  +-- server
 |  |
 |  |  +-- server.js
 |  |
 |  +-- js
 |  |
 |  |  +-- main.js
 |  |
 |    
 +-- package.json
 |    
 +-- webpack.config.js

有什么建议如何使其正常工作?因为如果我不使用webpack-dev-middleware应用程序,则可以正常工作。但是,每次进行一些更改时,都无法重建项目。

如果您首先导入资产,则webpack将解决捆绑包中的路径。然后,您可以将其加载到She中:

import blueButton1 from '../assets/ui/blue_button02.png'
this.load.image('blueButton1', blueButton1);

作为替代方案,您可以使用类似的插件:https://github.com/goldfire/phaser-webpack-loader

相关内容

  • 没有找到相关文章

最新更新