0

The error

 /node_modules/myLibrary/lib/index.js 97:8
Module parse failed: Unexpected token (97:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| class SomeClass {
|   
>   property;
|   property2;
|   property3;

I can't figure out what's wrong. If I use object with function properties it's work fine. If I use a class, I can't use it on node environment but not on React.

1
  • Oh, I explained myself badly. This is a require on top of a functional component, the problem isn't JSX, it's the module itself, that contains a class and webpack doesn't like it.
    – Icaruk
    Aug 7, 2021 at 21:04

1 Answer 1

0

I solved it using esbuild.

I created a file build.js

require('esbuild').build({
    entryPoints: [
        "lib/myLibrary.js",
    ],
    bundle: true,
    minifyWhitespace: true,
    minifySyntax: false,
    minifyIdentifiers: false,
    format: "cjs",
    platform: "neutral",
    outdir: 'out',
}).catch(() => process.exit(1))

Then I run node ./build.js and then I published the resulting file on NPM.
That solved the errors.

Edit: esbuild doesn't preserve JSDoc, Rollup does.

Alternative: Rollup

I used this config on rollup.config.js:

import commonjs from '@rollup/plugin-commonjs';

const config = {
    input: "src/myLibrary.js",
    output: {
        file: "myLibrary.js",
        format: "cjs",
        exports: "auto",
    },
    plugins: [commonjs()],
};

export default config;

Not the answer you're looking for? Browse other questions tagged or ask your own question.