Skip to content

Commit

Permalink
fix(compiler): should remove unused-in-template bindings in separated…
Browse files Browse the repository at this point in the history
… mode returns
  • Loading branch information
ShenQingchuan committed Nov 25, 2024
1 parent b506399 commit 8696e85
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 244 deletions.
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dev:lang-server": "NODE_ENV=development pnpm --filter @vue-vine/language-server run dev",
"build:compiler": "NODE_ENV=production pnpm --filter @vue-vine/compiler run build",
"build:vite": "NODE_ENV=production pnpm --filter @vue-vine/vite-plugin run build",
"build:plugin": "NODE_ENV=production pnpm --filter @vue-vine/compiler --filter @vue-vine/vite-plugin run build",
"build:lang-service": "NODE_ENV=production pnpm --filter @vue-vine/language-service run build",
"build:lang-server": "NODE_ENV=production pnpm --filter @vue-vine/language-server run build",
"build:root": "NODE_ENV=production pnpm --filter vue-vine run build",
Expand All @@ -24,13 +25,13 @@
"build:ext": "pnpm run build:ls && pnpm --filter vue-vine-extension run build",
"watch:ext-deps": "pnpm --parallel --filter @vue-vine/compiler --filter @vue-vine/language-service --filter @vue-vine/language-server --filter vue-vine-tsc run dev",
"watch:ext": "pnpm --filter vue-vine-extension run dev",
"test": "esno scripts/run-test.js",
"test:compiler": "pnpm --filter @vue-vine/compiler run test",
"test:e2e": "pnpm --filter @vue-vine/e2e-test run test",
"test:eslint-parser": "pnpm --filter @vue-vine/eslint-parser run test",
"test:nuxt": "pnpm --filter @vue-vine/nuxt run test",
"lint": "esno scripts/run-lint.js",
"lint:fix": "pnpm lint --fix",
"test": "NODE_ENV=test pnpm --parallel --filter @vue-vine/compiler --filter @vue-vine/e2e-test --filter @vue-vine/eslint-parser --filter @vue-vine/nuxt run test --run",
"test:compiler": "pnpm --filter @vue-vine/compiler run test --run",
"test:e2e": "pnpm --filter @vue-vine/e2e-test run test --run",
"test:eslint-parser": "pnpm --filter @vue-vine/eslint-parser run test --run",
"test:nuxt": "pnpm --filter @vue-vine/nuxt run test --run",
"lint": "node -e \"if (process.env.RUN_ENV === 'ci') { console.log('Running pnpm build:eslint'); require('child_process').execSync('pnpm build:eslint', { stdio: 'inherit' }); } console.log('Running pnpm eslint .'); require('child_process').execSync('pnpm eslint .', { stdio: 'inherit' });\"",
"lint:fix": "pnpm eslint . --fix",
"docs:dev": "pnpm --filter vue-vine-docs run dev",
"docs:build": "pnpm --filter vue-vine-docs run build",
"docs:preview": "pnpm --filter vue-vine-docs run preview",
Expand Down Expand Up @@ -80,7 +81,7 @@
}
},
"simple-git-hooks": {
"pre-commit": "pnpm esno scripts/pre-commit.js"
"pre-commit": "pnpm install && pnpm test && pnpm lint && pnpm lint-staged && pnpm esno scripts/pre-commit.js"
},
"lint-staged": {
"*": "eslint . --fix --cache"
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,10 +933,10 @@ function analyzeFileImportStmts(
vineFileCtx.userImports[spec.local.name] = importMeta
}

const isUsedInTemplate = vineFileCtx.vineCompFns.some(
vineCompFn => isImportUsed(vineCompFn, spec.local.name),
const specLocalName = spec.local.name
importMeta.isUsedInTemplate = () => vineFileCtx.vineCompFns.some(
vineCompFn => isImportUsed(vineCompFn, specLocalName),
)
importMeta.isUsedInTemplate = isUsedInTemplate
}
}
const lastImportStmt = fileImportStmts[fileImportStmts.length - 1]
Expand Down
26 changes: 17 additions & 9 deletions packages/compiler/src/template/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,24 @@ export function createSeparatedTemplateComposer(
// return bindings from script and script setup
const allBindings: Record<string, any> = { ...bindingMetadata }
for (const key in vineFileCtx.userImports) {
if (
!vineFileCtx.userImports[key].isType
&& vineFileCtx.userImports[key].isUsedInTemplate
) {
allBindings[key] = true
const isType = vineFileCtx.userImports[key].isType
const isUsedInTemplate = vineFileCtx.userImports[key].isUsedInTemplate?.()

if (isUsedInTemplate) {
if (!isType)
allBindings[key] = true
}
else {
allBindings[key] = false
}
}

let setupFnReturns = '{ '
for (const key in allBindings) {
if (
if (allBindings[key] === false) {
// skip unused bindings
}
else if (
allBindings[key] === true
&& vineFileCtx.userImports[key].source !== 'vue'
&& !vineFileCtx.userImports[key].source.endsWith('.vue')
Expand All @@ -345,9 +352,10 @@ export function createSeparatedTemplateComposer(
else if (bindingMetadata[key] === VineBindingTypes.SETUP_LET) {
// local let binding, also add setter
const setArg = key === 'v' ? '_v' : 'v'
setupFnReturns
+= `get ${key}() { return ${key} }, `
+ `set ${key}(${setArg}) { ${key} = ${setArg} }, `
setupFnReturns += (
`get ${key}() { return ${key} }, `
+ `set ${key}(${setArg}) { ${key} = ${setArg} }, `
)
}
else if (bindingMetadata[key] === VineBindingTypes.PROPS) {
// Skip props bindings
Expand Down
8 changes: 5 additions & 3 deletions packages/compiler/src/template/importUsageCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ function processExp(exp: string, dir?: string): string {
}

function stripStrings(exp: string) {
return exp
.replace(/'[^']*'|"[^"]*"/g, '')
.replace(/`[^`]+`/g, stripTemplateString)
return (
exp
?.replace(/'[^']*'|"[^"]*"/g, '')
?.replace(/`[^`]+`/g, stripTemplateString)
) ?? ''
}

function stripTemplateString(str: string): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export interface VineUserImport {
isType: boolean
isNamespace?: boolean
isDefault?: boolean
isUsedInTemplate?: boolean
isUsedInTemplate?: () => boolean
}

export interface VineFileCtx {
Expand Down
164 changes: 64 additions & 100 deletions packages/compiler/tests/__snapshots__/transform.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,67 +1,5 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`test transform > hmrId should be generated If there is no style 1`] = `
"import {
createElementVNode as _createElementVNode,
openBlock as _openBlock,
createElementBlock as _createElementBlock,
defineComponent as _defineComponent,
useCssVars as _useCssVars,
unref as _unref,
} from "vue";
export const About = (() => {
const __vine = _defineComponent({
name: "About",
/* No props */
/* No emits */
setup(__props, { expose: __expose }) {
__expose();
const props = __props;
return (_ctx, _cache) => {
return (
_openBlock(),
_createElementBlock(
"div",
null,
_cache[0] ||
(_cache[0] = [
_createElementVNode("h2", null, "About page", -1 /* HOISTED */),
]),
)
);
};
},
});
__vine.__hmrId = "ba945b60";
return __vine;
})();
typeof __VUE_HMR_RUNTIME__ !== "undefined" &&
__VUE_HMR_RUNTIME__.createRecord(About.__hmrId, About);
export const _rerender_only = false;
export const _rerender_vcf_fn_name = "";
import.meta.hot?.accept((mod) => {
if (!mod) {
return;
}
const { _rerender_only, _rerender_vcf_fn_name } = mod;
if (!_rerender_vcf_fn_name) {
return;
}
const component = mod[_rerender_vcf_fn_name];
if (_rerender_only) {
__VUE_HMR_RUNTIME__.rerender(component.__hmrId, component.render);
} else {
__VUE_HMR_RUNTIME__.reload(component.__hmrId, component);
}
});
"
`;

exports[`test transform > inline mode output result 1`] = `
"import { useDefaults as _useDefaults } from "vue-vine";
import {
Expand Down Expand Up @@ -532,12 +470,6 @@ export const MyProfile = (() => {
emits,
textColor,
handleRefresh,
ref,
someDefaultExport,
someNamedExport,
someNamespaceExport,
someExternalFunction1,
someExternalFunction2,
v1,
v2,
MyProfile,
Expand Down Expand Up @@ -600,19 +532,7 @@ export const MyApp = (() => {
__restore(),
__temp);
return {
data,
ref,
someDefaultExport,
someNamedExport,
someNamespaceExport,
someExternalFunction1,
someExternalFunction2,
v1,
v2,
MyProfile,
MyApp,
};
return { data, v1, v2, MyProfile, MyApp };
},
});
function __sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
Expand Down Expand Up @@ -762,12 +682,6 @@ export const MyProfile = (() => {
emits,
textColor,
handleRefresh,
ref,
someDefaultExport,
someNamedExport,
someNamespaceExport,
someExternalFunction1,
someExternalFunction2,
v1,
v2,
MyProfile,
Expand Down Expand Up @@ -823,19 +737,7 @@ export const MyApp = (() => {
__restore(),
__temp);
return {
data,
ref,
someDefaultExport,
someNamedExport,
someNamespaceExport,
someExternalFunction1,
someExternalFunction2,
v1,
v2,
MyProfile,
MyApp,
};
return { data, v1, v2, MyProfile, MyApp };
},
});
function __sfc_ssr_render(
Expand Down Expand Up @@ -897,3 +799,65 @@ import.meta.hot?.accept((mod) => {
});
"
`;
exports[`test transform > should generate hmrId If there is no style 1`] = `
"import {
createElementVNode as _createElementVNode,
openBlock as _openBlock,
createElementBlock as _createElementBlock,
defineComponent as _defineComponent,
useCssVars as _useCssVars,
unref as _unref,
} from "vue";
export const About = (() => {
const __vine = _defineComponent({
name: "About",
/* No props */
/* No emits */
setup(__props, { expose: __expose }) {
__expose();
const props = __props;
return (_ctx, _cache) => {
return (
_openBlock(),
_createElementBlock(
"div",
null,
_cache[0] ||
(_cache[0] = [
_createElementVNode("h2", null, "About page", -1 /* HOISTED */),
]),
)
);
};
},
});
__vine.__hmrId = "ba945b60";
return __vine;
})();
typeof __VUE_HMR_RUNTIME__ !== "undefined" &&
__VUE_HMR_RUNTIME__.createRecord(About.__hmrId, About);
export const _rerender_only = false;
export const _rerender_vcf_fn_name = "";
import.meta.hot?.accept((mod) => {
if (!mod) {
return;
}
const { _rerender_only, _rerender_vcf_fn_name } = mod;
if (!_rerender_vcf_fn_name) {
return;
}
const component = mod[_rerender_vcf_fn_name];
if (_rerender_only) {
__VUE_HMR_RUNTIME__.rerender(component.__hmrId, component.render);
} else {
__VUE_HMR_RUNTIME__.reload(component.__hmrId, component);
}
});
"
`;
32 changes: 14 additions & 18 deletions packages/compiler/tests/analyze.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,26 @@ import type { SomeType } from 'types-2'
compileVineTypeScriptFile(content, 'testAnalyzeImports', { compilerHooks: mockCompilerHooks })
expect(mockCompilerCtx.vineCompileErrors.length).toBe(0)
const fileCtx = mockCompilerCtx.fileCtxMap.get('testAnalyzeImports')
expect(fileCtx?.userImports).toMatchInlineSnapshot(`
{
"SomeType": {
"isType": true,
"isUsedInTemplate": false,
"source": "types-2",
},
"Something": {
"isNamespace": true,
"isType": false,
"isUsedInTemplate": false,
"source": "lib-1",
expect(JSON.stringify(fileCtx?.userImports, null, 2)).toMatchInlineSnapshot(`
"{
"ref": {
"source": "vue",
"isType": false
},
"VueReactive": {
"isType": false,
"isUsedInTemplate": false,
"source": "vue",
"isType": false
},
"ref": {
"Something": {
"source": "lib-1",
"isType": false,
"isUsedInTemplate": false,
"source": "vue",
"isNamespace": true
},
}
"SomeType": {
"source": "types-2",
"isType": true
}
}"
`)
})

Expand Down
Loading

0 comments on commit 8696e85

Please sign in to comment.