Skip to content

After update from webpack4 to webpack5 exported function is wrapped in object #12675

@ivictbor

Description

@ivictbor

Bug report

I have a library that has 26k npm downloads, it exports a function, and users use it with npm install and then import commonjs2 file with:

import Painterro from 'painterro';

Painterro(params)

Once I updated webpack I noted that Painterro is not function but object. So basic destruction works fine:

import { Painterro } from 'painterro';

Though named import is not so bad, I can imagine that my users will receive a lot of pain if I break backward compatibility even despite on minor number change. I was searching for the changes in the documentation but found nothing.

I prepared a super-minimal example to show the issue.

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

  1. Create file main.js:
module.exports = () => 1234;
  1. Create file package.json:
{
  "name": "p2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --progress"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^5.21.2",
    "webpack-cli": "^4.5.0"
  }
}

  1. Create file webpack.config.js:
'use strict';
const path = require('path');

module.exports = {
  mode: 'development',
  entry: './main.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'p2.commonjs2.js',
    library: 'P2',
    libraryTarget: 'commonjs2',
  },
}
  1. Also, create a simple test.js:
const p2 = require('./build/p2.commonjs2.js');

console.log(p2)

now do:

  1. npm run build
  2. And node test.js. I see:
{ P2: [Function] }

What is the expected behavior?

  1. Next I do: npm i [email protected] -D
  2. npm run build
  3. node test.js. I see:
node test.js
[Function]

Other relevant information:
webpack version: 5.21.2
Node.js version: v12.20.1
Operating System: Windows WSL 2
Additional tools: npm 7.5.4,

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions