Skip to content

Commit cd7583a

Browse files
committed
Avoid dynamic require by replacing vue with shim in browser
1 parent 2f764bc commit cd7583a

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

Diff for: package-lock.json

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
"@babel/core": "^7.12.10",
3939
"@babel/preset-env": "^7.12.11",
4040
"@contentful/rich-text-types": "^15.12.1",
41-
"@rollup/plugin-commonjs": "^22.0.0",
41+
"@rollup/plugin-alias": "^3.1.9",
4242
"@rollup/plugin-babel": "^5.3.1",
43+
"@rollup/plugin-commonjs": "^22.0.0",
4344
"@rollup/plugin-node-resolve": "^13.3.0",
4445
"@vue/test-utils": "^2.0.2",
4546
"babel-jest": "^26.6.3",
4647
"jest": "^26.6.3",
4748
"rollup": "^2.73.0",
4849
"vue": "^3.2.37"
4950
}
50-
}
51+
}

Diff for: rollup.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { nodeResolve } from "@rollup/plugin-node-resolve";
22
import { babel } from "@rollup/plugin-babel";
33
import common from "@rollup/plugin-commonjs";
4+
import alias from "@rollup/plugin-alias";
45
import pkg from "./package.json";
56

67
export default [
@@ -15,6 +16,9 @@ export default [
1516
},
1617
],
1718
plugins: [
19+
alias({
20+
entries: [{ find: "vue", replacement: "./vue-browser-shim" }],
21+
}),
1822
nodeResolve({
1923
browser: true,
2024
}),
@@ -27,7 +31,7 @@ export default [
2731
},
2832

2933
{
30-
external: ["@contentful/rich-text-types"],
34+
external: ["@contentful/rich-text-types", "vue"],
3135
input: "src/index.js",
3236
output: [
3337
{

Diff for: src/index.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { BLOCKS, MARKS, INLINES, helpers } from "@contentful/rich-text-types";
2-
3-
let vueInstance;
4-
if (typeof Vue === "undefined") {
5-
vueInstance = require("vue");
6-
} else {
7-
vueInstance = Vue;
8-
}
9-
const { h } = vueInstance;
2+
import { h } from "vue";
103

114
const defaultInline = (type, node, key) => {
125
return h(

Diff for: src/vue-browser-shim.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { h } = globalThis.Vue;
2+
3+
export default globalThis.Vue;
4+
export { h };

0 commit comments

Comments
 (0)