Skip to content

Commit 5047bc8

Browse files
committed
workflow: basic template explorer
1 parent 4d2fa51 commit 5047bc8

File tree

12 files changed

+188
-12
lines changed

12 files changed

+188
-12
lines changed

packages/compiler-core/src/utils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { parse } from 'acorn'
2020
import { walk } from 'estree-walker'
2121
import { TransformContext } from './transform'
2222
import { OPEN_BLOCK, CREATE_BLOCK, MERGE_PROPS } from './runtimeConstants'
23-
import { isString } from '@vue/shared'
23+
import { isString, isFunction } from '@vue/shared'
2424
import { PropsExpression } from './transforms/transformElement'
2525

2626
// cache node requires
@@ -29,12 +29,22 @@ import { PropsExpression } from './transforms/transformElement'
2929
let _parse: typeof parse
3030
let _walk: typeof walk
3131

32+
function loadDep(name: string) {
33+
if (typeof process !== 'undefined' && isFunction(require)) {
34+
return require(name)
35+
} else {
36+
// This is only used when we are building a dev-only build of the compiler
37+
// which runs in the browser but also uses Node deps.
38+
return (window as any)._deps[name]
39+
}
40+
}
41+
3242
export const parseJS: typeof parse = (code: string, options: any) => {
3343
assert(
3444
!__BROWSER__,
3545
`Expression AST analysis can only be performed in non-browser builds.`
3646
)
37-
const parse = _parse || (_parse = require('acorn').parse)
47+
const parse = _parse || (_parse = loadDep('acorn').parse)
3848
return parse(code, options)
3949
}
4050

@@ -43,7 +53,7 @@ export const walkJS: typeof walk = (ast, walker) => {
4353
!__BROWSER__,
4454
`Expression AST analysis can only be performed in non-browser builds.`
4555
)
46-
const walk = _walk || (_walk = require('estree-walker').walk)
56+
const walk = _walk || (_walk = loadDep('estree-walker').walk)
4757
return walk(ast, walker)
4858
}
4959

packages/shared/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"name": "@vue/shared",
3+
"version": "3.0.0-alpha.1",
34
"private": true
45
}

packages/template-explorer/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Template Explorer
2+
3+
Live explorer for template compilation output.
4+
5+
To run:
6+
7+
- `yarn dev template-explorer`
8+
- Serve project root over a local HTTP server.

packages/template-explorer/index.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<link rel="stylesheet" data-name="vs/editor/editor.main" href="../../node_modules/monaco-editor/min/vs/editor/editor.main.css">
2+
<style>
3+
body {
4+
margin: 0;
5+
}
6+
.editor {
7+
position: absolute;
8+
top: 0;
9+
bottom: 0;
10+
box-sizing: border-box;
11+
}
12+
#source {
13+
left: 0;
14+
width: 40%;
15+
}
16+
#output {
17+
left: 40%;
18+
width: 60%;
19+
}
20+
</style>
21+
22+
<div id="source" class="editor"></div>
23+
<div id="output" class="editor"></div>
24+
25+
<script src="../../node_modules/acorn/dist/acorn.js"></script>
26+
<script src="../../node_modules/estree-walker/dist/estree-walker.umd.js"></script>
27+
<script src="../../node_modules/monaco-editor/min/vs/loader.js"></script>
28+
<script src="./dist/template-explorer.global.js"></script>
29+
<script>
30+
window._deps = {
31+
acorn,
32+
'estree-walker': estreeWalker
33+
}
34+
35+
require.config({
36+
paths: {
37+
'vs': '../../node_modules/monaco-editor/min/vs'
38+
}
39+
})
40+
</script>
41+
<script>
42+
require(['vs/editor/editor.main'], init /* injected by build */)
43+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@vue/template-explorer",
3+
"version": "3.0.0-alpha.1",
4+
"private": true,
5+
"buildOptions": {
6+
"formats": [
7+
"global"
8+
],
9+
"env": "development",
10+
"enableNonBrowserBranches": true
11+
},
12+
"dependencies": {
13+
"monaco-editor": "^0.18.1"
14+
}
15+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import * as m from 'monaco-editor'
2+
import { compile } from '@vue/compiler-dom'
3+
4+
const self = window as any
5+
6+
self.init = () => {
7+
const monaco = (window as any).monaco as typeof m
8+
const persistedContent =
9+
decodeURIComponent(window.location.hash.slice(1)) ||
10+
`<div>{{ foo + bar }}</div>`
11+
12+
self.compilerOptions = {
13+
mode: 'module',
14+
prefixIdentifiers: true,
15+
hoistStatic: true
16+
}
17+
18+
function compileCode(source: string): string {
19+
console.clear()
20+
try {
21+
const { code, ast } = compile(source, self.compilerOptions)
22+
23+
console.log(ast)
24+
return code
25+
} catch (e) {
26+
console.error(e)
27+
return `/* See console for error */`
28+
}
29+
}
30+
31+
const sharedOptions = {
32+
theme: 'vs-dark',
33+
fontSize: 14,
34+
wordWrap: 'on',
35+
scrollBeyondLastLine: false,
36+
minimap: {
37+
enabled: false
38+
}
39+
} as const
40+
41+
const editor = monaco.editor.create(
42+
document.getElementById('source') as HTMLElement,
43+
{
44+
value: persistedContent,
45+
language: 'html',
46+
...sharedOptions
47+
}
48+
)
49+
50+
const model = editor.getModel()!
51+
52+
model.updateOptions({
53+
tabSize: 2
54+
})
55+
56+
model.onDidChangeContent(() => {
57+
const src = editor.getValue()
58+
window.location.hash = encodeURIComponent(src)
59+
const res = compileCode(src)
60+
if (res) {
61+
output.setValue(res)
62+
}
63+
})
64+
65+
const output = monaco.editor.create(
66+
document.getElementById('output') as HTMLElement,
67+
{
68+
value: compileCode(persistedContent),
69+
language: 'javascript',
70+
readOnly: true,
71+
...sharedOptions
72+
}
73+
)
74+
75+
output.getModel()!.updateOptions({
76+
tabSize: 2
77+
})
78+
79+
window.addEventListener('resize', () => {
80+
editor.layout()
81+
output.layout()
82+
})
83+
}

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ function createConfig(output, plugins = []) {
119119
createReplacePlugin(
120120
isProductionBuild,
121121
isBunlderESMBuild,
122-
isGlobalBuild || isBrowserESMBuild
122+
(isGlobalBuild || isBrowserESMBuild) &&
123+
!packageOptions.enableNonBrowserBranches
123124
),
124125
...plugins
125126
],

