dprint / js-formatter

JS formatter for dprint Wasm plugins.
MIT License
31 stars 5 forks source link

wasm plugins do not build in next.js #9

Open tamird opened 9 months ago

tamird commented 9 months ago

In my application I have utility libraries that are shared between a typescript CLI program and a web app. The interface exposed by the various plugins (e.g. @dprint/markdown) doesn't work play nice with build tools like next.js which want to bundle everything statically. In particular, I have the following code:

const markdownFormatter = createFromBuffer(readFileSync(markdownPath()));
// https://dprint.dev/plugins/markdown/config/
markdownFormatter.setConfig(
  {
    lineWidth: 80,
  },
  {},
);
markdownFormatter.getConfigDiagnostics().forEach((diagnostic) => {
  console.error(
    `markdown formatter config diagnostics property=${diagnostic.propertyName}: ${diagnostic.message}`,
  );
});

const typescriptFormatter = createFromBuffer(readFileSync(typescriptPath()));
// https://dprint.dev/plugins/typescript/config/
typescriptFormatter.setConfig(
  {
    lineWidth: 110,
  },
  {
    'arrowFunction.useParentheses': 'force',
    quoteStyle: 'alwaysSingle',
  },
);

typescriptFormatter.getConfigDiagnostics().forEach((diagnostic) => {
  console.error(
    `typescript formatter config diagnostics property=${diagnostic.propertyName}: ${diagnostic.message}`,
  );
});

and attempting to build this with next.js produces:

Error: ENOENT: no such file or directory, open '/path/to/project/services/app/.next/server/chunks/plugin.wasm'
    at Object.openSync (node:fs:581:18)
    at readFileSync (node:fs:457:35)
    at 50509 (/path/to/project/services/app/.next/server/chunks/707.js:3:163)
    at t (/path/to/project/services/app/.next/server/webpack-runtime.js:1:143)
    at 19428 (/path/to/project/services/app/.next/server/app/(studio)/[orgName]/[projectName]/studio/page.js:236:5398)
    at Function.t (/path/to/project/services/app/.next/server/webpack-runtime.js:1:143)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async collectGenerateParams (/path/to/project/node_modules/.pnpm/next@14.0.4_@babel+core@7.23.6_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/utils.js:884:17) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/path/to/project/services/app/.next/server/chunks/plugin.wasm'
}

> Build error occurred
Error: Failed to collect page data for /[orgName]/[projectName]/studio
    at /path/to/project/node_modules/.pnpm/next@14.0.4_@babel+core@7.23.6_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/utils.js:1220:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: 'Error'
}
   Collecting page data  . ELIFECYCLE  Command failed with exit code 1.