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

Commit 8335566

Browse files
committed
Revert "fix: call transformer from whitelisted custom blocks (#310)"
This reverts commit ab13f3b.
1 parent ddcd5fa commit 8335566

File tree

6 files changed

+45
-130
lines changed

6 files changed

+45
-130
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"runtime/"
4747
],
4848
"dependencies": {
49-
"@vue/component-compiler": "^4.2.0",
49+
"@vue/component-compiler": "^4.1.0",
5050
"@vue/component-compiler-utils": "^3.0.0",
5151
"debug": "^4.1.1",
5252
"hash-sum": "^1.0.2",

src/index.ts

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {
44
parseVuePartRequest,
55
resolveVuePart,
66
isVuePartRequest,
7-
transformRequireToImport,
8-
DEFAULT_LANGS
7+
transformRequireToImport
98
} from './utils'
109
import {
1110
createDefaultCompiler,
@@ -184,15 +183,11 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
184183
if (!opts.styleInjectorShadow)
185184
opts.styleInjectorShadow = '~' + require.resolve('../runtime/shadow')
186185

187-
const defaultLang: Record<string, string> = {
188-
...DEFAULT_LANGS,
186+
createVuePartRequest.defaultLang = {
187+
...createVuePartRequest.defaultLang,
189188
...opts.defaultLang
190189
}
191190

192-
if (opts.defaultLang && typeof opts.defaultLang.styles === 'string') {
193-
defaultLang.style = opts.defaultLang.styles
194-
}
195-
196191
const shouldExtractCss = opts.css === false
197192
const customBlocks: string[] = []
198193

@@ -274,10 +269,10 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
274269

275270
resolveId(id, importer) {
276271
const request = id
277-
278-
if (!importer) return
272+
273+
if (!importer) return
279274
if (!isVuePartRequest(id)) return
280-
275+
281276
id = path.resolve(path.dirname(importer), id)
282277
const ref = parseVuePartRequest(id)
283278

@@ -312,12 +307,7 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
312307
let map = element.map as any
313308

314309
if (request.meta.type === 'styles') {
315-
code = prependStyle(
316-
id,
317-
request.meta.lang || defaultLang.style,
318-
code,
319-
map
320-
).code
310+
code = prependStyle(id, request.meta.lang, code, map).code
321311
}
322312

323313
dL(`id: ${id}\ncode: \n${code}\nmap: ${JSON.stringify(map, null, 2)}\n\n`)
@@ -354,7 +344,7 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
354344
if (style.content) {
355345
style.content = prependStyle(
356346
filename,
357-
style.lang || defaultLang.style,
347+
style.lang || 'css',
358348
style.content,
359349
style.map
360350
).code
@@ -398,12 +388,12 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
398388
code: `
399389
export * from '${createVuePartRequest(
400390
filename,
401-
descriptor.script.lang || defaultLang.script,
391+
descriptor.script.lang || 'js',
402392
'script'
403393
)}'
404394
import script from '${createVuePartRequest(
405395
filename,
406-
descriptor.script.lang || defaultLang.script,
396+
descriptor.script.lang || 'js',
407397
'script'
408398
)}'
409399
export default script
@@ -443,23 +433,22 @@ export default function vue(opts: Partial<VuePluginOptions> = {}): Plugin {
443433
.filter(Boolean)
444434
}
445435

446-
// Why?
447436
input.script.code = input.script.code.replace(/^\s+/gm, '')
448437

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

451440
descriptor.customBlocks.forEach((block, index) => {
452441
if (!isAllowed(block.type)) return
453-
const lang =
454-
typeof block.attrs.lang === 'string'
455-
? block.attrs.lang
456-
: defaultLang[block.type] || block.type
457-
const id = createVuePartRequest(filename, lang, block.type, index)
458442
result.code +=
459443
'\n' +
460-
`export * from '${id}'\n` +
461-
`import __custom_block_${index}__ from '${id}'\n` +
462-
`__custom_block_${index}__(__vue_component__)`
444+
`export * from '${createVuePartRequest(
445+
filename,
446+
(typeof block.attrs.lang === 'string' && block.attrs.lang) ||
447+
createVuePartRequest.defaultLang[block.type] ||
448+
block.type,
449+
'customBlocks',
450+
index
451+
)}'`
463452
})
464453

465454
dT(

src/utils.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export interface VuePartRequestMeta {
2121
index?: number
2222
}
2323

24+
export interface VuePartRequestCreator {
25+
(filename: string, lang: string, type: string, index?: number): string
26+
27+
defaultLang: {
28+
[key: string]: string
29+
}
30+
}
31+
2432
export function createVueFilter(
2533
include: Array<string | RegExp> | string | RegExp = [/\.vue$/i],
2634
exclude: Array<string | RegExp> | string | RegExp = []
@@ -41,11 +49,7 @@ export function getVueMetaFromQuery(id: string): VuePartRequestMeta | null {
4149
? (query[PARAM_NAME] as any)[0]
4250
: query[PARAM_NAME]) as string
4351

44-
let [type, index, lang] = data.split('.')
45-
46-
if (!/^(template|styles|script)$/i.test(type)) {
47-
type = 'customBlocks'
48-
}
52+
const [type, index, lang] = data.split('.')
4953

5054
return (lang
5155
? { type, lang, index: parseInt(index) } // styles.0.css
@@ -60,13 +64,13 @@ export function isVuePartRequest(id: string): boolean {
6064
return getVueMetaFromQuery(id) !== null
6165
}
6266

63-
export function createVuePartRequest(
67+
export const createVuePartRequest: VuePartRequestCreator = ((
6468
filename: string,
6569
lang: string | undefined,
6670
type: string,
6771
index?: number
68-
): string {
69-
lang = DEFAULT_LANGS[type] || lang
72+
): string => {
73+
lang = lang || createVuePartRequest.defaultLang[type]
7074

7175
const match = GET_QUERY.exec(filename)
7276

@@ -77,14 +81,12 @@ export function createVuePartRequest(
7781
.join('.')
7882

7983
return `${path.basename(filename)}?${queryString.stringify(query)}`
80-
}
84+
}) as VuePartRequestCreator
8185

82-
export const DEFAULT_LANGS: Record<string, string> = {
86+
createVuePartRequest.defaultLang = {
8387
template: 'html',
84-
style: 'css',
85-
script: 'js',
86-
docs: 'md',
87-
i18n: 'json'
88+
styles: 'css',
89+
script: 'js'
8890
}
8991

9092
export function parseVuePartRequest(id: string): VuePartRequest | undefined {

test/options/__snapshots__/custom-blocks.spec.ts.snap

Lines changed: 0 additions & 52 deletions
This file was deleted.

test/options/custom-blocks.spec.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import vue, { VuePluginOptions } from '../../src'
22
import { pluginInline } from '../setup/plugins'
33
import { rollup } from 'rollup'
4-
function pluginText() {
5-
return {
6-
name: 'text',
7-
transform(source: string, id: string) {
8-
if (/\.(md|txt)$/.test(id)) {
9-
return `export default ${JSON.stringify(source.trim())}`
10-
}
11-
},
12-
}
13-
}
144

155
describe('customBlocks', () => {
166
async function setup(options?: Partial<VuePluginOptions>) {
@@ -31,16 +21,11 @@ describe('customBlocks', () => {
3121
</docs>
3222
`
3323
),
34-
pluginText(),
3524
vue({
3625
...options,
37-
defaultLang: {
38-
docs: 'md',
39-
custom: 'txt',
40-
},
41-
normalizer: 'vue-runtime-helpers/dist/normalize-component.mjs',
42-
}),
43-
],
26+
normalizer: 'vue-runtime-helpers/dist/normalize-component.mjs'
27+
})
28+
]
4429
})
4530
.then(bundle => bundle.generate({ format: 'es' }))
4631
.then(generated => generated.output[0])
@@ -55,15 +40,15 @@ describe('customBlocks', () => {
5540

5641
it('array of tags', async () => {
5742
const { code } = await setup({
58-
customBlocks: ['custom'],
43+
customBlocks: ['custom']
5944
})
6045

6146
expect(code).toEqual(expect.stringContaining('My Custom Block'))
6247
expect(code).not.toEqual(expect.stringContaining('My Docs Block'))
6348
})
6449
it('negative array of tags', async () => {
6550
const { code } = await setup({
66-
customBlocks: ['*', '!custom'],
51+
customBlocks: ['*', '!custom']
6752
})
6853

6954
expect(code).not.toEqual(expect.stringContaining('My Custom Block'))
@@ -73,19 +58,10 @@ describe('customBlocks', () => {
7358
const { code } = await setup({
7459
customBlocks(tag) {
7560
return tag === 'custom'
76-
},
61+
}
7762
})
7863

7964
expect(code).toEqual(expect.stringContaining('My Custom Block'))
8065
expect(code).not.toEqual(expect.stringContaining('My Docs Block'))
8166
})
82-
83-
it('transform', async () => {
84-
const { code } = await setup({
85-
customBlocks: ['docs'],
86-
})
87-
88-
expect(code).toEqual(expect.stringContaining('__custom_block_1__(__vue_component__)'))
89-
expect(code).toMatchSnapshot()
90-
})
9167
})

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,10 +1571,10 @@
15711571
source-map "~0.6.1"
15721572
vue-template-es2015-compiler "^1.9.0"
15731573

1574-
"@vue/component-compiler@^4.2.0":
1575-
version "4.2.0"
1576-
resolved "https://registry.yarnpkg.com/@vue/component-compiler/-/component-compiler-4.2.0.tgz#437855cd59f3d713a4eef81bac7ab0f4950977b4"
1577-
integrity sha512-bxFNxUpKzLfHDoGTsAe2w7gEz4OwII7tp5m7sAXES1DApbpYglH4YSpYxdZRZ4GN/wj2fPD0u72QRJXd4UPvFQ==
1574+
"@vue/component-compiler@^4.1.0":
1575+
version "4.1.0"
1576+
resolved "https://registry.yarnpkg.com/@vue/component-compiler/-/component-compiler-4.1.0.tgz#92ccb90e425aa7e61d58bf092a5cfcdd6d0f9315"
1577+
integrity sha512-20S7mm7CYP94m2Morw2ftz1tqoBu1nX7KYiqo5rlgKPZ0dlY7VZX7wAL/etN3s4HD0PBeenr1pUUCBIgGSaB2g==
15781578
dependencies:
15791579
"@vue/component-compiler-utils" "^3.0.0"
15801580
clean-css "^4.1.11"

0 commit comments

Comments
 (0)