What about having two buildings? You'll have the main building, the one that is currently banned while slavery is active. Add a second one that is:
- more expensive
- less productive (and/or with other malus)
- still provides the resource
- available at the same tech
- replaced by the equivalent "No Slavery" building
- only buildable with Slavery active (worldview slavery in city?)
In this way, you have an equivalent building that can be built while slavery is active, but if you don't have slavery, this weaker and more expensive equivalent building is replaced by the completely superior non-slavery building. Possibly, you could have the slavery-enabled building only buildable while the city has worldview slavery present?
As for the other resources, is there a reason they out-and-out obsolete at the factory tech, as opposed to just being replaced? Wouldn't that allow you to research the tech without those buildings simply vanishing? Does it cause issues with AI?
As for making slavery less viable as you progress, perhaps add further maluses to it, and reduce the output of its specialists, at the capstone/era change techs? Sort of the opposite of how higher tourism, education, etc autobuildings open up with new eras. I'm concerned this might be somewhat hidden from the player, though. While the adjustments would be listed in the tech, there won't be any easy way of communicating to future players that slavery becomes less and less productive (or has more and more negatives) unless they read through the era techs closely. I suppose it could be mentioned as part of slavery's strategy text in the pedia.