levp / wrapper-webpack-plugin

A webpack plugin that wraps output files (chunks) with custom text or code.
91 stars 37 forks source link

WrapperPlugin is not compatible with webpack 5 #15

Closed AlexKlimenkov closed 2 years ago

AlexKlimenkov commented 3 years ago

wrapper-webpack-plugin causes a build error when used with webpack 5

Demo

https://github.com/AlexKlimenkov/webpack-sources-issue

Steps

git clone git@github.com:AlexKlimenkov/webpack-sources-issue.git
cd webpack-sources-issue
# npm install -g yarn
yarn
yarn start

Result

[webpack-cli] HookWebpackError: item.node is not a function
    at makeWebpackError (D:\Work\webpack-sources-issue\node_modules\webpack\lib\HookWebpackError.js:49:9)
    at D:\Work\webpack-sources-issue\node_modules\webpack\lib\Compilation.js:1995:11
    at eval (eval at create (D:\Work\webpack-sources-issue\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:17:1)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
-- inner error --
TypeError: item.node is not a function
    at D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\ConcatSource.js:59:50
    at Array.map (<anonymous>)
    at ConcatSource.node (D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\ConcatSource.js:58:63)
    at ConcatSource.proto.sourceAndMap (D:\work\webpack-sources-issue\node_modules\webpack-sources\lib\SourceAndMapMixin.js:29:18)
    at D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\dist\index.js:159:25
    at D:\work\webpack-sources-issue\node_modules\p-try\index.js:4:10
    at new Promise (<anonymous>)
    at pTry (D:\work\webpack-sources-issue\node_modules\p-try\index.js:3:37)
    at run (D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\node_modules\p-limit\index.js:24:18)
    at D:\work\webpack-sources-issue\node_modules\terser-webpack-plugin\node_modules\p-limit\index.js:46:18
error Command failed with exit code 1.

original discussion here: https://ask.csdn.net/questions/4560546

Fix

The issue seems to be fixed by accessing webpack-sources from the compiler.webpack object instead of requiring it directly using require("webpack-sources")

    apply(compiler) {
        const WebpackSources = compiler.webpack.sources;
        const ConcatSource = WebpackSources.ConcatSource;
AlexKlimenkov commented 3 years ago

PR: https://github.com/levp/wrapper-webpack-plugin/pull/16

levp commented 2 years ago

Fixed by #16

Thanks @AlexKlimenkov