rollup-plugin-comlink icon indicating copy to clipboard operation
rollup-plugin-comlink copied to clipboard

TypeScript: (plugin comlink) TypeError: Cannot read property 'id' of null

Open navono opened this issue 3 years ago • 4 comments

Followed Readme instruction, cannot compile with error: [!] (plugin comlink) TypeError: Cannot read property 'id' of null TypeError: Cannot read property 'id' of null at Object.resolveId (D:\sourcecode\frontend\ts-rollup-web-worker\node_modules\@surma\rollup-plugin-comlink\index.js:55:58)

base code was from rollup-starter-lib typescript branch.

rollup.config.js

import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import typescript from 'rollup-plugin-typescript';
import comlink from "@surma/rollup-plugin-comlink";
import omt from "@surma/rollup-plugin-off-main-thread";
import pkg from './package.json';

export default [
	// browser-friendly UMD build
	{
		input: 'src/main.ts',
		output: {
			name: 'howLongUntilLunch',
			file: pkg.browser,
			format: 'amd'
		},
		plugins: [
			comlink(),
			omt(),
			resolve(),   // so Rollup can find `ms`
			commonjs(),  // so Rollup can convert `ms` to an ES module
			typescript() // so Rollup can convert TypeScript to JavaScript
		]
	},

	// CommonJS (for Node) and ES module (for bundlers) build.
	// (We could have three entries in the configuration array
	// instead of two, but it's quicker to generate multiple
	// builds from a single configuration where possible, using
	// an array for the `output` option, where we can specify 
	// `file` and `format` for each target)
	// {
	// 	input: 'src/main.ts',
	// 	external: ['ms'],
	// 	plugins: [
	// 		typescript() // so Rollup can convert TypeScript to JavaScript
	// 	],
	// 	output: [
	// 		{ file: pkg.main, format: 'cjs' },
	// 		{ file: pkg.module, format: 'es' }
	// 	]
	// }
];

navono avatar Mar 08 '21 06:03 navono

Hi @navono,

you need to import your worker and specify .ts extension like so: import workerAPI from "comlink:./worker/worker.ts";

boazhoch avatar Apr 04 '21 06:04 boazhoch

@boazhoch, thank you for reply.

I do, but still not work, I create a repo for it.

navono avatar Apr 06 '21 08:04 navono

@navono Can you give me some reproduction steps? There’s all kinds of dependencies and other cruft, the tests don’t pass and I honestly have no idea how to reproduce the error you are complaining about.

(You are definitely importing a wrong/non-existent file here)

surma avatar Apr 06 '21 15:04 surma

first all, I have done with readme instruction about typescript.

// main.ts
import workerAPI from "comlink:./worker.js";

console.log(await workerAPI.sayHello("surma"));
// This would fail to compile:
// console.log(await workerAPI.doesNotExit("surma"));
// worker.ts
export function sayHello(name: string): string {
  return `Hello ${name}!`;
}
// worker-modules.d.ts
declare module "comlink:./worker.js" {
  // Do *not* move this to a top-level import, as it will turn this
  // .d.ts file from an ambient module into a local module.
  const wrap: import("comlink").Remote<typeof import("./worker.js")>;
  export default wrap;
}

no lucky.

then as @boazhoch said, I change .js to .ts as you can see in comlink branch code, also not work.

Steps to reproduce

  1. clone this repo
  2. checkou out comlink branch
  3. enter rollup-lib directory
  4. executen yarn, then yarn build.

Hope I explained the steps clearly.

navono avatar Apr 07 '21 02:04 navono