Skip to content

Build fails because "use client" is placed after "use strict" #152

@LucasStbnr

Description

@LucasStbnr

When I am using swc-plugin-coverage-instrument to calculate my coverage on my Next.js project,the build fails because /next-runtime-env/build/provider/env-provider.js and /next-runtime-env/build/provider/use-env-context.js have "use client" placed after "use strict".

Example :

// env-provider.js

"use strict";
'use client';
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvProvider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const env_context_1 = require("./env-context");
/**
 * Provides the environment variables to the application.
 *
 * Usage:
 * ```ts
 * <EnvProvider env={{ NODE_ENV: 'test', API_URL: 'http://localhost:3000' }}>
 *   <App />
 * </EnvProvider>
 * ```
 */
const EnvProvider = ({ children, env }) => {
    return (0, jsx_runtime_1.jsx)(env_context_1.EnvContext.Provider, { value: env, children: children });
};
exports.EnvProvider = EnvProvider;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZW52LXByb3ZpZGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3Byb3ZpZGVyL2Vudi1wcm92aWRlci50c3giXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFlBQVksQ0FBQzs7OztBQUtiLCtDQUEyQztBQU0zQzs7Ozs7Ozs7O0dBU0c7QUFDSSxNQUFNLFdBQVcsR0FBeUIsQ0FBQyxFQUFFLFFBQVEsRUFBRSxHQUFHLEVBQUUsRUFBRSxFQUFFO0lBQ3JFLE9BQU8sdUJBQUMsd0JBQVUsQ0FBQyxRQUFRLElBQUMsS0FBSyxFQUFFLEdBQUcsWUFBRyxRQUFRLEdBQXVCLENBQUM7QUFDM0UsQ0FBQyxDQUFDO0FBRlcsUUFBQSxXQUFXLGVBRXRCIn0=

The error :

Failed to compile.

./node_modules/next-runtime-env/build/provider/env-provider.js
Error: 
  × The "use client" directive must be placed before other expressions. Move it to the top of the file to resolve this issue.
   ╭─[/Users/lustoebn/Projects/nextweb/node_modules/next-runtime-env/build/provider/env-provider.js:1:1]
 1 │ "use strict";
 2 │ 'use client';
   · ─────────────
 3 │ Object.defineProperty(exports, "__esModule", { value: true });
 4 │ exports.EnvProvider = void 0;
 5 │ const jsx_runtime_1 = require("react/jsx-runtime");
   ╰────

When I manually invert the directives in the node_modules, it works as expected.

My Next.js config :

const nextConfig = {
    ...
    experimental: {
        swcPlugins: [['swc-plugin-coverage-instrument', { coverageVariable: '__coverage__' }]],
        workerThreads: false,
        cpus: 1,
    },
    ...
};

Versions used :
"swc-plugin-coverage-instrument": "0.0.24"
"next-runtime-env": "3.2.2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions