🌐 AI搜索 & 代理 主页
Skip to content

Commit 56f8bb5

Browse files
tmorehousejacobmllr95
authored andcommitted
fix(docs): handle undocumented breaking changes in babel-standalone for IE11 (#4484)
* chore(docs): add gated flag to polyfill.io request * Update nuxt.config.js * Update nuxt.config.js * Update needs-transpiler.js * Update play.vue * Update play.vue * Update play.vue * Update play.vue * Update play.vue * Update play.vue * Update compile-js.js * Update play.vue * Update play.vue * Update compile-js.js * Update play.vue * Update compile-js.js * Update nuxt.config.js * Update compile-js.js * Update compile-js.js * Update compile-js.js * Update nuxt.config.js * Update play.vue * Update compile-js.js * Update compile-js.js * Update needs-transpiler.js * Update play.vue * Update play.vue * Update compile-js.js
1 parent b246682 commit 56f8bb5

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

docs/pages/play.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,6 @@ export default {
685685
} else {
686686
delete options.template
687687
}
688-
689688
// Vue's `errorCapture` doesn't always handle errors in methods (although it
690689
// does if the method is used as a `v-on`/`@` handler), so we wrap any methods
691690
// with a try/catch handler so we can show the error in our GUI log console.
@@ -709,15 +708,15 @@ export default {
709708
})
710709
}
711710
712-
// Try and buld the user app
711+
// Try and build the user app
713712
try {
714713
const holder = document.createElement('div')
715714
this.$refs.result.appendChild(holder)
716715
this.playVM = new Vue({
717716
...options,
718717
el: holder,
719-
// Router needed for tooltips/popovers so they hide when
720-
// docs route changes
718+
// Router needed for tooltips/popovers/toasts so
719+
// that they hide when docs route changes
721720
router: this.$router,
722721
// We set a fake parent so we can capture most runtime and
723722
// render errors (this is an error boundary component)
@@ -751,15 +750,15 @@ export default {
751750
this.compiledJs = null
752751
return
753752
}
754-
const js = this.js.trim() || '{}'
753+
const js = (this.js || '').trim() || '{}'
755754
this.compiling = true
756755
let compiled = null
757756
this.$nextTick(() => {
758757
this.requestAF(() => {
759758
try {
760759
// The app build process expects the app options to
761760
// be assigned to the `options` variable
762-
compiled = this.compiler(`;options = ${js};`)
761+
compiled = this.compiler(';options = ' + js + ';')
763762
} catch (err) {
764763
this.errHandler(err, 'javascript')
765764
window.console.error('Error in javascript', err)

docs/utils/compile-js.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
1-
// Utility for tranpiling ES6 code into ES5 for playground and v-play
1+
// Utility for transpiling ES6 code into ES5 for playground and `v-play`
2+
// Imported only on demand when needed
23
import { transform, disableScriptTags } from '@babel/standalone'
34

5+
// Babel broke the standalone version via PR https://github.com/babel/babel/pull/10420
6+
// Which assumes the browser supports String.prototype.trimLeft/Right
7+
// IE 11 does not support either, and polyfill.io does not polyfill them
8+
// So we do it here (as this file is only loaded if we need transpilation):
9+
if (typeof window !== 'undefined') {
10+
const Proto = window.String.prototype
11+
12+
// Ensure we have a `trimStart` method
13+
;((obj, prop) => {
14+
if (!(prop in obj && obj[prop])) {
15+
const rx = /^\s+/
16+
obj[prop] =
17+
obj.trimLeft ||
18+
function() {
19+
return this.replace(rx, '')
20+
}
21+
}
22+
})(Proto, 'trimStart')
23+
24+
// Ensure we have a `trimLeft` method
25+
;((obj, prop) => {
26+
if (!(prop in obj && obj[prop])) {
27+
obj[prop] = obj.trimStart
28+
}
29+
})(Proto, 'trimLeft')
30+
31+
// Ensure we have a `trimEnd` method
32+
;((obj, prop) => {
33+
if (!(prop in obj && obj[prop])) {
34+
const rx = /\s+$/
35+
obj[prop] =
36+
obj.trimRight ||
37+
function() {
38+
return this.replace(rx, '')
39+
}
40+
}
41+
})(Proto, 'trimEnd')
42+
43+
// Ensure we have a `trimRight` method
44+
;((obj, prop) => {
45+
if (!(prop in obj && obj[prop])) {
46+
obj[prop] = obj.trimEnd
47+
}
48+
})(Proto, 'trimRight')
49+
}
50+
51+
// Prevent Babel/Standalone from processing <script> tag insertions
452
if (typeof window !== 'undefined' && window && window.removeEventListener) {
5-
// Prevent Babel/Standalone from processing <script> tag insertions
653
disableScriptTags()
754
}
855

56+
// Our babel transform options
957
const transformOptions = {
58+
sourceType: 'script',
1059
presets: ['es2015', 'es2016', 'es2017'],
1160
plugins: [
1261
// Not used as we need to import the helpers into the transpiled code
@@ -15,6 +64,7 @@ const transformOptions = {
1564
]
1665
}
1766

67+
// Our transpilation compiler method
1868
export default function compileJs(code) {
1969
if (!code) {
2070
return ''

docs/utils/needs-transpiler.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ const tests = [
88
// Arrow functions
99
'const test1 = (a) => a',
1010
// Object function shortcut
11-
'const test2 = { a: 1, b () { return 0 } }',
11+
'const test2 = { a: 1, b() { return 0 } }',
1212
// Object shortcut
13-
'const test3a = { a: 1}; const test3b = { test3a, b: 2 }',
14-
// Object rest spread
15-
'const test4a = { a: 1, b: 2}; const test4b = { c: 3, ...test4a }',
13+
'const test3a = { a: 1 }; const test3b = { test3a, b: 2 }',
14+
// Object spread
15+
'const test4a = { a: 1, b: 2 }; const test4b = { c: 3, ...test4a }',
16+
// Array spread
17+
'const test5a = [1, 2]; const test5b = [...test5a, 3, 4]',
1618
// String interpolation
1719
/* eslint-disable no-template-curly-in-string */
18-
'const test5a = "bar"; const test5b = `foo${test5a}`'
20+
'const test6a = "bar"; const test6b = `foo${test6a}`'
1921
/* eslint-enable no-template-curly-in-string */
2022
]
2123

0 commit comments

Comments
 (0)