Skip to content

Commit ac4941f

Browse files
authored
feat: Vue3 migration (#28)
BREAKING CHANGE: - Require Vue 3
1 parent 1c7617e commit ac4941f

File tree

4 files changed

+123
-41
lines changed

4 files changed

+123
-41
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"lint": "xo",
1919
"prepublishOnly": "npm run build",
2020
"build": "npm run build:umd && npm run build:cjs",
21-
"build:umd": "bili --file-name \"vue-prism-component[min].js\" --format umd --format umd-min --env.NODE_ENV production --module-name PrismComponent --external prismjs",
21+
"build:umd": "bili --file-name \"vue-prism-component[min].js\" --format umd --format umd-min --env.NODE_ENV production --module-name PrismComponent --external prismjs --external vue",
2222
"build:cjs": "bili --file-name \"vue-prism-component.common.js\" --format cjs"
2323
},
2424
"author": "egoist <0x142857@gmail.com>",
@@ -28,6 +28,7 @@
2828
"bili": "^4.9.0",
2929
"eslint-config-rem": "^3.0.0",
3030
"prismjs": "^1.6.0",
31+
"vue": "^3.0.4",
3132
"xo": "^0.18.0"
3233
},
3334
"xo": {

src/index.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { h, defineComponent } from 'vue'
12
import Prism from 'prismjs'
2-
import { assign } from './utils'
33

4-
export default {
5-
functional: true,
4+
export default defineComponent({
65
props: {
76
code: {
87
type: String
@@ -16,12 +15,14 @@ export default {
1615
default: 'markup'
1716
}
1817
},
19-
render(h, ctx) {
18+
setup(props, { slots, attrs }) {
19+
const defaultSlot = (slots && slots.default && slots.default()) || []
2020
const code =
21-
ctx.props.code ||
22-
(ctx.children && ctx.children.length > 0 ? ctx.children[0].text : '')
23-
const inline = ctx.props.inline
24-
const language = ctx.props.language
21+
props.code || (defaultSlot && defaultSlot.length)
22+
? defaultSlot[0].children
23+
: ''
24+
const inline = props.inline
25+
const language = props.language
2526
const prismLanguage = Prism.languages[language]
2627
const className = `language-${language}`
2728

@@ -30,32 +31,28 @@ export default {
3031
`Prism component for language "${language}" was not found, did you forget to register it? See all available ones: https://cdn.jsdelivr.net/npm/prismjs/components/`
3132
)
3233
}
34+
return () => {
35+
if (inline) {
36+
return h('code', {
37+
class: [className],
38+
innerHTML: Prism.highlight(code, prismLanguage)
39+
})
40+
}
3341

34-
if (inline) {
3542
return h(
36-
'code',
37-
assign({}, ctx.data, {
38-
class: [ctx.data.class, className],
39-
domProps: assign({}, ctx.data.domProps, {
43+
'pre',
44+
{
45+
...attrs,
46+
class: [attrs.class, className]
47+
},
48+
[
49+
h('code', {
50+
...attrs,
51+
class: [attrs.class, className],
4052
innerHTML: Prism.highlight(code, prismLanguage)
4153
})
42-
})
54+
]
4355
)
4456
}
45-
46-
return h(
47-
'pre',
48-
assign({}, ctx.data, {
49-
class: [ctx.data.class, className]
50-
}),
51-
[
52-
h('code', {
53-
class: className,
54-
domProps: {
55-
innerHTML: Prism.highlight(code, prismLanguage)
56-
}
57-
})
58-
]
59-
)
6057
}
61-
}
58+
})

src/utils.js

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

yarn.lock

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@
236236
dependencies:
237237
"@babel/types" "^7.8.3"
238238

239+
"@babel/helper-validator-identifier@^7.12.11":
240+
version "7.12.11"
241+
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.12.11.tgz#c9a1f021917dcb5ccf0d4e453e399022981fc9ed"
242+
integrity sha512-np/lG3uARFybkoHokJUmf1QfEvRVCPbmQeUQpKow5cQ3xWrV9i3rUHodKDJPQfTVX61qKi+UdYk8kik84n7XOw==
243+
239244
"@babel/helper-validator-identifier@^7.9.0", "@babel/helper-validator-identifier@^7.9.5":
240245
version "7.9.5"
241246
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.9.5.tgz#90977a8e6fbf6b431a7dc31752eee233bf052d80"
@@ -269,6 +274,11 @@
269274
chalk "^2.0.0"
270275
js-tokens "^4.0.0"
271276

277+
"@babel/parser@^7.12.0":
278+
version "7.12.11"
279+
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.11.tgz#9ce3595bcd74bc5c466905e86c535b8b25011e79"
280+
integrity sha512-N3UxG+uuF4CMYoNj8AhnbAcJF0PiuJ9KHuy1lQmkYsxTer/MAH9UBNHsBoAX/4s6NvlDD047No8mYVGGzLL4hg==
281+
272282
"@babel/parser@^7.8.6", "@babel/parser@^7.9.0":
273283
version "7.9.4"
274284
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.9.4.tgz#68a35e6b0319bbc014465be43828300113f2f2e8"
@@ -804,6 +814,15 @@
804814
globals "^11.1.0"
805815
lodash "^4.17.13"
806816

817+
"@babel/types@^7.12.0":
818+
version "7.12.11"
819+
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.12.11.tgz#a86e4d71e30a9b6ee102590446c98662589283ce"
820+
integrity sha512-ukA9SQtKThINm++CX1CwmliMrE54J6nIYB5XTwL5f/CLFW9owfls+YSU8tVW15RQ2w+a3fSbPjC6HdQNtWZkiA==
821+
dependencies:
822+
"@babel/helper-validator-identifier" "^7.12.11"
823+
lodash "^4.17.19"
824+
to-fast-properties "^2.0.0"
825+
807826
"@babel/types@^7.4.4", "@babel/types@^7.8.3", "@babel/types@^7.8.6", "@babel/types@^7.9.0", "@babel/types@^7.9.5":
808827
version "7.9.5"
809828
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.9.5.tgz#89231f82915a8a566a703b3b20133f73da6b9444"
@@ -835,6 +854,54 @@
835854
dependencies:
836855
"@types/node" "*"
837856

857+
"@vue/compiler-core@3.0.4":
858+
version "3.0.4"
859+
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.0.4.tgz#0122aca6eada4cb28b39ed930af917444755e330"
860+
integrity sha512-snpMICsbWTZqBFnPB03qr4DtiSxVYfDF3DvbDSkN9Z9NTM8Chl8E/lYhKBSsvauq91DAWAh8PU3lr9vrLyQsug==
861+
dependencies:
862+
"@babel/parser" "^7.12.0"
863+
"@babel/types" "^7.12.0"
864+
"@vue/shared" "3.0.4"
865+
estree-walker "^2.0.1"
866+
source-map "^0.6.1"
867+
868+
"@vue/compiler-dom@3.0.4":
869+
version "3.0.4"
870+
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.0.4.tgz#834fd4b15c5698cf9f4505c2bfbccca058a843eb"
871+
integrity sha512-FOxbHBIkkGjYQeTz1DlXQjS1Ms8EPXQWsdTdTPeohoS0KzCz6RiOjiAG+jLtMi6Nr5GX2h0TlCvcnI8mcsicFQ==
872+
dependencies:
873+
"@vue/compiler-core" "3.0.4"
874+
"@vue/shared" "3.0.4"
875+
876+
"@vue/reactivity@3.0.4":
877+
version "3.0.4"
878+
resolved "https://registry.yarnpkg.com/@vue/reactivity/-/reactivity-3.0.4.tgz#b6599dd8271a745960a03f05744ccf7991ba5d8d"
879+
integrity sha512-AFTABrLhUYZY2on3ea9FxeXal7w3f6qIp9gT+/oG93H7dFTL5LvVnxygCopv7tvkIl/GSGQb/yK1D1gmXx1Pww==
880+
dependencies:
881+
"@vue/shared" "3.0.4"
882+
883+
"@vue/runtime-core@3.0.4":
884+
version "3.0.4"
885+
resolved "https://registry.yarnpkg.com/@vue/runtime-core/-/runtime-core-3.0.4.tgz#a5b9a001560b1fd8c01a43f68b764c555de7836c"
886+
integrity sha512-qH9e4kqU7b3u1JewvLmGmoAGY+mnuBqz7aEKb2mhpEgwa1yFv496BRuUfMXXMCix3+TndUVMJ8jt41FSdNppwg==
887+
dependencies:
888+
"@vue/reactivity" "3.0.4"
889+
"@vue/shared" "3.0.4"
890+
891+
"@vue/runtime-dom@3.0.4":
892+
version "3.0.4"
893+
resolved "https://registry.yarnpkg.com/@vue/runtime-dom/-/runtime-dom-3.0.4.tgz#6f81aec545f24511d2c28a315aa3391420b69c68"
894+
integrity sha512-BGIoiTSESzWUhN0Ofi2X/q+HN8f6IUFmUEyyBGKbmx7DTAJNZhFfjqsepfXQrM5IGeTfJLB1ZEVyroDQJNXq3g==
895+
dependencies:
896+
"@vue/runtime-core" "3.0.4"
897+
"@vue/shared" "3.0.4"
898+
csstype "^2.6.8"
899+
900+
"@vue/shared@3.0.4":
901+
version "3.0.4"
902+
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.4.tgz#6dc50f593bdfdeaa6183d1dbc15e2d45e7c6b8b3"
903+
integrity sha512-Swfbz31AaMX48CpFl+YmIrqOH9MgJMTrltG9e26A4ZxYx9LjGuMV+41WnxFzS3Bc9nbrc6sDPM37G6nIT8NJSg==
904+
838905
acorn-dynamic-import@^4.0.0:
839906
version "4.0.0"
840907
resolved "https://registry.yarnpkg.com/acorn-dynamic-import/-/acorn-dynamic-import-4.0.0.tgz#482210140582a36b83c3e342e1cfebcaa9240948"
@@ -1182,9 +1249,9 @@ caniuse-api@^3.0.0:
11821249
lodash.uniq "^4.5.0"
11831250

11841251
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001038:
1185-
version "1.0.30001040"
1186-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001040.tgz#103fc8e6eb1d7397e95134cd0e996743353d58ea"
1187-
integrity sha512-Ep0tEPeI5wCvmJNrXjE3etgfI+lkl1fTDU6Y3ZH1mhrjkPlVI9W4pcKbMo+BQLpEWKVYYp2EmYaRsqpPC3k7lQ==
1252+
version "1.0.30001170"
1253+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001170.tgz"
1254+
integrity sha512-Dd4d/+0tsK0UNLrZs3CvNukqalnVTRrxb5mcQm8rHL49t7V5ZaTygwXkrq+FB+dVDf++4ri8eJnFEJAB8332PA==
11881255

11891256
capture-stack-trace@^1.0.0:
11901257
version "1.0.0"
@@ -1596,6 +1663,11 @@ csso@^4.0.2:
15961663
dependencies:
15971664
css-tree "1.0.0-alpha.39"
15981665

1666+
csstype@^2.6.8:
1667+
version "2.6.14"
1668+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.14.tgz#004822a4050345b55ad4dcc00be1d9cf2f4296de"
1669+
integrity sha512-2mSc+VEpGPblzAxyeR+vZhJKgYg0Og0nnRi7pmRXFYYxSfnOnW8A5wwQb4n4cE2nIOzqKOAzLCaEX6aBmNEv8A==
1670+
15991671
currently-unhandled@^0.4.1:
16001672
version "0.4.1"
16011673
resolved "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
@@ -2094,6 +2166,11 @@ estree-walker@^0.6.0, estree-walker@^0.6.1:
20942166
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
20952167
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
20962168

2169+
estree-walker@^2.0.1:
2170+
version "2.0.2"
2171+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
2172+
integrity sha512-Rfkk/Mp/DL7JVje3u18FxFujQlTNR2q6QfMSMB7AvCBx91NGj/ba3kCfza0f6dVDbw7YlRf/nDrn7pQrCCyQ/w==
2173+
20972174
esutils@^2.0.2:
20982175
version "2.0.2"
20992176
resolved "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz#0abf4f1caa5bcb1f7a9d8acc6dea4faaa04bac9b"
@@ -3087,6 +3164,11 @@ lodash@^4.17.13:
30873164
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
30883165
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
30893166

3167+
lodash@^4.17.19:
3168+
version "4.17.20"
3169+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
3170+
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
3171+
30903172
log-symbols@^1.0.2:
30913173
version "1.0.2"
30923174
resolved "https://registry.npmjs.org/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"
@@ -4946,6 +5028,15 @@ vlq@^0.2.2:
49465028
resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26"
49475029
integrity sha512-DRibZL6DsNhIgYQ+wNdWDL2SL3bKPlVrRiBqV5yuMm++op8W4kGFtaQfCs4KEJn0wBZcHVHJ3eoywX8983k1ow==
49485030

5031+
vue@^3.0.4:
5032+
version "3.0.4"
5033+
resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.4.tgz#872c65c143f5717bd5387c61613d9f55f4cc0f43"
5034+
integrity sha512-2o+AiQF8sAupyhbyl3oxVCl3WCwC/n5NI7VMM+gVQ231qvSB8eI7sCBloloqDJK6yA367EEtmRSeSCf4sxCC+A==
5035+
dependencies:
5036+
"@vue/compiler-dom" "3.0.4"
5037+
"@vue/runtime-dom" "3.0.4"
5038+
"@vue/shared" "3.0.4"
5039+
49495040
wcwidth@^1.0.1:
49505041
version "1.0.1"
49515042
resolved "https://registry.yarnpkg.com/wcwidth/-/wcwidth-1.0.1.tgz#f0b0dcf915bc5ff1528afadb2c0e17b532da2fe8"

0 commit comments

Comments
 (0)