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

Commit

Permalink
feat: Add exposeFilename to control __file property
Browse files Browse the repository at this point in the history
The `exposeFilename` option is opt-in (defaults to `false`) as rollup-plugin-vue is mainly used to compile libraries and most libraries already
define `name` property so `__file` is not required.
  • Loading branch information
znck committed Feb 5, 2019
1 parent 5fb30ce commit 5c1dffb
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/index.ts
Expand Up @@ -83,6 +83,15 @@ export interface VuePluginOptions {
* ```
*/
css?: boolean
/**
* Expose filename in __file property.
* @default `false`
* @example
* ```js
* VuePlugin({ exposeFilename: true })
* ```
*/
exposeFilename?: boolean
compiler?: VueTemplateCompiler
compilerParseOptions?: VueTemplateCompilerParseOptions
sourceRoot?: string
Expand Down Expand Up @@ -154,8 +163,11 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
opts.beforeAssemble ||
((d: DescriptorCompileResult): DescriptorCompileResult => d)

const exposeFilename =
typeof opts.exposeFilename === 'boolean' ? opts.exposeFilename : false
delete opts.beforeAssemble
delete opts.css
delete opts.exposeFilename
delete opts.blackListCustomBlocks
delete opts.whiteListCustomBlocks
delete opts.defaultLang
Expand All @@ -171,9 +183,11 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
},
...opts.template
} as any

if (opts.template && typeof opts.template.isProduction === 'undefined') {
opts.template.isProduction = isProduction
}

const compiler = createDefaultCompiler(opts)
const descriptors = new Map<string, SFCDescriptor>()

Expand Down Expand Up @@ -229,6 +243,7 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {

async transform(source: string, filename: string) {
if (isVue(filename)) {
// Create deep copy to prevent issue during watching changes.
const descriptor: SFCDescriptor = JSON.parse(
JSON.stringify(
parse({
Expand Down Expand Up @@ -298,11 +313,16 @@ export default function vue(opts: VuePluginOptions = {}): Plugin {
'script'
)}'
export default script
${
exposeFilename
? `
// 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)
}`
: ''
}
`
}
Expand Down

0 comments on commit 5c1dffb

Please sign in to comment.