首页 > 解决方案 > 在 NextJS 中导入 GLSL 着色器——如何检查 Webpack 加载器是否工作?

问题描述

我正在尝试在我的 NextJS 应用程序中加载 GLSL 着色器。我已经像这样配置了我的 next.config.js:

module.exports = {
    reactStrictMode: true,
    images: {
        domains: [
            'localhost',
            'backend.rolideluxe.ch'
        ]
    },

    webpack: ( config, {} ) => {
        config.module.rules.push({
            test: /\.mp4$/,
            use: {
                loader: 'file-loader'
            }
        })

        config.module.rules.push({
            test: /\.(glsl|vs|fs|vert|frag)$/,
            use: {
                loader: 'raw-loader'
            }
        })

        return config
    },
}

这将返回错误(底部是着色器的着色器开始)

./public/shaders/blob/vertex.glsl
Module parse failed: Unexpected token (2:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
> uniform vec2 uFrequency;

我对 NextJS 和 ThreeJS 比较陌生,对 Webpack 的经验也很少。我尝试了不同的加载器(如 glsl-shader-loader 和 shader-loader),结果都一样。从我收集到的信息来看,错误应该在 Webpack 和 NextJS 的交集处。因此我的两个问题:

  1. 如何检查 Webpack 加载器是否有效。
  2. 有什么想法可以让我的导入工作吗?

标签: webpacknext.jsglsl

解决方案


如果您下载glslify-loader并将其推送到规则,您可以制作它

/** @type {import('next').NextConfig} */
module.exports = {
    reactStrictMode: true,
    webpack: (config, options) => {
        config.module.rules.push({
            test: /\.(glsl|vs|fs|vert|frag)$/,
            use: ['raw-loader', 'glslify-loader'],
        });

        return config;
    }
};

推荐阅读