Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 2ae1bbf

Browse files
committed
feat: Add beforeAssemble hook
closes #237
1 parent 08229a0 commit 2ae1bbf

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

docs/faqs.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
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.
55

66
- **Cannot find module `vue-template-compiler`?**
7-
`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.
7+
`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.
8+
9+
- **Cannot find module `less` or `node-sass` or `stylus`?**
10+
If you're using any of the style languages (other than css) supported in `.vue` file, you have to install that language's compiler.

docs/options.md

+7
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,10 @@ The template render functions compilation supports a special transform `stripWit
180180

181181
- type: `string`
182182
- default: `undefined`
183+
184+
## `beforeAssemble` __(dangerous)__
185+
186+
- type: `(descriptor: DescriptorCompileResult) => DescriptorCompileResult`
187+
- default: `undefined`
188+
189+
A hook before blocks of SFC are assembled together.

src/index.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
StyleOptions,
1414
TemplateOptions,
1515
StyleCompileResult,
16+
DescriptorCompileResult,
1617
} from '@vue/component-compiler'
1718
import { Plugin } from 'rollup'
1819
import * as path from 'path'
@@ -109,6 +110,8 @@ export interface VuePluginOptions {
109110
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) module name or global function for custom style injector factory for SSR environment.
110111
*/
111112
styleInjectorSSR?: string
113+
114+
beforeAssemble?(descriptor: DescriptorCompileResult): DescriptorCompileResult
112115
}
113116
/**
114117
* Rollup plugin for handling .vue files.
@@ -142,6 +145,9 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
142145
(!blacklisted.has('*') || !blacklisted.has(customBlockType)) &&
143146
(whitelisted.has('*') || whitelisted.has(customBlockType))
144147

148+
const beforeAssemble = opts.beforeAssemble || ((d: DescriptorCompileResult): DescriptorCompileResult => d)
149+
150+
delete opts.beforeAssemble
145151
delete opts.css
146152
delete opts.blackListCustomBlocks
147153
delete opts.whiteListCustomBlocks
@@ -284,8 +290,12 @@ export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
284290
)}'
285291
export default script
286292
// For security concerns, we use only base name in production mode. See https://github.com/vuejs/rollup-plugin-vue/issues/258
287-
script.__file = ${isProduction ? JSON.stringify(path.basename(filename)) : JSON.stringify(filename)}
288-
`
293+
script.__file = ${
294+
isProduction
295+
? JSON.stringify(path.basename(filename))
296+
: JSON.stringify(filename)
297+
}
298+
`,
289299
}
290300
: { code: '' }
291301

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

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

315-
const result = assemble(compiler, filename, input, opts)
325+
const result = assemble(compiler, filename, beforeAssemble(input), opts)
316326

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

331-
dT(`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(result.map, null, 2)}\n`)
341+
dT(
342+
`id: ${filename}\ncode:\n${result.code}\n\nmap:\n${JSON.stringify(
343+
result.map,
344+
null,
345+
2
346+
)}\n`
347+
)
332348

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

0 commit comments

Comments
 (0)