This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
index.ts
180 lines (161 loc) · 5.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
try {
require.resolve('@vue/compiler-sfc')
} catch (e) {
throw new Error(
'rollup-plugin-vue requires @vue/compiler-sfc to be present in the dependency ' +
'tree.'
)
}
import {
SFCTemplateCompileOptions,
SFCAsyncStyleCompileOptions,
} from '@vue/compiler-sfc'
import fs from 'fs'
import createDebugger from 'debug'
import { Plugin } from 'rollup'
import { createFilter } from 'rollup-pluginutils'
import { transformSFCEntry } from './sfc'
import { transformTemplate } from './template'
import { transformStyle } from './style'
import { createCustomBlockFilter } from './utils/customBlockFilter'
import { getDescriptor, setDescriptor } from './utils/descriptorCache'
import { parseVuePartRequest } from './utils/query'
import { normalizeSourceMap } from './utils/sourceMap'
import { getResolvedScript } from './script'
const debug = createDebugger('rollup-plugin-vue')
export interface Options {
include: string | RegExp | (string | RegExp)[]
exclude: string | RegExp | (string | RegExp)[]
target: 'node' | 'browser'
exposeFilename: boolean
customBlocks?: string[]
// if true, handle preprocessors directly instead of delegating to other
// rollup plugins
preprocessStyles?: boolean
// sfc template options
templatePreprocessOptions?: Record<
string,
SFCTemplateCompileOptions['preprocessOptions']
>
compiler?: SFCTemplateCompileOptions['compiler']
compilerOptions?: SFCTemplateCompileOptions['compilerOptions']
transformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
// sfc style options
postcssOptions?: SFCAsyncStyleCompileOptions['postcssOptions']
postcssPlugins?: SFCAsyncStyleCompileOptions['postcssPlugins']
cssModulesOptions?: SFCAsyncStyleCompileOptions['modulesOptions']
preprocessCustomRequire?: SFCAsyncStyleCompileOptions['preprocessCustomRequire']
preprocessOptions?: SFCAsyncStyleCompileOptions['preprocessOptions']
}
const defaultOptions: Options = {
include: /\.vue$/,
exclude: [],
target: 'browser',
exposeFilename: false,
customBlocks: [],
}
export default function PluginVue(userOptions: Partial<Options> = {}): Plugin {
const options: Options = {
...defaultOptions,
...userOptions,
}
const isServer = options.target === 'node'
const isProduction =
process.env.NODE_ENV === 'production' || process.env.BUILD === 'production'
const rootContext = process.cwd()
const filter = createFilter(options.include, options.exclude)
const filterCustomBlock = createCustomBlockFilter(options.customBlocks)
return {
name: 'vue',
async resolveId(id, importer) {
const query = parseVuePartRequest(id)
if (query.vue) {
if (query.src) {
const resolved = await this.resolve(query.filename, importer, {
skipSelf: true,
})
if (resolved) {
setDescriptor(resolved.id, getDescriptor(importer!))
const [, originalQuery] = id.split('?', 2)
resolved.id += `?${originalQuery}`
return resolved
}
} else if (!filter(query.filename)) {
return null
}
debug(`resolveId(${id})`)
return id
}
return null
},
load(id) {
const query = parseVuePartRequest(id)
if (query.vue) {
if (query.src) {
return fs.readFileSync(query.filename, 'utf-8')
}
const descriptor = getDescriptor(query.filename)
if (descriptor) {
const block =
query.type === 'template'
? descriptor.template!
: query.type === 'script'
? getResolvedScript(descriptor, isServer)
: query.type === 'style'
? descriptor.styles[query.index]
: typeof query.index === 'number'
? descriptor.customBlocks[query.index]
: null
if (block) {
return {
code: block.content,
map: normalizeSourceMap(block.map, id),
}
}
}
}
return null
},
async transform(code, id) {
const query = parseVuePartRequest(id)
// *.vue file
// generate an entry module that imports the actual blocks of the SFC
if (!query.vue && filter(id)) {
debug(`transform SFC entry (${id})`)
const output = transformSFCEntry(
code,
id,
options,
rootContext,
isProduction,
isServer,
filterCustomBlock,
this
)
if (output) {
debug('SFC entry code:', '\n' + output.code + '\n')
}
return output
}
// sub request for blocks
if (query.vue) {
if (!query.src && !filter(query.filename)) {
return null
}
if (query.src) {
this.addWatchFile(query.filename)
}
if (query.type === 'template') {
debug(`transform template (${id})`)
return transformTemplate(code, id, options, query, this)
} else if (query.type === 'style') {
debug(`transform style (${id})`)
return transformStyle(code, id, options, query, isProduction, this)
}
}
return null
},
}
}
// overwrite for cjs require('rollup-plugin-vue')() usage
module.exports = PluginVue