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

Commit 4288e84

Browse files
committed
feat: transform mustache sytax
1 parent d00f753 commit 4288e84

16 files changed

+688
-37
lines changed

jest.config.js

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "/private/var/folders/g4/53nrh02956j164gn6d1lhjt00000gn/T/jest_dx",
16+
17+
// Automatically clear mock calls and instances between every test
18+
// clearMocks: false,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
collectCoverage: true,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: null,
25+
26+
// The directory where Jest should output its coverage files
27+
// coverageDirectory: null,
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
coveragePathIgnorePatterns: ['/node_modules/'],
31+
32+
// A list of reporter names that Jest uses when writing coverage reports
33+
coverageReporters: [
34+
// "json",
35+
'text'
36+
// "lcov",
37+
// "clover"
38+
],
39+
40+
// An object that configures minimum threshold enforcement for coverage results
41+
// coverageThreshold: null,
42+
43+
// A path to a custom dependency extractor
44+
// dependencyExtractor: null,
45+
46+
// Make calling deprecated APIs throw helpful error messages
47+
// errorOnDeprecated: false,
48+
49+
// Force coverage collection from ignored files using an array of glob patterns
50+
// forceCoverageMatch: [],
51+
52+
// A path to a module which exports an async function that is triggered once before all test suites
53+
// globalSetup: null,
54+
55+
// A path to a module which exports an async function that is triggered once after all test suites
56+
// globalTeardown: null,
57+
58+
// A set of global variables that need to be available in all test environments
59+
// globals: {},
60+
61+
// An array of directory names to be searched recursively up from the requiring module's location
62+
moduleDirectories: ['node_modules'],
63+
64+
// An array of file extensions your modules use
65+
// moduleFileExtensions: [
66+
// "js",
67+
// "json",
68+
// "jsx",
69+
// "ts",
70+
// "tsx",
71+
// "node"
72+
// ],
73+
74+
// A map from regular expressions to module names that allow to stub out resources with a single module
75+
// moduleNameMapper: {},
76+
77+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
78+
// modulePathIgnorePatterns: [],
79+
80+
// Activates notifications for test results
81+
// notify: false,
82+
83+
// An enum that specifies notification mode. Requires { notify: true }
84+
// notifyMode: "failure-change",
85+
86+
// A preset that is used as a base for Jest's configuration
87+
// preset: null,
88+
89+
// Run tests from one or more projects
90+
// projects: null,
91+
92+
// Use this configuration option to add custom reporters to Jest
93+
// reporters: undefined,
94+
95+
// Automatically reset mock state between every test
96+
// resetMocks: false,
97+
98+
// Reset the module registry before running each individual test
99+
// resetModules: false,
100+
101+
// A path to a custom resolver
102+
// resolver: null,
103+
104+
// Automatically restore mock state between every test
105+
// restoreMocks: false,
106+
107+
// The root directory that Jest should scan for tests and modules within
108+
// rootDir: null,
109+
110+
// A list of paths to directories that Jest should use to search for files in
111+
// roots: [
112+
// "<rootDir>"
113+
// ],
114+
115+
// Allows you to use a custom runner instead of Jest's default test runner
116+
// runner: "jest-runner",
117+
118+
// The paths to modules that run some code to configure or set up the testing environment before each test
119+
// setupFiles: [],
120+
121+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
122+
// setupFilesAfterEnv: [],
123+
124+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
125+
// snapshotSerializers: [],
126+
127+
// The test environment that will be used for testing
128+
testEnvironment: 'node'
129+
130+
// Options that will be passed to the testEnvironment
131+
// testEnvironmentOptions: {},
132+
133+
// Adds a location field to test results
134+
// testLocationInResults: false,
135+
136+
// The glob patterns Jest uses to detect test files
137+
// testMatch: [
138+
// "**/__tests__/**/*.[jt]s?(x)",
139+
// "**/?(*.)+(spec|test).[tj]s?(x)"
140+
// ],
141+
142+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
143+
// testPathIgnorePatterns: [
144+
// "/node_modules/"
145+
// ],
146+
147+
// The regexp pattern or array of patterns that Jest uses to detect test files
148+
// testRegex: [],
149+
150+
// This option allows the use of a custom results processor
151+
// testResultsProcessor: null,
152+
153+
// This option allows use of a custom test runner
154+
// testRunner: "jasmine2",
155+
156+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
157+
// testURL: "http://localhost",
158+
159+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
160+
// timers: "real",
161+
162+
// A map from regular expressions to paths to transformers
163+
// transform: null,
164+
165+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
166+
// transformIgnorePatterns: [
167+
// "/node_modules/"
168+
// ],
169+
170+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
171+
// unmockedModulePathPatterns: undefined,
172+
173+
// Indicates whether each individual test should be reported during the run
174+
// verbose: null,
175+
176+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
177+
// watchPathIgnorePatterns: [],
178+
179+
// Whether to use watchman for file crawling
180+
// watchman: true,
181+
}

