@open-wxa/wxa-runner-compiler 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -113,14 +113,14 @@ function findPackagedCompiler(ext) {
|
|
|
113
113
|
|
|
114
114
|
// src/wcc.ts
|
|
115
115
|
var MAX_INLINE_ARGS_LENGTH = 3e4;
|
|
116
|
+
var SPLIT = ">_<1024";
|
|
116
117
|
async function compileWxml(options) {
|
|
117
118
|
const { wcc } = findCompilerBinaries();
|
|
118
|
-
const split = `>_<${Date.now() % 1e4}`;
|
|
119
119
|
const config = [options.components.length];
|
|
120
120
|
for (const component of options.components) {
|
|
121
121
|
config.push(`./${component.path}.wxml`, component.tags.length, ...component.tags);
|
|
122
122
|
}
|
|
123
|
-
const args = ["-d", "--split",
|
|
123
|
+
const args = ["-d", "--split", SPLIT, options.cut ? "-xc" : "-cc", config.join(SPLIT)];
|
|
124
124
|
if (options.files.some((file) => file.endsWith(".wxs"))) {
|
|
125
125
|
args.unshift("-ds");
|
|
126
126
|
}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/wcc.ts","../src/exec.ts","../src/locate.ts","../src/wcsc.ts"],"sourcesContent":["/**\n * wxa-runner-compiler:官方 wcc / wcsc 编译器封装\n */\n\nexport { compileWxml } from './wcc';\nexport type { ComponentTags, WxmlCompileOptions } from './wcc';\nexport { compileWxss, parseWcscOutput, unescapeJsString } from './wcsc';\nexport type { WxssCompileOptions, WxssCompileResult } from './wcsc';\nexport { findCompilerBinaries } from './locate';\nexport type { CompilerBinaries } from './locate';\n","/**\n * WXML 编译(wcc)\n *\n * 所有 wxml/wxs 一次性编译成一个 $gwx 函数,模板 import/include 和 wxs 引用由 wcc 自己解析。\n */\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport { runCompiler } from './exec';\nimport { findCompilerBinaries } from './locate';\n\nexport interface ComponentTags {\n /** 页面或组件路径,不带扩展名,如 \"pages/index/index\" */\n path: string;\n /** 该 wxml 中属于自定义组件(含抽象节点)的标签名 */\n tags: string[];\n}\n\nexport interface WxmlCompileOptions {\n /** 小程序根目录 */\n cwd: string;\n /** 相对 cwd 的 ./ 开头路径,包含 .wxml 和 .wxs */\n files: string[];\n components: ComponentTags[];\n /**\n * true:逻辑层使用的裁剪版(-xc),只保留组件结构信息;\n * false:渲染层使用的完整版(-cc)\n */\n cut: boolean;\n genfuncName?: string;\n}\n\n// Windows 命令行长度上限约 32K,超出时改用配置文件传参(开发者工具同样如此)\nconst MAX_INLINE_ARGS_LENGTH = 30000;\n\nexport async function compileWxml(options: WxmlCompileOptions): Promise<string> {\n const { wcc } = findCompilerBinaries();\n const split = `>_<${Date.now() % 10000}`;\n\n const config: Array<string | number> = [options.components.length];\n for (const component of options.components) {\n config.push(`./${component.path}.wxml`, component.tags.length, ...component.tags);\n }\n\n const args = ['-d', '--split', split, options.cut ? '-xc' : '-cc', config.join(split)];\n if (options.files.some(file => file.endsWith('.wxs'))) {\n args.unshift('-ds');\n }\n args.push(...options.files, '-gn', options.genfuncName || '$gwx');\n\n if (args.join(' ').length <= MAX_INLINE_ARGS_LENGTH) {\n return runCompiler(wcc, args, options.cwd);\n }\n\n const configFile = path.join(os.tmpdir(), `wxa-runner-wcc-${process.pid}-${Date.now()}.txt`);\n fs.writeFileSync(configFile, args.join('\\n'));\n try {\n return await runCompiler(wcc, ['--config-path', configFile], options.cwd);\n } finally {\n fs.rmSync(configFile, { force: true });\n }\n}\n","import { execFile } from 'child_process';\n\nconst MAX_OUTPUT = 512 * 1024 * 1024;\n\nexport function runCompiler(bin: string, args: string[], cwd: string): Promise<string> {\n return new Promise((resolve, reject) => {\n execFile(bin, args, { cwd, maxBuffer: MAX_OUTPUT, encoding: 'utf8' }, (error, stdout, stderr) => {\n if (error) {\n const detail = (stderr || stdout || error.message).trim();\n reject(new Error(`${bin.split(/[\\\\/]/).pop()} 编译失败:\\n${detail}`));\n return;\n }\n resolve(stdout);\n });\n });\n}\n","/**\n * 定位 wcc / wcsc 可执行文件\n *\n * 使用开发者工具自带的命令行版本(node_modules/wcc-exec),而不是 wcc.node 原生模块:\n * 原生模块在 Node 进程里重复调用会随机触发 std::bad_alloc 崩溃,\n * 子进程方式则天然隔离,单次编译只需几毫秒。\n *\n * 没有开发者工具时(如 Linux、CI),用微信官方 npm 包 miniprogram-compiler 自带的编译器。\n * 它的编译内核较旧(20200413,开发者工具为 20211229):页面的编译结果相同,\n * 只有模板运行时有少量差异,如旧版限制单页最多 16000 个节点。\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport { findDevtoolsPackageDirs } from '@open-wxa/wxa-runner-extractor';\n\nexport interface CompilerBinaries {\n wcc: string;\n wcsc: string;\n}\n\nlet cached: CompilerBinaries | null = null;\n\nexport function findCompilerBinaries(): CompilerBinaries {\n if (!cached) cached = locateCompilerBinaries(findDevtoolsPackageDirs());\n return cached;\n}\n\n/**\n * 依次查找:环境变量 WXA_WCC + WXA_WCSC、开发者工具、miniprogram-compiler\n *\n * @throws 都找不到时\n */\nexport function locateCompilerBinaries(devtoolsDirs: string[]): CompilerBinaries {\n const ext = process.platform === 'win32' ? '.exe' : '';\n const candidates: CompilerBinaries[] = [];\n\n if (process.env.WXA_WCC && process.env.WXA_WCSC) {\n candidates.push({ wcc: process.env.WXA_WCC, wcsc: process.env.WXA_WCSC });\n }\n for (const dir of devtoolsDirs) {\n const execDir = path.join(dir, 'node_modules', 'wcc-exec');\n candidates.push({ wcc: path.join(execDir, 'wcc' + ext), wcsc: path.join(execDir, 'wcsc' + ext) });\n }\n const packaged = findPackagedCompiler(ext);\n if (packaged) candidates.push(packaged);\n\n const found = candidates.find(c => fs.existsSync(c.wcc) && fs.existsSync(c.wcsc));\n if (!found) {\n throw new Error(\n '没有找到 wcc/wcsc 编译器。请安装微信开发者工具,或重新安装依赖以安装 miniprogram-compiler(Linux 只支持 x86-64),' +\n '或通过 WXA_DEVTOOLS_PATH / WXA_WCC + WXA_WCSC 指定位置。'\n );\n }\n // npm 包里的 Linux 版没有可执行权限\n if (found === packaged) {\n for (const file of [found.wcc, found.wcsc]) {\n try {\n fs.accessSync(file, fs.constants.X_OK);\n } catch {\n fs.chmodSync(file, 0o755);\n }\n }\n }\n return found;\n}\n\n/** miniprogram-compiler 里当前平台的编译器;它是可选依赖,没装上或平台不支持时返回 null */\nfunction findPackagedCompiler(ext: string): CompilerBinaries | null {\n const platformDir =\n process.platform === 'darwin' ? 'mac'\n : process.platform === 'win32' ? 'windows'\n : process.platform === 'linux' && process.arch === 'x64' ? 'linux'\n : null;\n if (!platformDir) return null;\n\n let root: string;\n try {\n root = path.dirname(require.resolve('miniprogram-compiler/package.json'));\n } catch {\n return null;\n }\n const dir = path.join(root, 'bin', platformDir);\n return { wcc: path.join(dir, 'wcc' + ext), wcsc: path.join(dir, 'wcsc' + ext) };\n}\n","/**\n * WXSS 编译(wcsc)\n *\n * 输出是 \"key=value=key=value\" 交替的字符串,value 是转义过的 JS 源码:\n * comm 公共运行时(setCssToHead、rpx 换算)以及非页面样式(app.wxss 等)的立即执行代码\n * ./pages/a.wxss 每个页面/组件一个 setCssToHead(...),返回的函数在页面创建时调用\n */\n\nimport { runCompiler } from './exec';\nimport { findCompilerBinaries } from './locate';\n\nexport interface WxssCompileOptions {\n cwd: string;\n /** 页面和组件的 wxss,./ 开头 */\n pageFiles: string[];\n /** 其余样式(app.wxss),以公共样式形式写入 comm */\n commonFiles: string[];\n}\n\nexport interface WxssCompileResult {\n comm: string;\n /** 键为不带 ./ 的路径,如 \"pages/index/index.wxss\" */\n pages: Record<string, string>;\n}\n\nexport async function compileWxss(options: WxssCompileOptions): Promise<WxssCompileResult> {\n const { wcsc } = findCompilerBinaries();\n const files = [...options.pageFiles, ...options.commonFiles];\n if (files.length === 0) {\n return { comm: '', pages: {} };\n }\n\n const args = ['-db', '-pc', String(options.pageFiles.length), ...files];\n const raw = parseWcscOutput(await runCompiler(wcsc, args, options.cwd));\n\n const pages: Record<string, string> = {};\n for (const file of options.pageFiles) {\n if (raw[file] !== undefined) pages[file.replace(/^\\.\\//, '')] = raw[file];\n }\n return { comm: raw.comm || '', pages };\n}\n\nexport function parseWcscOutput(output: string): Record<string, string> {\n const parts = output.split('=');\n const result: Record<string, string> = {};\n for (let i = 0; i + 1 < parts.length; i += 2) {\n result[parts[i]] = unescapeJsString(parts[i + 1]);\n }\n return result;\n}\n\nconst SIMPLE_ESCAPES: Record<string, string> = {\n n: '\\n', t: '\\t', r: '\\r', b: '\\b', f: '\\f', v: '\\v', '0': '\\0'\n};\n\nexport function unescapeJsString(value: string): string {\n return value.replace(/\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|[\\s\\S])/g, (_, seq: string) => {\n if ((seq[0] === 'x' || seq[0] === 'u') && seq.length > 1) {\n return String.fromCharCode(parseInt(seq.slice(1), 16));\n }\n return SIMPLE_ESCAPES[seq] ?? seq;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,aAAe;AACf,gBAAe;AACf,IAAAC,eAAiB;;;ACRjB,2BAAyB;AAEzB,IAAM,aAAa,MAAM,OAAO;AAEzB,SAAS,YAAY,KAAa,MAAgB,KAA8B;AACrF,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,uCAAS,KAAK,MAAM,EAAE,KAAK,WAAW,YAAY,UAAU,OAAO,GAAG,CAAC,OAAO,QAAQ,WAAW;AAC/F,UAAI,OAAO;AACT,cAAM,UAAU,UAAU,UAAU,MAAM,SAAS,KAAK;AACxD,eAAO,IAAI,MAAM,GAAG,IAAI,MAAM,OAAO,EAAE,IAAI,CAAC;AAAA,EAAW,MAAM,EAAE,CAAC;AAChE;AAAA,M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,SAAS,iBAAiB,OAAuB;AACtD,SAAO,MAAM,QAAQ,+CAA+C,CAAC,GAAG,QAAgB;AACtF,SAAK,IAAI,CAAC,MAAM,OAAO,IAAI,CAAC,MAAM,QAAQ,IAAI,SAAS,GAAG;AACxD,aAAO,OAAO,aAAa,SAAS,IAAI,MAAM,CAAC,GAAG,EAAE,CAAC;AAAA,IACvD;AACA,WAAO,eAAe,GAAG,KAAK;AAAA,EAChC,CAAC;AACH;","names":["import_fs","import_path","path","fs","path","os","fs"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/wcc.ts","../src/exec.ts","../src/locate.ts","../src/wcsc.ts"],"sourcesContent":["/**\n * wxa-runner-compiler:官方 wcc / wcsc 编译器封装\n */\n\nexport { compileWxml } from './wcc';\nexport type { ComponentTags, WxmlCompileOptions } from './wcc';\nexport { compileWxss, parseWcscOutput, unescapeJsString } from './wcsc';\nexport type { WxssCompileOptions, WxssCompileResult } from './wcsc';\nexport { findCompilerBinaries } from './locate';\nexport type { CompilerBinaries } from './locate';\n","/**\n * WXML 编译(wcc)\n *\n * 所有 wxml/wxs 一次性编译成一个 $gwx 函数,模板 import/include 和 wxs 引用由 wcc 自己解析。\n */\n\nimport fs from 'fs';\nimport os from 'os';\nimport path from 'path';\nimport { runCompiler } from './exec';\nimport { findCompilerBinaries } from './locate';\n\nexport interface ComponentTags {\n /** 页面或组件路径,不带扩展名,如 \"pages/index/index\" */\n path: string;\n /** 该 wxml 中属于自定义组件(含抽象节点)的标签名 */\n tags: string[];\n}\n\nexport interface WxmlCompileOptions {\n /** 小程序根目录 */\n cwd: string;\n /** 相对 cwd 的 ./ 开头路径,包含 .wxml 和 .wxs */\n files: string[];\n components: ComponentTags[];\n /**\n * true:逻辑层使用的裁剪版(-xc),只保留组件结构信息;\n * false:渲染层使用的完整版(-cc)\n */\n cut: boolean;\n genfuncName?: string;\n}\n\n// Windows 命令行长度上限约 32K,超出时改用配置文件传参(开发者工具同样如此)\nconst MAX_INLINE_ARGS_LENGTH = 30000;\n\n// 组件配置的分隔符。miniprogram-compiler 的 wcc 遇到 >_<7 这样只带一位数字的分隔符会崩溃,所以固定用 4 位\nconst SPLIT = '>_<1024';\n\nexport async function compileWxml(options: WxmlCompileOptions): Promise<string> {\n const { wcc } = findCompilerBinaries();\n\n const config: Array<string | number> = [options.components.length];\n for (const component of options.components) {\n config.push(`./${component.path}.wxml`, component.tags.length, ...component.tags);\n }\n\n const args = ['-d', '--split', SPLIT, options.cut ? '-xc' : '-cc', config.join(SPLIT)];\n if (options.files.some(file => file.endsWith('.wxs'))) {\n args.unshift('-ds');\n }\n args.push(...options.files, '-gn', options.genfuncName || '$gwx');\n\n if (args.join(' ').length <= MAX_INLINE_ARGS_LENGTH) {\n return runCompiler(wcc, args, options.cwd);\n }\n\n const configFile = path.join(os.tmpdir(), `wxa-runner-wcc-${process.pid}-${Date.now()}.txt`);\n fs.writeFileSync(configFile, args.join('\\n'));\n try {\n return await runCompiler(wcc, ['--config-path', configFile], options.cwd);\n } finally {\n fs.rmSync(configFile, { force: true });\n }\n}\n","import { execFile } from 'child_process';\n\nconst MAX_OUTPUT = 512 * 1024 * 1024;\n\nexport function runCompiler(bin: string, args: string[], cwd: string): Promise<string> {\n return new Promise((resolve, reject) => {\n execFile(bin, args, { cwd, maxBuffer: MAX_OUTPUT, encoding: 'utf8' }, (error, stdout, stderr) => {\n if (error) {\n const detail = (stderr || stdout || error.message).trim();\n reject(new Error(`${bin.split(/[\\\\/]/).pop()} 编译失败:\\n${detail}`));\n return;\n }\n resolve(stdout);\n });\n });\n}\n","/**\n * 定位 wcc / wcsc 可执行文件\n *\n * 使用开发者工具自带的命令行版本(node_modules/wcc-exec),而不是 wcc.node 原生模块:\n * 原生模块在 Node 进程里重复调用会随机触发 std::bad_alloc 崩溃,\n * 子进程方式则天然隔离,单次编译只需几毫秒。\n *\n * 没有开发者工具时(如 Linux、CI),用微信官方 npm 包 miniprogram-compiler 自带的编译器。\n * 它的编译内核较旧(20200413,开发者工具为 20211229):页面的编译结果相同,\n * 只有模板运行时有少量差异,如旧版限制单页最多 16000 个节点。\n */\n\nimport fs from 'fs';\nimport path from 'path';\nimport { findDevtoolsPackageDirs } from '@open-wxa/wxa-runner-extractor';\n\nexport interface CompilerBinaries {\n wcc: string;\n wcsc: string;\n}\n\nlet cached: CompilerBinaries | null = null;\n\nexport function findCompilerBinaries(): CompilerBinaries {\n if (!cached) cached = locateCompilerBinaries(findDevtoolsPackageDirs());\n return cached;\n}\n\n/**\n * 依次查找:环境变量 WXA_WCC + WXA_WCSC、开发者工具、miniprogram-compiler\n *\n * @throws 都找不到时\n */\nexport function locateCompilerBinaries(devtoolsDirs: string[]): CompilerBinaries {\n const ext = process.platform === 'win32' ? '.exe' : '';\n const candidates: CompilerBinaries[] = [];\n\n if (process.env.WXA_WCC && process.env.WXA_WCSC) {\n candidates.push({ wcc: process.env.WXA_WCC, wcsc: process.env.WXA_WCSC });\n }\n for (const dir of devtoolsDirs) {\n const execDir = path.join(dir, 'node_modules', 'wcc-exec');\n candidates.push({ wcc: path.join(execDir, 'wcc' + ext), wcsc: path.join(execDir, 'wcsc' + ext) });\n }\n const packaged = findPackagedCompiler(ext);\n if (packaged) candidates.push(packaged);\n\n const found = candidates.find(c => fs.existsSync(c.wcc) && fs.existsSync(c.wcsc));\n if (!found) {\n throw new Error(\n '没有找到 wcc/wcsc 编译器。请安装微信开发者工具,或重新安装依赖以安装 miniprogram-compiler(Linux 只支持 x86-64),' +\n '或通过 WXA_DEVTOOLS_PATH / WXA_WCC + WXA_WCSC 指定位置。'\n );\n }\n // npm 包里的 Linux 版没有可执行权限\n if (found === packaged) {\n for (const file of [found.wcc, found.wcsc]) {\n try {\n fs.accessSync(file, fs.constants.X_OK);\n } catch {\n fs.chmodSync(file, 0o755);\n }\n }\n }\n return found;\n}\n\n/** miniprogram-compiler 里当前平台的编译器;它是可选依赖,没装上或平台不支持时返回 null */\nfunction findPackagedCompiler(ext: string): CompilerBinaries | null {\n const platformDir =\n process.platform === 'darwin' ? 'mac'\n : process.platform === 'win32' ? 'windows'\n : process.platform === 'linux' && process.arch === 'x64' ? 'linux'\n : null;\n if (!platformDir) return null;\n\n let root: string;\n try {\n root = path.dirname(require.resolve('miniprogram-compiler/package.json'));\n } catch {\n return null;\n }\n const dir = path.join(root, 'bin', platformDir);\n return { wcc: path.join(dir, 'wcc' + ext), wcsc: path.join(dir, 'wcsc' + ext) };\n}\n","/**\n * WXSS 编译(wcsc)\n *\n * 输出是 \"key=value=key=value\" 交替的字符串,value 是转义过的 JS 源码:\n * comm 公共运行时(setCssToHead、rpx 换算)以及非页面样式(app.wxss 等)的立即执行代码\n * ./pages/a.wxss 每个页面/组件一个 setCssToHead(...),返回的函数在页面创建时调用\n */\n\nimport { runCompiler } from './exec';\nimport { findCompilerBinaries } from './locate';\n\nexport interface WxssCompileOptions {\n cwd: string;\n /** 页面和组件的 wxss,./ 开头 */\n pageFiles: string[];\n /** 其余样式(app.wxss),以公共样式形式写入 comm */\n commonFiles: string[];\n}\n\nexport interface WxssCompileResult {\n comm: string;\n /** 键为不带 ./ 的路径,如 \"pages/index/index.wxss\" */\n pages: Record<string, string>;\n}\n\nexport async function compileWxss(options: WxssCompileOptions): Promise<WxssCompileResult> {\n const { wcsc } = findCompilerBinaries();\n const files = [...options.pageFiles, ...options.commonFiles];\n if (files.length === 0) {\n return { comm: '', pages: {} };\n }\n\n const args = ['-db', '-pc', String(options.pageFiles.length), ...files];\n const raw = parseWcscOutput(await runCompiler(wcsc, args, options.cwd));\n\n const pages: Record<string, string> = {};\n for (const file of options.pageFiles) {\n if (raw[file] !== undefined) pages[file.replace(/^\\.\\//, '')] = raw[file];\n }\n return { comm: raw.comm || '', pages };\n}\n\nexport function parseWcscOutput(output: string): Record<string, string> {\n const parts = output.split('=');\n const result: Record<string, string> = {};\n for (let i = 0; i + 1 < parts.length; i += 2) {\n result[parts[i]] = unescapeJsString(parts[i + 1]);\n }\n return result;\n}\n\nconst SIMPLE_ESCAPES: Record<string, string> = {\n n: '\\n', t: '\\t', r: '\\r', b: '\\b', f: '\\f', v: '\\v', '0': '\\0'\n};\n\nexport function unescapeJsString(value: string): string {\n return value.replace(/\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|[\\s\\S])/g, (_, seq: string) => {\n if ((seq[0] === 'x' || seq[0] === 'u') && seq.length > 1) {\n return String.fromCharCode(parseInt(seq.slice(1), 16));\n }\n return SIMPLE_ESCAPES[seq] ?? seq;\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,aAAe;AACf,gBAAe;AACf,IAAAC,eAAiB;;;ACRjB,2BAAyB;AAEzB,IAAM,aAAa,MAAM,OAAO;AAEzB,SAAS,YAAY,KAAa,MAAgB,KAA8B;AACrF,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,uCAAS,KAAK,MAAM,EAAE,KAAK,WAAW,YAAY,UAAU,OAAO,GAAG,CAAC,OAAO,QAAQ,WAAW;AAC/F,UAAI,OAAO;AACT,cAAM,UAAU,UAAU,UAAU,MAAM,SAAS,KAAK;AACxD,eAAO,IAAI,MAAM,GAAG,IAAI,MAAM,OAAO,EAAE,IAAI,CAAC;AAAA,EAAW,MAAM,EAAE,CAAC;AAChE;AAAA,MACF;AACA,cAAQ,MAAM;AAAA,IAChB,CAAC;AAAA,EACH,CAAC;AACH;;;ACHA,gBAAe;AACf,kBAAiB;AACjB,kCAAwC;AAOxC,IAAI,SAAkC;AAE/B,SAAS,uBAAyC;AACvD,MAAI,CAAC,OAAQ,UAAS,2BAAuB,qDAAwB,CAAC;AACtE,SAAO;AACT;AAOO,SAAS,uBAAuB,cAA0C;AAC/E,QAAM,MAAM,QAAQ,aAAa,UAAU,SAAS;AACpD,QAAM,aAAiC,CAAC;AAExC,MAAI,QAAQ,IAAI,WAAW,QAAQ,IAAI,UAAU;AAC/C,eAAW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iler",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Wrapper around the official WXML/WXSS compilers (wcc/wcsc) shipped with WeChat DevTools",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "open-wxa",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@open-wxa/wxa-runner-extractor": "0.2.
|
|
22
|
+
"@open-wxa/wxa-runner-extractor": "0.2.3"
|
|
23
23
|
},
|
|
24
24
|
"optionalDependencies": {
|
|
25
25
|
"miniprogram-compiler": "0.2.3"
|