Skip to content

Instantly share code, notes, and snippets.

@ScriptedAlchemy
Created November 24, 2019 23:38
Show Gist options
  • Select an option

  • Save ScriptedAlchemy/6840c4b2486c3b266d3f2f7c474ebaa4 to your computer and use it in GitHub Desktop.

Select an option

Save ScriptedAlchemy/6840c4b2486c3b266d3f2f7c474ebaa4 to your computer and use it in GitHub Desktop.
Tree-shaking drops interleaved modules if not used
import React from 'react'
import Nav from './components/Menu' //Didnt use {MobileNav}, will be tree shaken
export default class App extends React {
render(){
return (<Nav>)
}
}
import React from 'react'
import ExternalImport,{importDependenciesOf} from 'webpack-external-import'
export default class App extends React {
render(){
// MobileNav isnt going to be there, webpack will tree-shake it out because app1 didnt use it
return (<ExternalImport src={importDependenciesOf('http://localhost:3002', 'somenamespace', 'Nav')} export="MobileNav"/>)
}
}
// Menu.js
import React from 'react';
const Nav = ({items}) => {
return (<nav><ul>{items.map(Item=><Item/>)}</ul></nav>)
}
// not used in App1, and will be tree-shaken as an unused export, but its actually used in App2
export const MobileNav = (things) => {
return (<nav>{things}</nav>)
}
export default Nav
// Marked with externalize comment, team intends for this file interface to be concumable by other applications
/*externalize:Nav*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment