thread-loader icon indicating copy to clipboard operation
thread-loader copied to clipboard

babel-loader.plugin[0] must be a string, object, function

Open csywweb opened this issue 4 years ago • 3 comments
trafficstars

  • Operating System: MacOs 11.4
  • Node Version: 10.23.0
  • NPM Version: 6.14.8
  • webpack Version: 4.44.1
  • thread-loader Version:2.1.3

Actual Behavior

Hi, I'm trying to use thread-loader with babel-loader but I have this error:

ERROR in ./src/main.js
Module build failed (from ./node_modules/thread-loader/dist/cjs.js):
Thread Loader (Worker 0)
.plugins[0] must be a string, object, function

    at assertPluginTarget (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/option-assertions.js:283:11)
    at assertPluginItem (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/option-assertions.js:275:5)
    at arr.forEach (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/option-assertions.js:241:30)
    at Array.forEach (<anonymous>)
    at assertPluginList (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/option-assertions.js:241:9)
    at Object.keys.forEach.key (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/options.js:123:5)
    at Array.forEach (<anonymous>)
    at validateNested (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/options.js:96:21)
    at validate (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/validation/options.js:87:10)
    at loadPrivatePartialConfig (/Users/csy/project/mei-pc-goods/node_modules/@babel/core/lib/config/partial.js:78:50)

Code

// webpack.config.js
 module: {
        rules: [
            {
                test: /\.(js?|tsx?|ts?)$/,
                include: [
                    path.resolve(rootPath, './src'),
                    path.resolve(rootPath, './shared'),
                ],
                use:  [
                        //   'cache-loader',
                          'thread-loader',
                          {
                              loader: 'babel-loader',
                              options: {
                                  presets: ["react-app", { flow: false, typescript: true }],
                                  plugins: [
                                     slot, 
                                    "add-module-exports",
                                   "@babel/plugin-proposal-export-default-from",
                                   ["@babel/plugin-proposal-decorators", { legacy: true }],
                                   "@babel/plugin-transform-modules-commonjs",
                                  "@babel/plugin-proposal-object-rest-spread",
                                  "@babel/plugin-proposal-export-namespace-from",],
                              },
                          },
                      ]

plugin slot is my custom plugin like this:

function slot() {
     return {
        visitor: {
              Program: {
                  enter: function enter(path, state) {
                    console.log(state);
                    console.log("-----");
                    console.log(Object.keys(state));
                  }
              }
        }
    }
}

when i remove the thead-loader, everything is ok

csywweb avatar Sep 05 '21 09:09 csywweb

If you want to work properly, you need to modify the way the custom plugin is used like this

slot => require.resolve({YourBabelPluginPath})

LeoJ-cn avatar Aug 08 '22 03:08 LeoJ-cn

the same problem:

webpack 5.65 react 17.0.2

image

alvin0216 avatar Aug 09 '22 07:08 alvin0216

the same problem:

webpack 5.65 react 17.0.2

image

I need to look at the configuration of babel-loder, you are hiding it

LeoJ-cn avatar Aug 15 '22 02:08 LeoJ-cn