antfu / purge-icons

🎐 Bundles icons on demand

Home Page:https://www.npmjs.com/package/purge-icons

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[purge-icons-webpack-plugin] TypeError: PurgeIconsPlugin is not a constructor

ycs77 opened this issue · comments

I use purge-icons-webpack-plugin, but throw TypeError: PurgeIconsPlugin is not a constructor error:

const PurgeIconsPlugin = require('purge-icons-webpack-plugin')

module.exports = {
  // ...options
  plugins: [
    new PurgeIconsPlugin({
      /* PurgeIcons Options */
    })
  ]
}

Later I changed this code to run:

const PurgeIconsPlugin = require('purge-icons-webpack-plugin').default

// ...

I think this is a limitation for rollup when outputting in CJS. I don't see a proper way to solve this for now, if anyone knowing more context, please leave comments or just file a PR. Much appreciated.

One other way is to use TypeScript along with Webpack, if using TS works for you. https://webpack.js.org/guides/typescript/. With "esModuleInterop": true set, it should work as you expected.

@ycs77 for me after switching to

const PurgeIconsPlugin = require('purge-icons-webpack-plugin').default

TypeError: Cannot read property 'data' of undefined

my setup is using "webpack": "^5.0.0-beta.29" with vue-loader

@antfu "esModuleInterop": true didn't help for me

Guys, what do you think if we change the API to:

const { PurgeIcons } = require('purge-icons-webpack-plugin')
// or
import { PurgeIcons } from 'purge-icons-webpack-plugin'

Added in v0.4.1. It's backward compatible, so the existing .default should also work.

Great!