Permalink
Browse files

feat: Add beforeAssemble hook

closes #237
  • Loading branch information...
znck committed Jan 14, 2019
1 parent 08229a0 commit 2ae1bbf2b896a072cc724dddb8de75c428b52d64
Showing with 32 additions and 6 deletions.
  1. +4 −1 docs/faqs.md
  2. +7 −0 docs/options.md
  3. +21 −5 src/index.ts
@@ -4,4 +4,7 @@
The `__file` variable is used by devtools to provide "open in editor" feature. However, in production mode only filename is used. See issue [#258](https://github.com/vuejs/rollup-plugin-vue/issues/258) to enable production mode.

- **Cannot find module `vue-template-compiler`?**
`vue-template-compiler` has a constraint that it should be exact same version as `vue` that is why it is included as peer dependency. Make sure you install `vue-template-compiler` and `vue` in your project.
`vue-template-compiler` has a constraint that it should be exact same version as `vue` that is why it is included as peer dependency. Make sure you install `vue-template-compiler` and `vue` in your project.

- **Cannot find module `less` or `node-sass` or `stylus`?**
If you're using any of the style languages (other than css) supported in `.vue` file, you have to install that language's compiler.
@@ -180,3 +180,10 @@ The template render functions compilation supports a special transform `stripWit

- type: `string`
- default: `undefined`

## `beforeAssemble` __(dangerous)__

- type: `(descriptor: DescriptorCompileResult) => DescriptorCompileResult`
- default: `undefined`

A hook before blocks of SFC are assembled together.
@@ -13,6 +13,7 @@ import {
StyleOptions,
TemplateOptions,
StyleCompileResult,
DescriptorCompileResult,
} from '@vue/component-compiler'
import { Plugin } from 'rollup'
import * as path from 'path'
@@ -109,6 +110,8 @@ export interface VuePluginOptions {
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) module name or global function for custom style injector factory for SSR environment.
*/
styleInjectorSSR?: string

beforeAssemble?(descriptor: DescriptorCompileResult): DescriptorCompileResult
}
/**
* Rollup plugin for handling .vue files.
@@ -142,6 +145,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
(!blacklisted.has('*') || !blacklisted.has(customBlockType)) &&
(whitelisted.has('*') || whitelisted.has(customBlockType))

const beforeAssemble = opts.beforeAssemble || ((d: DescriptorCompileResult): DescriptorCompileResult => d)

delete opts.beforeAssemble
delete opts.css
delete opts.blackListCustomBlocks
delete opts.whiteListCustomBlocks
@@ -284,8 +290,12 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
)}'
export default script
// For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
script.__file = ${isProduction ? JSON.stringify(path.basename(filename)) : JSON.stringify(filename)}
`
script.__file = ${
isProduction
? JSON.stringify(path.basename(filename))
: JSON.stringify(filename)
}
`,
}
: { code: '' }

@@ -310,9 +320,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
.filter(Boolean)
}

input.script.code = input.script.code.replace(/^\s+/mg, '')
input.script.code = input.script.code.replace(/^\s+/gm, '')

const result = assemble(compiler, filename, input, opts)
const result = assemble(compiler, filename, beforeAssemble(input), opts)

descriptor.customBlocks.forEach((block, index) => {
if (!isAllowed(block.type)) return
@@ -328,7 +338,13 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
)}'`
})

dT(`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(result.map, null, 2)}\n`)
dT(
`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(
result.map,
null,
2
)}\n`
)

result.map = result.map || { mappings: '' }

0 comments on commit 2ae1bbf

Please sign in to comment.