Skip to content

Commit

Permalink
fix: apply loadable transformations before any other, fixes #466
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jan 2, 2020
1 parent cee8992 commit ac5ba45
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 42 deletions.
5 changes: 1 addition & 4 deletions examples/server-side-rendering/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ module.exports = api => {
},
],
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@loadable/babel-plugin'
],
plugins: ['@babel/plugin-syntax-dynamic-import', '@loadable/babel-plugin'],
}
}
21 changes: 9 additions & 12 deletions examples/server-side-rendering/src/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ const webStats = path.resolve(
'../../public/dist/web/loadable-stats.json',
)

app.get(
'*',
(req, res) => {
const nodeExtractor = new ChunkExtractor({ statsFile: nodeStats })
const { default: App } = nodeExtractor.requireEntrypoint()
app.get('*', (req, res) => {
const nodeExtractor = new ChunkExtractor({ statsFile: nodeStats })
const { default: App } = nodeExtractor.requireEntrypoint()

const webExtractor = new ChunkExtractor({ statsFile: webStats })
const jsx = webExtractor.collectChunks(<App />)
const webExtractor = new ChunkExtractor({ statsFile: webStats })
const jsx = webExtractor.collectChunks(<App />)

const html = renderToString(jsx)
const html = renderToString(jsx)

res.set('content-type', 'text/html')
res.send(`
res.set('content-type', 'text/html')
res.send(`
<!DOCTYPE html>
<html>
<head>
Expand All @@ -63,8 +61,7 @@ app.get(
</body>
</html>
`)
},
)
})

// eslint-disable-next-line no-console
app.listen(9000, () => console.log('Server started http://localhost:9000'))
22 changes: 14 additions & 8 deletions packages/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import importAsyncProperty from './properties/importAsync'
import requireAsyncProperty from './properties/requireAsync'
import requireSyncProperty from './properties/requireSync'
import resolveProperty from './properties/resolve'
import stateProperty from './properties/state';
import stateProperty from './properties/state'

const properties = [
stateProperty,
Expand Down Expand Up @@ -109,13 +109,19 @@ const loadablePlugin = api => {
return {
inherits: syntaxDynamicImport,
visitor: {
CallExpression(path) {
if (!isValidIdentifier(path)) return
transformImport(path)
},
'ArrowFunctionExpression|FunctionExpression|ObjectMethod': path => {
if (!hasLoadableComment(path)) return
transformImport(path)
Program: {
enter(programPath) {
programPath.traverse({
CallExpression(path) {
if (!isValidIdentifier(path)) return
transformImport(path)
},
'ArrowFunctionExpression|FunctionExpression|ObjectMethod': path => {
if (!hasLoadableComment(path)) return
transformImport(path)
},
})
},
},
},
}
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-plugin/src/properties/requireAsync.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
export default function requireAsyncProperty({types: t, template}) {

export default function requireAsyncProperty({ types: t, template }) {
const tracking = template.ast(`
const key = this.resolve(props)
this.resolved[key] = false
return this.importAsync(props).then(resolved => {
this.resolved[key] = true
return resolved;
});
`);
`)

return () =>
t.objectMethod(
Expand Down
8 changes: 2 additions & 6 deletions packages/babel-plugin/src/properties/state.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
export default function requireAsyncProperty({types: t}) {

export default function requireAsyncProperty({ types: t }) {
return () =>
t.objectProperty(
t.identifier('resolved'),
t.objectExpression([])
)
t.objectProperty(t.identifier('resolved'), t.objectExpression([]))
}
12 changes: 6 additions & 6 deletions packages/component/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"dist/loadable.cjs.js": {
"bundled": 12683,
"minified": 6125,
"gzipped": 2203
"bundled": 12664,
"minified": 6110,
"gzipped": 2195
},
"dist/loadable.esm.js": {
"bundled": 12300,
"minified": 5816,
"gzipped": 2135,
"bundled": 12281,
"minified": 5801,
"gzipped": 2126,
"treeshaked": {
"rollup": {
"code": 259,
Expand Down
4 changes: 3 additions & 1 deletion packages/component/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import hoistNonReactStatics from 'hoist-non-react-statics'

export function resolveComponent(loadedModule, { Loadable }) {
// eslint-disable-next-line no-underscore-dangle
const Component = loadedModule.__esModule ? loadedModule.default : (loadedModule.default || loadedModule)
const Component = loadedModule.__esModule
? loadedModule.default
: loadedModule.default || loadedModule
hoistNonReactStatics(Loadable, Component, {
preload: true,
})
Expand Down
10 changes: 8 additions & 2 deletions packages/server/src/ChunkExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ function getSriHtmlAttributes(asset) {
function assetToScriptTag(asset, extraProps) {
return `<script async data-chunk="${asset.chunk}" src="${
asset.url
}"${getSriHtmlAttributes(asset)}${extraPropsToString(asset, extraProps)}></script>`
}"${getSriHtmlAttributes(asset)}${extraPropsToString(
asset,
extraProps,
)}></script>`
}

function assetToScriptElement(asset, extraProps) {
Expand Down Expand Up @@ -135,7 +138,10 @@ function assetToLinkTag(asset, extraProps) {
const hint = LINK_ASSET_HINTS[asset.type]
return `<link ${hint}="${asset.chunk}" rel="${asset.linkType}" as="${
asset.scriptType
}" href="${asset.url}"${getSriHtmlAttributes(asset)}${extraPropsToString(asset, extraProps)}>`
}" href="${asset.url}"${getSriHtmlAttributes(asset)}${extraPropsToString(
asset,
extraProps,
)}>`
}

function assetToLinkElement(asset, extraProps) {
Expand Down

0 comments on commit ac5ba45

Please sign in to comment.