Skip to content

Comments

Inject "use strict" directive based on tsconfig alwaysStrict setting#8266

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-use-strict-to-cjs
Draft

Inject "use strict" directive based on tsconfig alwaysStrict setting#8266
Copilot wants to merge 3 commits intomainfrom
copilot/add-use-strict-to-cjs

Conversation

Copy link
Contributor

Copilot AI commented Feb 10, 2026

When bundling for non-ESM formats with tsconfig enabled and alwaysStrict: true (or strict: true), the output should include "use strict"; at the top of the bundle.

Changes

Core Infrastructure

  • Added tsconfig field to NormalizedBundlerOptions to track tsconfig configuration through the pipeline
  • Added resolver to GenerateContext and GenerateStage to enable tsconfig resolution during code generation

Directive Injection Logic

  • Implemented should_inject_use_strict() helper that:

    • Returns false for ESM format (already strict)
    • Returns false when tsconfig is disabled
    • Resolves tsconfig for entry module using oxc_resolver
    • Parses tsconfig.json to check alwaysStrict or strict compiler options
    • Respects include/exclude patterns via oxc_resolver's tsconfig resolution
  • Modified directive collection in ecma_generator.rs to prepend "use strict"; when appropriate, avoiding duplicates

Test Coverage

Created test fixtures for:

  • CJS with alwaysStrict: true
  • CJS with alwaysStrict: false
  • CJS with strict: true (implies alwaysStrict) ✓
  • IIFE with alwaysStrict: true
  • ESM with alwaysStrict: true (should not emit) ✓
  • Disabled tsconfig (should not emit) ✓

Known Issue

Format detection bug: ctx.options.format returns the global bundle format (ESM by default) rather than the per-output format. This causes the ESM format check to always trigger, blocking injection even for CJS/IIFE outputs. Need to identify where the per-chunk output format is stored and use that instead.

Debug output confirms:

DEBUG: format = Esm
DEBUG: Skipping ESM format

Even when output config specifies format: 'cjs' and the generated code contains exports object.

Original prompt

This section details on the original issue you should resolve

<issue_title>"use strict" is not injected for CJS builds</issue_title>
<issue_description>I've seen #4607, but I feel it's important to bring this up again.

I think this decision is quite unfortunate.

Is the expectation that developers will add 'use strict' to their ESM/TS source code when dual-bundling as ESM/CJS? Including 'use strict' in ESM code seems counterproductive.

An alternative might be to add 'use strict' to CJS files by default when "type": "module" is used and bundling for CJS.</issue_description>

<agent_instructions>> 1. Should this only apply when the output format is CJS?

This should apply to all formats (other than ESM as it already enables strict mode).

  1. Should this behavior only take effect when tsconfig is enabled? If the user does not enable tsconfig, should we avoid emitting "use strict"?

Yes. This behavior should only take effect when tsconfig is enabled.

  1. If the user enables tsconfig: 'auto' and we resolve the tsconfig.json from the current working directory, or if they explicitly specify tsconfig: '..', and the include / exclude configuration excludes the entry file, should we still respect the alwaysStrict setting from that tsconfig.json?

No. It should apply the alwaysStrict setting only when the other settings are respected. In other words, we should respect the include / exclude options.
</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@sapphi-red We are going to follow esbuild's behavior, which is to respect tsconfig.json's `strict` (`alwaysStrict`) option. When that is implemented, you should be able to output `"use strict"` by setting `"alwaysStrict": true` in tsconfig.json. If the tsconfig.json has `include` & `exclude`, the entrypoint should be included in them.

For now, if you add `"use strict"` to all entrypoints manually, Rolldown should emit `"use strict"`. @shulaoda I’d like to clarify a few points: 1. Should this only apply when the output format is CJS? 2. Should this behavior only take effect when `tsconfig` is enabled? If the user does not enable `tsconfig`, should we avoid emitting `"use strict"`? 3. If the user enables `tsconfig: 'auto'` and we resolve the `tsconfig.json` from the current working directory, or if they explicitly specify `tsconfig: '..'`, and the `include` / `exclude` configuration excludes the entry file, should we still respect the `alwaysStrict` setting from that `tsconfig.json`? @sapphi-red > 1. Should this only apply when the output format is CJS?

This should apply to all formats (other than ESM as it already enables strict mode).

  1. Should this behavior only take effect when tsconfig is enabled? If the user does not enable tsconfig, should we avoid emitting "use strict"?

Yes. This behavior should only take effect when tsconfig is enabled.

  1. If the user enables tsconfig: 'auto' and we resolve the tsconfig.json from the current working directory, or if they explicitly specify tsconfig: '..', and the include / exclude configuration excludes the entry file, should we still respect the alwaysStrict setting from that tsconfig.json?

No. It should apply the alwaysStrict setting only when the other settings are respected. In other words, we should respect the include / exclude options.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@netlify
Copy link

netlify bot commented Feb 10, 2026

Deploy Preview for rolldown-rs ready!

Name Link
🔨 Latest commit 05aa884
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/698b2e92ed3bf40008fef7ab
😎 Deploy Preview https://deploy-preview-8266--rolldown-rs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI changed the title [WIP] Add 'use strict' injection for CJS builds Inject "use strict" directive based on tsconfig alwaysStrict setting Feb 10, 2026
Copilot AI requested a review from shulaoda February 10, 2026 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"use strict" is not injected for CJS builds

2 participants