|
1 |
| -import inline from './simple' |
2 |
| -import delegate from './delegate' |
| 1 | +import { |
| 2 | + createVueFilter, |
| 3 | + isVuePartRequest, |
| 4 | + createVuePartRequest, |
| 5 | + parseVuePartRequest, |
| 6 | + resolveVuePart |
| 7 | +} from './utils' |
| 8 | +import * as path from 'path' |
| 9 | +import { parse } from '@vue/component-compiler-utils' |
| 10 | +import { createDefaultCompiler, assemble } from '@vue/component-compiler' |
| 11 | +import hash from 'hash-sum' |
| 12 | +import { relative } from 'path' |
3 | 13 |
|
4 |
| -inline.delegate = delegate |
| 14 | +export default function vue(opts = {}) { |
| 15 | + const isVue = createVueFilter(opts.include, opts.exclude) |
| 16 | + const isProduction = process.env.NODE_ENV === 'production' |
| 17 | + createVuePartRequest.defaultLang = { |
| 18 | + ...createVuePartRequest.defaultLang, |
| 19 | + ...opts.defaultLang |
| 20 | + } |
5 | 21 |
|
6 |
| -export default inline |
| 22 | + const shouldExtractCss = opts.css === false |
| 23 | + const blacklisted = new Set(opts.blacklistCustomBlocks || ['*']) |
| 24 | + const whitelisted = new Set(opts.blacklistCustomBlocks || []) |
| 25 | + |
| 26 | + const isAllowed = any => |
| 27 | + (!blacklisted.has('*') || !blacklisted.has(any)) && |
| 28 | + (whitelisted.has('*') || whitelisted.has(any)) |
| 29 | + |
| 30 | + delete opts.css |
| 31 | + delete opts.blacklistCustomBlocks |
| 32 | + delete opts.defaultLang |
| 33 | + delete opts.include |
| 34 | + delete opts.exclude |
| 35 | + |
| 36 | + const compiler = createDefaultCompiler(opts) |
| 37 | + const descriptors = new WeakMap() |
| 38 | + |
| 39 | + return { |
| 40 | + name: 'vue.delegate', |
| 41 | + |
| 42 | + resolveId(id) { |
| 43 | + if (isVuePartRequest(id)) { |
| 44 | + const ref = parseVuePartRequest(id) |
| 45 | + const element = resolveVuePart(descriptors, ref) |
| 46 | + |
| 47 | + if (element.src && ref.meta.type !== 'styles') |
| 48 | + return path.resolve(path.dirname(ref.filename), element.src) |
| 49 | + |
| 50 | + return id |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + load(id) { |
| 55 | + if (!isVuePartRequest(id)) return |
| 56 | + |
| 57 | + id = parseVuePartRequest(id) |
| 58 | + const element = resolveVuePart(descriptors, id) |
| 59 | + |
| 60 | + return element.code || element.content |
| 61 | + }, |
| 62 | + |
| 63 | + async transform(source, filename) { |
| 64 | + if (isVue(filename)) { |
| 65 | + const descriptor = (descriptors[filename] = parse({ |
| 66 | + filename, |
| 67 | + source, |
| 68 | + needMap: true |
| 69 | + })) |
| 70 | + const scopeId = |
| 71 | + 'data-v-' + |
| 72 | + (isProduction |
| 73 | + ? hash(path.basename(filename) + source) |
| 74 | + : hash(filename + source)) |
| 75 | + const input = { |
| 76 | + scopeId, |
| 77 | + styles: descriptor.styles.map(style => |
| 78 | + compiler.compileStyle(filename, scopeId, style) |
| 79 | + ), |
| 80 | + customBlocks: [] |
| 81 | + } |
| 82 | + |
| 83 | + if (descriptor.template) { |
| 84 | + input.template = compiler.compileTemplate( |
| 85 | + filename, |
| 86 | + descriptor.template |
| 87 | + ) |
| 88 | + |
| 89 | + if (input.template.errors && input.template.errors.length) { |
| 90 | + console.error( |
| 91 | + '> Errors: ' + |
| 92 | + relative(process.cwd(), filename) + |
| 93 | + '\n' + |
| 94 | + input.template.errors.map(it => ' - ' + it).join('\n') |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + if (input.template.tips && input.template.tips.length) { |
| 99 | + console.log( |
| 100 | + '> Tips: ' + |
| 101 | + relative(process.cwd(), filename) + |
| 102 | + '\n' + |
| 103 | + input.template.tips.map(it => ' - ' + it).join('\n') |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + input.script = descriptor.script |
| 109 | + ? { |
| 110 | + code: ` |
| 111 | + export * from '${createVuePartRequest( |
| 112 | + filename, |
| 113 | + descriptor.script.lang, |
| 114 | + 'script' |
| 115 | + )}' |
| 116 | + import script from '${createVuePartRequest( |
| 117 | + filename, |
| 118 | + descriptor.script.lang, |
| 119 | + 'script' |
| 120 | + )}' |
| 121 | + export default script |
| 122 | + ` |
| 123 | + } |
| 124 | + : { code: '' } |
| 125 | + |
| 126 | + if (shouldExtractCss) { |
| 127 | + input.styles = input.styles |
| 128 | + .map((style, index) => { |
| 129 | + descriptor.styles[index].code = style.code |
| 130 | + |
| 131 | + input.script.code += |
| 132 | + '\n' + |
| 133 | + `import '${createVuePartRequest( |
| 134 | + filename, |
| 135 | + 'css', |
| 136 | + 'styles', |
| 137 | + index |
| 138 | + )}'` |
| 139 | + |
| 140 | + if (style.module) { |
| 141 | + return { ...style, code: '' } |
| 142 | + } |
| 143 | + }) |
| 144 | + .filter(Boolean) |
| 145 | + } |
| 146 | + |
| 147 | + const result = assemble(compiler, filename, input, opts) |
| 148 | + |
| 149 | + descriptor.customBlocks.forEach((block, index) => { |
| 150 | + if (!isAllowed(block.type)) return |
| 151 | + result.code += |
| 152 | + '\n' + |
| 153 | + `export * from '${createVuePartRequest( |
| 154 | + filename, |
| 155 | + block.attrs.lang || |
| 156 | + createVuePartRequest.defaultLang[block.type] || |
| 157 | + block.type, |
| 158 | + 'customBlocks', |
| 159 | + index |
| 160 | + )}'` |
| 161 | + }) |
| 162 | + |
| 163 | + return result |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments