Skip to content

Commit

Permalink
feat(#14): do not generate unused imported return bindings in TS
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix-ru committed Jun 8, 2024
1 parent a8b8b23 commit c8b4cb7
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 57 deletions.
11 changes: 9 additions & 2 deletions crates/fervid_codegen/src/control_flow/sfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,24 @@ impl CodegenContext {
.bindings_helper
.options_api_bindings
.as_ref()
.map_or_else(|| [].iter().chain([].iter()), |v| v.imports.iter().chain(v.setup.iter()));
.map_or_else(
|| [].iter().chain([].iter()),
|v| v.imports.iter().chain(v.setup.iter()),
);
let setup_iter = self.bindings_helper.setup_bindings.iter();

let all_bindings = options_api_iter.chain(setup_iter);
let is_ts = self.bindings_helper.is_ts;

// https://github.com/vuejs/core/blob/530d9ec5f69a39246314183d942d37986c01dc46/packages/compiler-sfc/src/compileScript.ts#L826-L844
for binding in all_bindings {
match binding.1 {
// `get smth() { return smth }`
BindingTypes::Imported => {
// TODO Skip if TS and binding is unused
// Skip if TS and binding is unused
if is_ts && !self.bindings_helper.used_bindings.contains_key(&binding.0) {
continue;
}

let ident = Ident {
span: DUMMY_SP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,47 @@ export default {
"
`;
exports[`SFC compile <script setup> > should expose top level declarations w/ ts 1`] = `
"import { xx } from './x';
let aa = 1;
const bb = 2;
function cc() {}
class dd {
}
import { x } from './x';
export default {
__name: "anonymous",
setup () {
let a = 1;
const b = 2;
function c() {}
class d {
}
return {
get aa () {
return aa;
},
set aa (v){
aa = v;
},
bb,
cc,
dd,
get a () {
return a;
},
set a (v){
a = v;
},
b,
c,
d
};
}
};
"
`;
exports[`SFC genDefaultAs > <script setup> only 1`] = `
"const _sfc_ = {
__name: "anonymous",
Expand Down
48 changes: 48 additions & 0 deletions crates/fervid_napi/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,54 @@ describe('SFC compile <script setup>', () => {
// })
assertCode(content)
})

// Difference with the original is that in TS `x` and `xx` may be types,
// and compiler doesn't know without looking at the usage.
// Since neither are used, they are excluded.
test('should expose top level declarations w/ ts', () => {
const { content } = compile(`
<script setup lang="ts">
import { x } from './x'
let a = 1
const b = 2
function c() {}
class d {}
</script>
<script lang="ts">
import { xx } from './x'
let aa = 1
const bb = 2
function cc() {}
class dd {}
</script>
`)

expect(content).toMatch(
` return {
get aa () {
return aa;
},
set aa (v){
aa = v;
},
bb,
cc,
dd,
get a () {
return a;
},
set a (v){
a = v;
},
b,
c,
d
};`,
)

assertCode(content)
})
})

describe('SFC analyze <script> bindings', () => {
Expand Down
56 changes: 3 additions & 53 deletions crates/fervid_napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,62 +208,12 @@ switch (platform) {
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'napi.linux-arm-musleabihf.node'))
try {
if (localFileExisted) {
nativeBinding = require('./napi.linux-arm-musleabihf.node')
} else {
nativeBinding = require('@fervid/napi-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'napi.linux-arm-gnueabihf.node'))
try {
if (localFileExisted) {
nativeBinding = require('./napi.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@fervid/napi-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(join(__dirname, 'napi.linux-riscv64-musl.node'))
try {
if (localFileExisted) {
nativeBinding = require('./napi.linux-riscv64-musl.node')
} else {
nativeBinding = require('@fervid/napi-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(join(__dirname, 'napi.linux-riscv64-gnu.node'))
try {
if (localFileExisted) {
nativeBinding = require('./napi.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@fervid/napi-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(join(__dirname, 'napi.linux-s390x-gnu.node'))
localFileExisted = existsSync(join(__dirname, 'napi.linux-arm-gnueabihf.node'))
try {
if (localFileExisted) {
nativeBinding = require('./napi.linux-s390x-gnu.node')
nativeBinding = require('./napi.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@fervid/napi-linux-s390x-gnu')
nativeBinding = require('@fervid/napi-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
Expand Down
18 changes: 16 additions & 2 deletions crates/fervid_transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use error::TransformError;
use fervid_core::SfcDescriptor;
use fervid_core::{SfcDescriptor, SfcScriptBlock, SfcScriptLang};
use misc::infer_name;
use script::transform_and_record_scripts;
use style::{attach_scope_id, create_style_scope, transform_style_blocks};
Expand Down Expand Up @@ -29,9 +29,23 @@ pub fn transform_sfc<'o>(
options: TransformSfcOptions<'o>,
errors: &mut Vec<TransformError>,
) -> TransformSfcResult {
// Transform the scripts
// Create the bindings helper
let mut bindings_helper = BindingsHelper::default();
bindings_helper.is_prod = options.is_prod;

// TS if any of scripts is TS.
// Unlike the official compiler, we don't care if languages are mixed, because nothing changes.
let recognize_lang = |script: &SfcScriptBlock| matches!(script.lang, SfcScriptLang::Typescript);
bindings_helper.is_ts = sfc_descriptor
.script_setup
.as_ref()
.map_or(false, recognize_lang)
|| sfc_descriptor
.script_legacy
.as_ref()
.map_or(false, recognize_lang);

// Transform the scripts
let mut transform_result = transform_and_record_scripts(
sfc_descriptor.script_setup,
sfc_descriptor.script_legacy,
Expand Down
2 changes: 2 additions & 0 deletions crates/fervid_transform/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct BindingsHelper {
pub custom_directives: HashMap<FervidAtom, CustomDirectiveBinding>,
/// Are we compiling for DEV or PROD
pub is_prod: bool,
/// Is Typescript or Javascript used
pub is_ts: bool,
/// Scopes of the `<template>` for in-template variable resolutions
pub template_scopes: Vec<TemplateScope>,
/// Bindings in `<script setup>`
Expand Down

0 comments on commit c8b4cb7

Please sign in to comment.