Dolphin
Dolphin

Reputation: 38893

RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)

When I using this rollup "rollup": "^3.27.0" to compile the project, shows error:

 y-websocket git:(ts-migration) ✗ npm run build
npm info using [email protected]
npm info using [email protected]

> [email protected] build
> tsc --outDir dist/lib --declarationDir dist/types --declaration true && rollup -c


./src/y-websocket.ts → dist/y-websocket.cjs...
[!] RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
src/y-websocket.ts (28:21)
26:  * @type {Array<function(encoding.Encoder, decoding.Decoder, WebsocketProvider, boolean,    number):void>}
27:  */
28: const messageHandlers: any[] = [];
                         ^
29: 
30: messageHandlers[messageSync] = (
    at error (/Users/xiaoqiangjiang/source/reddwarf/backend/y-websocket/node_modules/rollup/dist/shared/rollup.js:353:30)
    at Module.error (/Users/xiaoqiangjiang/source/reddwarf/backend/y-websocket/node_modules/rollup/dist/shared/rollup.js:15237:16)
    at Module.tryParse (/Users/xiaoqiangjiang/source/reddwarf/backend/y-websocket/node_modules/rollup/dist/shared/rollup.js:15968:25)
    at Module.setSource (/Users/xiaoqiangjiang/source/reddwarf/backend/y-websocket/node_modules/rollup/dist/shared/rollup.js:15569:39)
    at ModuleLoader.addModuleSource (/Users/xiaoqiangjiang/source/reddwarf/backend/y-websocket/node_modules/rollup/dist/shared/rollup.js:25804:20)

I have tried to add the "@rollup/plugin-typescript": "^12.1.2" package and typescrit plugin the roll config file like this:

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

export default {
  input: './src/y-websocket.ts',
  external: id => /^(lib0|yjs|y-protocols)/.test(id),
  output: [{
    name: 'y-websocket',
    file: 'dist/y-websocket.cjs',
    format: 'cjs',
    sourcemap: true,
    dir: 'dist',
    paths: path => {
      if (/^lib0\//.test(path)) {
        return `lib0/dist${path.slice(4)}.cjs`
      } else if (/^y-protocols\//.test(path)) {
        return `y-protocols/dist${path.slice(11)}.cjs`
      }
      return path
    },
    plugins: [typescript()]
  }]
}

still did not fixed this issue, am I missing something? what should I do to fixed this issue? I have add the rootDir in the tsconfig.ts file like this:

"rootDir": "src", 

did not fixed this issue.

Upvotes: 0

Views: 46

Answers (1)

Lastik
Lastik

Reputation: 981

Please take a closer look to this basic Rollup configuration for typescript:

// rollup.config.js
import typescript from '@rollup/plugin-typescript';

export default {
  input: 'src/app.ts',
  output: {
    dir: 'output',
    format: 'cjs',
  },
  plugins: [typescript()],
};

The plugins section should be at the root of an exporting object. In your case, it's within output array's element.

I think that's the cause of your error.

Upvotes: 0

Related Questions