diff --git a/crates/fervid_napi/.yarn/install-state.gz b/crates/fervid_napi/.yarn/install-state.gz index 85f1674..8480ec8 100644 Binary files a/crates/fervid_napi/.yarn/install-state.gz and b/crates/fervid_napi/.yarn/install-state.gz differ diff --git a/crates/fervid_napi/__tests__/__snapshots__/compileScript.spec.ts.snap b/crates/fervid_napi/__tests__/__snapshots__/compileScript.spec.ts.snap new file mode 100644 index 0000000..a0e1122 --- /dev/null +++ b/crates/fervid_napi/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -0,0 +1,53 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`SFC analyze + `, + { + filename: 'FooBar.vue', + id: '' + }, + ) + expect(content).toMatch(`export default { + __name: "FooBar"`) + assertCode(content) + }) + + test('do not overwrite manual name (object)', () => { + const { content } = compile( + ` + + `, + { + filename: 'FooBar.vue', + id: '' + }, + ) + expect(content).not.toMatch(`name: 'FooBar'`) + expect(content).toMatch(`name: 'Baz'`) + assertCode(content) + }) + + test('do not overwrite manual name (call)', () => { + const { content } = compile( + ` + + `, + { + filename: 'FooBar.vue', + id: '' + }, + ) + expect(content).not.toMatch(`name: 'FooBar'`) + expect(content).toMatch(`name: 'Baz'`) + assertCode(content) + }) + }) +}) + +// https://github.com/vuejs/core/blob/272ab9fbdcb1af0535108b9f888e80d612f9171d/packages/compiler-sfc/__tests__/utils.ts#L11-L24 +function compile(src: string, options: FervidCompileOptions) { + const compiler = new Compiler() + const result = compiler.compileSync(src, options) + + if (result.errors.length) { + console.warn(result.errors[0]) + } + + return { + content: result.code + } +} + +function assertCode(code: string) { + // parse the generated code to make sure it is valid + try { + babelParse(code, { + sourceType: 'module', + plugins: [ + 'typescript', + ['importAttributes', { deprecatedAssertSyntax: true }], + ], + }) + } catch (e: any) { + console.log(code) + throw e + } + expect(code).toMatchSnapshot() +} diff --git a/crates/fervid_napi/benchmark/bench.ts b/crates/fervid_napi/benchmark/bench.ts index 34936c1..661f605 100644 --- a/crates/fervid_napi/benchmark/bench.ts +++ b/crates/fervid_napi/benchmark/bench.ts @@ -7,7 +7,7 @@ import { join } from 'node:path' import { cpus } from 'node:os' import { compileScript, compileTemplate, parse } from '@vue/compiler-sfc' -import { Compiler } from '../index' +import { Compiler, FervidCompileOptions } from '../index' // Increase libuv thread pool for a better async result. // 4 threads is a default thread pool size. @@ -18,6 +18,11 @@ const input = readFileSync(join(__dirname, '../../fervid/benches/fixtures/input. encoding: 'utf-8', }) +const options: FervidCompileOptions = { + filename: 'input.vue', + id: '' +} + async function run() { const compiler = new Compiler() @@ -42,7 +47,7 @@ async function run() { }), b.add('@fervid/napi sync', () => { - compiler.compileSync(input) + compiler.compileSync(input, options) }), // The code below makes sure that async framework is not flawed. @@ -53,24 +58,24 @@ async function run() { b.add('@fervid/napi sync promise (4 threads)', () => { return Promise.allSettled( - Array.from({ length: 4 }, (_) => new Promise((resolve) => (compiler.compileSync(input), resolve()))), + Array.from({ length: 4 }, (_) => new Promise((resolve) => (compiler.compileSync(input, options), resolve()))), ) }), b.add(`@fervid/napi sync promise (${CPUS} threads)`, () => { return Promise.allSettled( - Array.from({ length: CPUS }, (_) => new Promise((resolve) => (compiler.compileSync(input), resolve()))), + Array.from({ length: CPUS }, (_) => new Promise((resolve) => (compiler.compileSync(input, options), resolve()))), ) }), // END b.add('@fervid/napi async (4 threads)', () => { - return Promise.allSettled(Array.from({ length: 4 }, (_) => compiler.compileAsync(input))) + return Promise.allSettled(Array.from({ length: 4 }, (_) => compiler.compileAsync(input, options))) }), b.add(`@fervid/napi async CPUS (${CPUS} threads)`, () => { - return Promise.allSettled(Array.from({ length: CPUS }, (_) => compiler.compileAsync(input))) + return Promise.allSettled(Array.from({ length: CPUS }, (_) => compiler.compileAsync(input, options))) }), // Custom cycle function to account for the async nature diff --git a/crates/fervid_napi/package.json b/crates/fervid_napi/package.json index e73fb5c..087f016 100644 --- a/crates/fervid_napi/package.json +++ b/crates/fervid_napi/package.json @@ -59,6 +59,7 @@ "version": "napi version" }, "devDependencies": { + "@babel/parser": "^7.24.4", "@napi-rs/cli": "^2.18.0", "@swc-node/register": "^1.9.0", "@swc/core": "^1.4.8", diff --git a/crates/fervid_napi/yarn.lock b/crates/fervid_napi/yarn.lock index 4426d18..ca20cb2 100644 --- a/crates/fervid_napi/yarn.lock +++ b/crates/fervid_napi/yarn.lock @@ -79,6 +79,15 @@ __metadata: languageName: node linkType: hard +"@babel/parser@npm:^7.24.4": + version: 7.24.4 + resolution: "@babel/parser@npm:7.24.4" + bin: + parser: ./bin/babel-parser.js + checksum: 8381e1efead5069cb7ed2abc3a583f4a86289b2f376c75cecc69f59a8eb36df18274b1886cecf2f97a6a0dff5334b27330f58535be9b3e4e26102cc50e12eac8 + languageName: node + linkType: hard + "@babel/types@npm:^7.8.3": version: 7.23.4 resolution: "@babel/types@npm:7.23.4" @@ -368,6 +377,7 @@ __metadata: version: 0.0.0-use.local resolution: "@fervid/napi@workspace:." dependencies: + "@babel/parser": "npm:^7.24.4" "@fervid/napi-android-arm-eabi": "npm:0.2.1" "@fervid/napi-android-arm64": "npm:0.2.1" "@fervid/napi-darwin-arm64": "npm:0.2.1"