Skip to content

[Compiler] Enhancement - per file control over compiled file extension #4170

@shayanhabibi

Description

@shayanhabibi

Introduction

The capacity to control the file extension of a file would allow us to have files compile to JavaScript configuration files in a build process. The use case in other target languages is unknown to me.

Essentially, there are many frameworks, libraries, and others which depend on a specific file name and extension schema for their configuration files.

This might not coincide with your target (such as compiling to JSX,), and so this would normally require you to configure these files in JavaScript.

My intention is to remove this obstruction by allowing explicit control of a files compiled extension.

Issues

Safety issues with regards to the capacity for a malicious library to inject shell scripts or other by compiling to a non-intended target extension when a consumer compiles it.

This might need to be an unsafe switch on the fable compiler to permit.

Solutions

Plugin Only

We could expose a method or mutable member of the PluginHelper which allows a plugin to set the intended file extension.

We could perhaps expose this as a method which accepts a union of predefined extensions (such as .config.js, .config.jsx, .jsx, .js etc). This would remove any issues regarding safety.

Top File Directives

We can modify the file read method in the compilation process to first peek the stream for #. With the presence of #, it would consume the first line (and if future directives were added, could continue until no more directives are available).

The line would be parsed for a valid key,value pair such as FABLE_FILE_EXT ".js" and provide that, wrapped in an Option, in a tuple with the complete file text. In the case where it is not a valid key,value pair, we consume the rest of the file, append the consumed line, and return the tuple with a None value .

This optional value would be utilised when writing the file to disk.

The advantage is that we could also perform the peek/read separate to the entire file consumption. In that case, we would be able to force the stream to position 0 without being concerned about dissecting characters.

The issue with this would be that we won't have access to typed unions as parameters. We could still enforce restriction on the file extensions.

Absurd Gimmicks

As a gimmick, we would be able to explicitly compile a .json file extension, therefore having capacity to create package.json through fable, whether with a key,value list or just embedded JS 😂.

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