package-lock.json

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

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "jest",
88
"commit": "npx git-cz",
99
"release": "npx standard-version",
1010
"lint": "eslint ./src",
@@ -22,6 +22,9 @@
2222
},
2323
"homepage": "https://github.com/AngusFu/wxml2vue#readme",
2424
"dependencies": {
25+
"lodash": "^4.17.11",
26+
"mustache": "^3.0.1",
27+
"parse-entities": "^1.2.2",
2528
"rehype-parse": "^6.0.0",
2629
"rehype-stringify": "^6.0.0",
2730
"to-vfile": "^6.0.0",

src/bak/vue-ast.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// const compiler = require('vue-template-compiler')
2+
3+
// var sfc = compiler.parseComponent(`<template>${template}</template>`)
4+
5+
// const astRes = compiler.compile(sfc.template.content, {
6+
// comments: true,
7+
// preserveWhitespace: false,
8+
// shouldDecodeNewlines: true
9+
// })
10+
// console.log(astRes)

src/index.js

+23-32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const template = `
1+
let template = `
22
<template name="staffName">
33
<view>
44
FirstName: {{firstName}}, LastName: {{lastName}}
55
</view>
66
</template>
77
8-
< data="{{...staffA}}"></template>
8+
<template data="{{...staffA}}"></template>
99
<template is="staffName" data="{{...staffB}}"></template>
1010
<template is="staffName" data="{{...staffC}}"></template>
1111
@@ -21,39 +21,30 @@ const template = `
2121
<text class="user-motto">{{motto}}</text>
2222
</view>
2323
</view>
24-
`
25-
26-
// const compiler = require('vue-template-compiler')
2724
28-
// var sfc = compiler.parseComponent(`<template>${template}</template>`)
25+
<view>
26+
<view> {{ message }} </view>
27+
<view> {{a + b}} + {{c}} + d </view>
28+
<view>{{"hello" + name}}</view>
29+
<view>{{object.key}} {{array[0]}}</view>
30+
</view>
31+
`
2932

30-
// const astRes = compiler.compile(sfc.template.content, {
31-
// comments: true,
32-
// preserveWhitespace: false,
33-
// shouldDecodeNewlines: true
34-
// })
35-
// console.log(astRes)
33+
// 0. 注意针对 template content 的处理
34+
// 1. text 中的插值: keep as it is
35+
// 2. property 插值:
36+
// 2.1 wx:for wx:if => directives
37+
// 2.2 normal properties
38+
// 3. <template is="{{name}}"></template>
39+
// 4. <import /> and <include/>
40+
// 5. Tagname 修改
3641

37-
const vfile = require('to-vfile')
38-
const unified = require('unified')
39-
const parse = require('rehype-parse')
40-
const stringify = require('rehype-stringify')
41-
const report = require('vfile-reporter')
42+
const proccess = require('./proccess')
4243

43-
unified()
44-
.use(parse, {
45-
fragment: true,
46-
emitParseErrors: true,
47-
duplicateAttribute: false
48-
})
49-
.use(require('./transform'))
50-
.use(stringify, {
51-
quoteSmart: true,
52-
closeSelfClosing: true,
53-
omitOptionalTags: true,
54-
entities: { useShortestReferences: true }
44+
proccess(template)
45+
.then(file => {
46+
console.log(file)
5547
})
56-
.process(vfile({ contents: template.trim() }), function(err, file) {
57-
console.error(report(err || file))
58-
// console.log(String(file))
48+
.catch(e => {
49+
console.error(proccess.report(e))
5950
})

src/proccess.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const vfile = require('to-vfile')
2+
const unified = require('unified')
3+
const parse = require('rehype-parse')
4+
const stringify = require('rehype-stringify')
5+
const report = require('vfile-reporter')
6+
const parseEntities = require('parse-entities')
7+
8+
module.exports = function(template, plugins) {
9+
const preccessor = [
10+
[
11+
parse,
12+
{
13+
fragment: true,
14+
emitParseErrors: true,
15+
duplicateAttribute: false
16+
}
17+
],
18+
...(plugins || []),
19+
[
20+
stringify,
21+
{
22+
quoteSmart: true,
23+
closeSelfClosing: true,
24+
omitOptionalTags: true,
25+
entities: { useShortestReferences: true }
26+
}
27+
]
28+
].reduce(
29+
(preccessor, current) => preccessor.use(...[].concat(current)),
30+
unified()
31+
)
32+
33+
return preccessor.process(vfile({ contents: template })).then(vfile => {
34+
vfile.contents = parseEntities(vfile.contents)
35+
return vfile
36+
})
37+
}
38+
39+
module.exports.report = report

0 commit comments

Comments
 (0)