scripts/bootstrap.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ const packagesDir = path.resolve(__dirname, '../packages')
99
const files = fs.readdirSync(packagesDir)
1010

1111
files.forEach(shortName => {
12-
if (shortName === 'shared') {
13-
return
14-
}
1512
if (!fs.statSync(path.join(packagesDir, shortName)).isDirectory()) {
1613
return
1714
}
1815

1916
const name = shortName === `vue` ? shortName : `@vue/${shortName}`
2017
const pkgPath = path.join(packagesDir, shortName, `package.json`)
21-
if (args.force || !fs.existsSync(pkgPath)) {
18+
const pkgExists = fs.existsSync(pkgPath)
19+
if (pkgExists) {
20+
const pkg = require(pkgPath)
21+
if (pkg.private) {
22+
return
23+
}
24+
}
25+
26+
if (args.force || !pkgExists) {
2227
const json = {
2328
name,
2429
version: baseVersion,

scripts/build.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ async function build(target) {
5151

5252
await fs.remove(`${pkgDir}/dist`)
5353

54+
const env =
55+
(pkg.buildOptions && pkg.buildOptions.env) ||
56+
(devOnly ? 'development' : 'production')
57+
5458
await execa(
5559
'rollup',
5660
[
5761
'-c',
5862
'--environment',
59-
`NODE_ENV:${devOnly ? 'development' : 'production'},` +
63+
`NODE_ENV:${env},` +
6064
`TARGET:${target}` +
6165
(formats ? `,FORMATS:${formats}` : ``) +
6266
(args.types ? `,TYPES:true` : ``) +

scripts/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const targets = (exports.targets = fs.readdirSync('packages').filter(f => {
55
return false
66
}
77
const pkg = require(`../packages/${f}/package.json`)
8-
if (pkg.private) {
8+
if (pkg.private && !pkg.buildOptions) {
99
return false
1010
}
1111
return true
@@ -26,6 +26,6 @@ exports.fuzzyMatchTarget = (partialTargets, includeAllMatching) => {
2626
if (matched.length) {
2727
return matched
2828
} else {
29-
throw new Error(`Target ${partialTarget} not found!`)
29+
throw new Error(`Target ${partialTargets} not found!`)
3030
}
3131
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"@vue/reactivity": ["packages/reactivity/src"],
2828
"@vue/compiler-core": ["packages/compiler-core/src"],
2929
"@vue/compiler-dom": ["packages/compiler-dom/src"],
30-
"@vue/server-renderer": ["packages/server-renderer/src"]
30+
"@vue/server-renderer": ["packages/server-renderer/src"],
31+
"@vue/template-explorer": ["packages/template-explorer/src"]
3132
}
3233
},
3334
"include": [

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4932,6 +4932,11 @@ modify-values@^1.0.0:
49324932
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
49334933
integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==
49344934

4935+
monaco-editor@^0.18.1:
4936+
version "0.18.1"
4937+
resolved "https://registry.yarnpkg.com/monaco-editor/-/monaco-editor-0.18.1.tgz#ced7c305a23109875feeaf395a504b91f6358cfc"
4938+
integrity sha512-fmL+RFZ2Hrezy+X/5ZczQW51LUmvzfcqOurnkCIRFTyjdVjzR7JvENzI6+VKBJzJdPh6EYL4RoWl92b2Hrk9fw==
4939+
49354940
move-concurrently@^1.0.1:
49364941
version "1.0.1"
49374942
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"

0 commit comments

Comments
 (0)