Here’s the text of the Prime Directive:
”Nothing within these articles of Federation shall authorize the United Federation of Planets to intervene in matters which are essentially the domestic jurisdiction of any planetary social system, or shall require the members to submit such matters to settlement under these Articles of Federation; But this principle shall not prejudice the application of enforcement measures under Chapter VII.
As the right of each sentient species to live in accordance with its normal cultural evolution is considered sacred, no Starfleet personnel may interfere with the normal and healthy development of alien life and culture. Such interference includes introducing superior knowledge, strength, or technology to a world whose society is incapable of handling such advantages wisely. Starfleet personnel may not violate this Prime Directive, even to save their lives and/or their ship, unless they are acting to right an earlier violation or an accidental contamination of said culture. This directive takes precedence over any and all other considerations, and carries with it the highest moral obligation.”
Clearly, violating a law on a non-Federation alien planet would subject a person to their law.
They did, eventually, violate the Prime Directive in order to transport everyone away and save (that annoying, obsinate) Wesley’s (pathetic little) life.