webpack-contrib / html-loader

HTML Loader
MIT License
1.16k stars 205 forks source link

require(...) is not a function - PostHTML #422

Closed azan-n closed 2 years ago

azan-n commented 2 years ago

Bug report

Similar to https://github.com/posthtml/posthtml-loader/issues/132 using PostHTML causes the following error:

TypeError: require(...) is not a function I'm guessing this is a problem with PostHTML itself. I've tried using both posthtml().use().process() and posthtml.process() with no avail, every syntactic approach just leads to the require related error.

    module: {
        rules: [
            {
                test: /\.html$/i,

                // With PostHTML used in html-loader preprocessor option

                loader: "html-loader",
                options: {
                    preprocessor: (content, loaderContext) => {
                        posthtml([
                            require('posthtml-highlight')({ inline: true }),
                        ])
                            .process(content, { sync: true })
                            .then((result) => { return result.html })
                            .catch((error) => {
                                loaderContext.emitError(error);
                                return content;
                            })
                    }
                },
            },
        ],
    }

Actual Behavior

Every time I use PostHTML in module.rules.options.preprocessor as defined in the html-loader documentation, I get the error as defined above.

Expected Behavior

The loader should execute the preprocessor function correctly on all HTML pages it receives.

How Do We Reproduce?

https://github.com/azan-n/posthtml-loader-require-bug has a reproduction of the issue with both html-loader config and posthtml-loader configuration in the same webpack.config.js of which the posthtml-loader config is commented out.

Please paste the results of npx webpack-cli info here, and mention other relevant information

System: OS: Linux 5.13 Ubuntu 20.04.3 LTS (Focal Fossa) CPU: (4) x64 Intel(R) Core(TM) i5-3337U CPU @ 1.80GHz Memory: 830.10 MB / 7.65 GB Binaries: Node: 16.13.0 - /opt/node/bin/node npm: 8.1.4 - /opt/node/bin/npm Browsers: Chrome: 98.0.4758.102 Firefox: 97.0 Packages: html-loader: ^3.1.0 => 3.1.0 html-webpack-plugin: ^5.5.0 => 5.5.0 posthtml-loader: ^2.0.1 => 2.0.1 webpack: ^5.69.0 => 5.69.0 webpack-cli: ^4.9.2 => 4.9.2

alexander-akait commented 2 years ago

Please use use, there is error's message, sorry, we can't fix it here