-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($newVersion): add new babel plugin so you only call import(), su…
…pport for 2-file imports (js+cs
- Loading branch information
1 parent
7770e5d
commit 10c8a17
Showing
20 changed files
with
1,512 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"presets": ["es2015", "react", "stage-0"], | ||
"plugins": ["transform-flow-strip-types"] | ||
"plugins": ["universal-import", "transform-flow-strip-types"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import React from 'react' | ||
|
||
export default () => <div>fixture1</div> | ||
|
||
export const foo = 'bar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
foo: 'bar-es5' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export default 'hello' | ||
export const foo = 'bar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,88 @@ | ||
import path from 'path' | ||
import React from 'react' | ||
import { createComponent } from './index' | ||
import universalComponent from '../src' | ||
import { | ||
createComponent, | ||
createBablePluginComponent, | ||
createDynamicBablePluginComponent | ||
} from './index' | ||
import universal from '../src' | ||
|
||
export const createPath = name => path.join(__dirname, '../__fixtures__', name) | ||
|
||
export default isWebpack => { | ||
const importAsync = createComponent(40, null, new Error('test error')) | ||
export const createApp = isWebpack => { | ||
const importAsync = createComponent(40) | ||
const create = name => | ||
universalComponent(importAsync, { | ||
path: path.join(__dirname, '..', '__fixtures__', name), | ||
chunkName: name, | ||
resolve: isWebpack && (() => createPath(name)) | ||
universal(importAsync, { | ||
path: createPath(name), | ||
resolve: isWebpack && createPath(name), | ||
chunkName: name | ||
}) | ||
|
||
const Loadable1 = create('component') | ||
const Loadable2 = create('component2') | ||
const Loadable3 = create('component3') | ||
const Component1 = create('component') | ||
const Component2 = create('component2') | ||
const Component3 = create('component3') | ||
|
||
return props => | ||
<div> | ||
{props.one ? <Loadable1 /> : null} | ||
{props.two ? <Loadable2 /> : null} | ||
{props.three ? <Loadable3 /> : null} | ||
</div> | ||
(<div> | ||
{props.one ? <Component1 /> : null} | ||
{props.two ? <Component2 /> : null} | ||
{props.three ? <Component3 /> : null} | ||
</div>) | ||
} | ||
|
||
export const createDynamicApp = isWebpack => { | ||
const importAsync = createComponent(40) | ||
const Component = universal(importAsync, { | ||
path: ({ page }) => createPath(page), | ||
chunkName: ({ page }) => page, | ||
resolve: isWebpack && (({ page }) => createPath(page)) | ||
}) | ||
|
||
return props => | ||
(<div> | ||
{props.one ? <Component page='component' /> : null} | ||
{props.two ? <Component page='component2' /> : null} | ||
{props.three ? <Component page='component3' /> : null} | ||
</div>) | ||
} | ||
|
||
export const createBablePluginApp = isWebpack => { | ||
const create = name => { | ||
const importAsync = createBablePluginComponent( | ||
40, | ||
null, | ||
new Error('test error'), | ||
createPath(name) | ||
) | ||
return universal(importAsync, { testBabelPlugin: true }) | ||
} | ||
|
||
const Component1 = create('component') | ||
const Component2 = create('component2') | ||
const Component3 = create('component3') | ||
|
||
return props => | ||
(<div> | ||
{props.one ? <Component1 /> : null} | ||
{props.two ? <Component2 /> : null} | ||
{props.three ? <Component3 /> : null} | ||
</div>) | ||
} | ||
|
||
export const createDynamicBablePluginApp = isWebpack => { | ||
const create = name => { | ||
const importAsync = createDynamicBablePluginComponent() | ||
return universal(importAsync, { testBabelPlugin: true }) | ||
} | ||
|
||
const Component1 = create('component') | ||
const Component2 = create('component2') | ||
const Component3 = create('component3') | ||
|
||
return props => | ||
(<div> | ||
{props.one ? <Component1 page='component' /> : null} | ||
{props.two ? <Component2 page='component2' /> : null} | ||
{props.three ? <Component3 page='component3' /> : null} | ||
</div>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,80 @@ | ||
import path from 'path' | ||
import React from 'react' | ||
|
||
// fake delay so we can test different stages of async loading lifecycle | ||
export const waitFor = ms => new Promise(resolve => setTimeout(resolve, ms)) | ||
|
||
// normalize the required path so tests pass in all environments | ||
export const normalizePath = path => path.split('__fixtures__')[1] | ||
|
||
// fake delay so we can test different stages of async loading lifecycle | ||
export const waitFor = ms => new Promise(resolve => setTimeout(resolve, ms)) | ||
export const createPath = name => path.join(__dirname, '../__fixtures__', name) | ||
|
||
export const Loading = props => <p>Loading... {JSON.stringify(props)}</p> | ||
export const Err = props => <p>Error! {JSON.stringify(props)}</p> | ||
export const MyComponent = props => <p>MyComponent {JSON.stringify(props)}</p> | ||
export const MyComponent2 = props => <p>MyComponent {JSON.stringify(props)}</p> | ||
|
||
export const createComponent = (delay, Component, error) => async () => { | ||
export const createComponent = ( | ||
delay, | ||
Component, | ||
error = new Error('test error') | ||
) => async () => { | ||
await waitFor(delay) | ||
if (Component) return Component | ||
throw error | ||
} | ||
|
||
if (Component) { | ||
return Component | ||
export const createDynamicComponent = ( | ||
delay, | ||
components, | ||
error = new Error('test error') | ||
) => async (props, tools) => { | ||
await waitFor(delay) | ||
const Component = components[props.page] | ||
if (Component) return Component | ||
throw error | ||
} | ||
|
||
export const createBablePluginComponent = ( | ||
delay, | ||
Component, | ||
error = new Error('test error'), | ||
name | ||
) => { | ||
const asyncImport = async () => { | ||
await waitFor(delay) | ||
if (Component) return Component | ||
throw error | ||
} | ||
|
||
throw error | ||
return { | ||
chunkName: () => name, | ||
path: () => name, | ||
resolve: () => name, | ||
load: () => Promise.all([asyncImport()]), | ||
id: name, | ||
file: `${name}.js` | ||
} | ||
} | ||
|
||
export const createDynamicBablePluginComponent = ( | ||
delay, | ||
components, | ||
error = new Error('test error') | ||
) => { | ||
const asyncImport = async page => { | ||
await waitFor(delay) | ||
const Component = components[page] | ||
if (Component) return Component | ||
throw error | ||
} | ||
|
||
return ({ page }) => ({ | ||
chunkName: () => page, | ||
path: () => createPath(page), | ||
resolve: () => createPath(page), | ||
load: () => Promise.all([asyncImport(page)]), | ||
id: page, | ||
file: `${page}.js` | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.