oxc icon indicating copy to clipboard operation
oxc copied to clipboard

feat(minify): add directives option

Open suxin2017 opened this issue 1 month ago • 4 comments

https://terser.org/docs/options/

  • directives (default: true) -- remove redundant or non-standard directives

When the js module is of the Module type, it may output from ejs to cjs. Therefore, we add this configuration to keep it similar to terser

It is possible to fix the related issues

https://github.com/rolldown/rolldown/issues/6880 https://github.com/rolldown/rolldown/issues/6879

suxin2017 avatar Nov 14 '25 06:11 suxin2017

When the js module is of the Module type, it may output from ejs to cjs. Therefore, we add this configuration to keep it similar to terser

I don't think so. If the code is not ESM, then the code should be parsed with Script mode, which makes node.source_type.is_module() to return false.

sapphi-red avatar Nov 14 '25 06:11 sapphi-red

You're right. There is also a problem with detecting module type here

https://repl.rolldown.rs/#eNptjkEKwzAMBL8idPEl+AEu/YkvwbFLiisFW24DwX+v0tDSQy5adiVmtWFCt+FMU1xtuNfdELq/YMCg3mOrEaqUOYjHiydPcV24SLWJGa5gVIwnPY/opLTYByyc88QvsoEpzTcrP/zJ5ug5oDDFNLYssPniCYCbLE3c1wIkLo9RA6MfmiPsu3wGdu1+KiyPEqtgfwNltE1r

output

{ span: Span { start: 0, end: 35 }, source_text: ""use strict";\n\nexports.foo = 'foo'\n", comments: Vec([]), hashbang: None, directives: Vec([Directive { span: Span { start: 0, end: 13 }, expression: StringLiteral { span: Span { start: 0, end: 12 }, value: "use strict", raw: Some(""use strict""), lone_surrogates: false }, directive: "use strict" }]), body: Vec([ExpressionStatement(ExpressionStatement { span: Span { start: 15, end: 34 }, expression: AssignmentExpression(AssignmentExpression { span: Span { start: 15, end: 34 }, left: StaticMemberExpression(StaticMemberExpression { span: Span { start: 15, end: 26 }, object: Identifier(IdentifierReference { span: Span { start: 15, end: 22 }, name: "exports", reference_id: Cell { value: Some(ReferenceId(0)) } }), property: IdentifierName { span: Span { start: 23, end: 26 }, name: "foo" }, optional: false }), right: StringLiteral(StringLiteral { span: Span { start: 29, end: 34 }, value: "foo", raw: Some("'foo'"), lone_surrogates: false }), operator: Assign }) })]), scope_id: Cell { value: Some(ScopeId(0)) }, source_type: SourceType { language: JavaScript, module_kind: Module, variant: Standard } }

source_type module_kind = Module

suxin2017 avatar Nov 14 '25 06:11 suxin2017

At present, it seems that adding this configuration is still quite necessary

suxin2017 avatar Nov 14 '25 07:11 suxin2017

At present, it seems that adding this configuration is still quite necessary

can you elaborate your use case? I try to avoid adding options until it's necessary for a specific modern usecase.

Boshen avatar Nov 14 '25 14:11 Boshen

I think #15691 solved the same problem without a new option.

sapphi-red avatar Nov 15 '25 07:11 sapphi-red