pilcrowonpaper/oslo

Importing server actions using `oslo/password` from client components in Next.js causes Webpack errors

Opened this issue · 7 comments

Using oslo/password with the serverComponentsExternalPackages option in Next.js causes errors when you try to import server actions from "use client" components. Works fine when for server components. Looks to be the same issue as tursodatabase/libsql-client-ts#128.

Using esmExternals does not seem to fix the issue, and Oslo doesn't use top-level await.

next build error:

Failed to compile.

405.js from Terser
  x await isn't allowed in non-async function
       ,-[13857:1]
 13857 | /* harmony import */ var _crypto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5986);
 13858 | /* harmony import */ var oslo_cookie__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4757);
 13859 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo__WEBPACK_IMPORTED_MODULE_0__, oslo_cookie__WEBPACK_IMPORTED_MODULE_2__]);
 13860 | ([oslo__WEBPACK_IMPORTED_MODULE_0__, oslo_cookie__WEBPACK_IMPORTED_MODULE_2__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 13861 | 
 13862 | 
       `----

  x await isn't allowed in non-async function
       ,-[14013:1]
 14013 | /* harmony import */ var oslo_crypto__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1175);
 14014 | /* harmony import */ var oslo_random__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3333);
 14015 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo_encoding__WEBPACK_IMPORTED_MODULE_0__, oslo_crypto__WEBPACK_IMPORTED_MODULE_1__, oslo_random__WEBPACK_IMPORTED_MODULE_2__]);
 14016 | ([oslo_encoding__WEBPACK_IMPORTED_MODULE_0__, oslo_crypto__WEBPACK_IMPORTED_MODULE_1__, oslo_random__WEBPACK_IMPORTED_MODULE_2__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 14017 | 
 14018 | 
       `----

  x await isn't allowed in non-async function
       ,-[14084:1]
 14084 | /* harmony import */ var oslo_cookie__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4757);
 14085 | /* harmony import */ var oslo_request__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(6953);
 14086 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo__WEBPACK_IMPORTED_MODULE_2__, oslo_cookie__WEBPACK_IMPORTED_MODULE_3__, oslo_request__WEBPACK_IMPORTED_MODULE_4__]);
 14087 | ([oslo__WEBPACK_IMPORTED_MODULE_2__, oslo_cookie__WEBPACK_IMPORTED_MODULE_3__, oslo_request__WEBPACK_IMPORTED_MODULE_4__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 14088 | 
 14089 | 
       `----

Caused by:
    0: failed to parse input file
    1: Syntax Error
Error: 
  x await isn't allowed in non-async function
       ,-[13857:1]
 13857 | /* harmony import */ var _crypto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5986);
 13858 | /* harmony import */ var oslo_cookie__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4757);
 13859 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo__WEBPACK_IMPORTED_MODULE_0__, oslo_cookie__WEBPACK_IMPORTED_MODULE_2__]);
 13860 | ([oslo__WEBPACK_IMPORTED_MODULE_0__, oslo_cookie__WEBPACK_IMPORTED_MODULE_2__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 13861 | 
 13862 | 
       `----

  x await isn't allowed in non-async function
       ,-[14013:1]
 14013 | /* harmony import */ var oslo_crypto__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1175);
 14014 | /* harmony import */ var oslo_random__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3333);
 14015 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo_encoding__WEBPACK_IMPORTED_MODULE_0__, oslo_crypto__WEBPACK_IMPORTED_MODULE_1__, oslo_random__WEBPACK_IMPORTED_MODULE_2__]);
 14016 | ([oslo_encoding__WEBPACK_IMPORTED_MODULE_0__, oslo_crypto__WEBPACK_IMPORTED_MODULE_1__, oslo_random__WEBPACK_IMPORTED_MODULE_2__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 14017 | 
 14018 | 
       `----

  x await isn't allowed in non-async function
       ,-[14084:1]
 14084 | /* harmony import */ var oslo_cookie__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4757);
 14085 | /* harmony import */ var oslo_request__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(6953);
 14086 | var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([oslo__WEBPACK_IMPORTED_MODULE_2__, oslo_cookie__WEBPACK_IMPORTED_MODULE_3__, oslo_request__WEBPACK_IMPORTED_MODULE_4__]);
 14087 | ([oslo__WEBPACK_IMPORTED_MODULE_2__, oslo_cookie__WEBPACK_IMPORTED_MODULE_3__, oslo_request__WEBPACK_IMPORTED_MODULE_4__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);
       :                                                                                                                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 14088 | 
 14089 | 
       `----

Caused by:
    0: failed to parse input file
    1: Syntax Error


> Build failed because of webpack errors

next dev errors

 ⨯ SyntaxError: Unexpected identifier '__webpack_async_dependencies__'
    at __webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)
    at eval (./lib/auth.ts:6:63)
    at (action-browser)/./lib/auth.ts (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/app/login/page.js:417:1)
    at __webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)
    at eval (./actions/getAccount.ts:8:67)
    at (action-browser)/./actions/getAccount.ts (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/app/login/page.js:316:1)
    at Function.__webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)
 ⨯ SyntaxError: Unexpected identifier '__webpack_async_dependencies__'
    at __webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)
    at eval (./lib/auth.ts:6:63)
    at (action-browser)/./lib/auth.ts (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/app/login/page.js:417:1)
    at __webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)
    at eval (./actions/getAccount.ts:8:67)
    at (action-browser)/./actions/getAccount.ts (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/app/login/page.js:316:1)
    at Function.__webpack_require__ (/Users/root/code/lucia-nextjs-webpack-issue/.next/server/webpack-runtime.js:33:42)

Looks like this fixes the issue

const nextConfig = {
	webpack: (config) => {
		config.externals.push("@node-rs/argon2", "@node-rs/bcrypt");
		return config;
	}
};
Tow96 commented

A similar issue also pops up in jest:

error:
`
Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

C:\Users\joset\Documents\Proyectos\financeapp\node_modules\oslo\dist\password\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { Argon2id } from "./argon2id.js";
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

  17 | import { InsertUser, insertUserSchema, users } from './Schema';
  18 |
> 19 | export class AuthenticationModel {
     |                   ^
  20 |   private db = drizzle(dbClient, { schema });
  21 |
  22 |   register = async (user: InsertUser, password: string) => {

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
  at Object.<anonymous> (src/libs/feature-authentication/Model.ts:19:19)
  at Object.<anonymous> (src/libs/feature-authentication/index.ts:19:16)
  at Object.<anonymous> (src/libs/feature-authentication/__tests__/server/Middleware.spec.ts:7:32)

`

`
// jest.config.mjs
import nextJest from 'next/jest.js';

const createJestConfig = nextJest({
dir: './',
});

const clientTestConfig = await createJestConfig({
displayName: 'client',
testEnvironment: 'jest-environment-jsdom',
testMatch: ['/tests/client//*.[jt]s?(x)'],
setupFilesAfterEnv: ['/jest.setup.ts']
})();

const serverTestConfig = await createJestConfig({
displayName: 'server',
testEnvironment: 'node',
testMatch: ['/tests/server//*.[jt]s?(x)'],
})();

/** @type {import('jest').Config} /
const config = {
projects: [clientTestConfig, serverTestConfig],
collectCoverageFrom: [
'/src//
.[jt]s?(x)',
'!/node_modules/',
'!/.next/',
'!/coverage/',
'!/mocks/',
'!**/*.spec.[jt]s?(x)',

],
};

export default config;
`

I don't know if there is a way to pass the webpack config to jest

i have same error

Same error, and I am using turbopack experimental feature. and it doesn't have an equivalent for the webpack function...