Skip to content

Commit

Permalink
feat: update to support named render function export
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 24, 2019
1 parent aa5530d commit 625b9bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/index.ts
Expand Up @@ -76,15 +76,16 @@ const loader: webpack.loader.Loader = function(source: string) {
const isServer = target === 'node'
const isProduction = mode === 'production'

let descriptor
try {
descriptor = parse(source, {
filename: resourcePath,
sourceMap
const { descriptor, errors } = parse(source, {
filename: resourcePath,
sourceMap
})

if (errors.length) {
errors.forEach(err => {
formatError(err, source, resourcePath)
loaderContext.emitError(err)
})
} catch (e) {
formatError(e, source, resourcePath)
loaderContext.emitError(e)
return ``
}

Expand Down Expand Up @@ -125,7 +126,7 @@ const loader: webpack.loader.Loader = function(source: string) {
const attrsQuery = attrsToQuery(descriptor.template.attrs)
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}${resourceQuery}`
templateRequest = stringifyRequest(src + query)
templateImport = `import render from ${templateRequest}`
templateImport = `import { render } from ${templateRequest}`
}

// script
Expand Down
13 changes: 9 additions & 4 deletions src/pitcher.ts
Expand Up @@ -83,17 +83,22 @@ pitcher.pitch = function() {

// Rewrite request. Technically this should only be done when we have deduped
// loaders. But somehow this is required for block source maps to work.
return genProxyModule(loaders, context)
return genProxyModule(loaders, context, query.type !== 'template')
}

function genProxyModule(
loaders: Loader[],
context: webpack.loader.LoaderContext
context: webpack.loader.LoaderContext,
exportDefault = true
) {
const request = genRequest(loaders, context)
// return a proxy module which simply re-exports everything from the
// actual request.
return `export { default } from ${request}; export * from ${request}`
// actual request. Note for template blocks the compiled module has no
// default export.
return (
(exportDefault ? `export { default } from ${request}; ` : ``) +
`export * from ${request}`
)
}

function genRequest(loaders: Loader[], context: webpack.loader.LoaderContext) {
Expand Down

0 comments on commit 625b9bb

Please sign in to comment.