Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

web-worker doesn't work with typescript and nextjs #2753

Open
4 tasks done
iSuslov opened this issue Apr 9, 2024 · 1 comment
Open
4 tasks done

web-worker doesn't work with typescript and nextjs #2753

iSuslov opened this issue Apr 9, 2024 · 1 comment
Labels
Type: Bug 🐛 Something isn't working

Comments

@iSuslov
Copy link

iSuslov commented Apr 9, 2024

Overview

web-worker doesn't work with typescript and nextjs. Suggested babel-loader only works with js files.
Nextjs uses webpack5 and bunch of own loaders.
Adding

module: {
    rules: [
      {
        test: /\.js/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              babelrc: false,
              plugins: [require.resolve('@shopify/web-worker/babel')],
            },
          },
        ],
      },
    ],
  },

is not enough.

I was able to make it work by modifying webpack config in next.config like this:

import {WebWorkerPlugin} from '@shopify/web-worker/webpack';
/** @type {import('next').NextConfig} */
const nextConfig = {
	webpack: (config, { isServer, nextRuntime, dev }) => {
		config.module.rules.map((r) => {
			if (
				r.issuerLayer === "app-pages-browser" &&
				Array.isArray(r.use) &&
				Array.isArray(r.resolve.mainFields)
			) {
				r.use.push({
					loader: "babel-loader",
					options: {
						babelrc: false,
						plugins: [import.meta.resolve("@shopify/web-worker/babel")],
					},
				});
			}
			return r;
		});
		config.plugins.push(
			new WebWorkerPlugin({
				globalObject: "self",
			}),
		);

		return config;
	},
};
export default nextConfig;

So the appropriate rule looks like this:
Screenshot 2024-04-09 at 3 40 45 AM

But this solution feels flakey.

Consuming repo

https://github.com/Shopify/quilt/tree/main/packages/web-worker

Scope

  • Is this issue related to a specific package?

    • @shopify/web-worker
    • @shopify/react-web-worker

Checklist

  • Please delete the labels section before submitting your issue
  • I have described this issue in a way that is actionable (if possible)
@iSuslov iSuslov added the Type: Bug 🐛 Something isn't working label Apr 9, 2024
@iSuslov
Copy link
Author

iSuslov commented Apr 12, 2024

This configuration allows dev server to run:

import { WebWorkerPlugin } from "@shopify/web-worker/webpack";

/** @type {import('next').NextConfig} */
const nextConfig = {
    webpack: (config, { isServer, nextRuntime, dev }) => {
        if (!isServer) {
            config.plugins.push(
                new WebWorkerPlugin({
                    globalObject: "self",
                }),
            );
            config.module.rules.push({
                test: /\.(ts|tsx)/,
                exclude: /node_modules/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        babelrc: false,
                        plugins: ['@shopify/web-worker/babel'],
                        presets: ["@babel/preset-typescript"]
                    }
                }]
            })
        }
    }
}
export default nextConfig

But build still fails:

TypeError: finalInputFileSystem._writeVirtualFile is not a function
at VirtualModulesPlugin.writeModule (/Users/user/Projects/p/node_modules/webpack-virtual-modules/lib/index.js:155:30)
at Object.pitch (/Users/user/Projects/p/node_modules/@shopify/web-worker/build/cjs/webpack-parts/loader.js:88:29)

@iSuslov iSuslov closed this as completed Apr 12, 2024
@iSuslov iSuslov reopened this Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant