The always-hot wire from the receptacle is spliced to the black wire on the fixture cable, and to a pigtail that connects back to the hot terminal on the receptacle. At the light fixture box, the black is spliced to the black from the switch cable. At the switch box the black wire is connected to the bottom terminal on the new switch.
This diagram shows the wiring for a new receptacle added from a light switch. The switch must have an always-hot wire for the source and a neutral wire must be present for the return path. The hot source is spliced with a pigtail back to the switch, and to a new 2-wire cable running to the new outlet. The neutral is spliced in the switch box to both the existing light, and the cable to the new receptacle.
At the switch, the other end of the black wire is connected to the bottom terminal on the switch and the black wire going to the new light is connected to the top terminal. The white wire from the receptacle is spliced to the white wire going to the light, it doesn’t connect to the switch in this diagram, but a new electronic switch will usually include a neutral wire connection. At the light the black wire connects to the hot terminal on the fixture and the white goes to the neutral terminal.
This diagram illustrates wiring for a circuit breaker with a built-in ground fault circuit interrupter. This 20 amp ground fault interrupter breaker is a form of gfci that can be installed at the circuit source. This kind of circuit is used for dishwashers, whirlpool spas and other locations where water contact is likely.