Directive
JavaScript has a feature called directive, which is used to annotate a part of the code.
Rolldown may not be able to preserve the semantics related to directives, here are the strategies when handling directives.
"use strict"
The "use strict" directive is a directive that tells the JavaScript engine to enforce strict mode. Because keeping the top-level "use strict" directive semantics is complicated and requires a bigger output size, Rolldown may not keep them.
Since ES modules are always in strict mode, Rolldown does not output any "use strict" directive for output.format: 'esm'. As a side note, this means code that is not in strict mode are forced to be in strict mode for ES module format output.
When output.format is not esm, Rolldown will output the "use strict" directive for any of the following cases:
- The directive is not in the top-level scope and not inside strict mode scope (REPL)
- The directive is in the top-level scope and the module is a entry module (REPL)
- The directive is in the top-level scope and
output.preserveModulesis enabled (REPL)
If you want to append "use strict" to all files, you can use the output.intro option:
import { defineConfig } from 'rolldown';
export default defineConfig({
output: {
intro: "'use strict';",
},
});Other directives
The ECMAScript specification allows implementations to define additional directives. Since those additional directives are not part of the specification, Rolldown does not know the semantics of them. Rolldown assumes that they follow the similar semantics as "use strict". But for the same reason as above, Rolldown may not preserve the top-level directives.
Rolldown will output the directive for any of the following cases:
- The directive is not in the top-level scope (REPL)
- The directive is in the top-level scope and the module is a entry module (REPL)
- The directive is in the top-level scope and
output.preserveModulesis enabled (REPL)
If you want to append custom directive to all files, you can use the output.banner option:
import { defineConfig } from 'rolldown';
export default defineConfig({
output: {
banner: "'use client';",
},
});