Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(treeshaking): allowing tree-shaking with terser #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function transformMain(

output.push(
`/* normalize component */
import __normalizer from "${NORMALIZER_ID}"
import {normalizeComponent as __normalizer, getExports as __getExports} from "${NORMALIZER_ID}"
var __component__ = /*#__PURE__*/__normalizer(
_sfc_main,
_sfc_render,
Expand Down Expand Up @@ -137,7 +137,7 @@ var __component__ = /*#__PURE__*/__normalizer(

let resolvedMap: RawSourceMap | undefined = scriptMap

output.push(`export default __component__.exports`)
output.push(`export default /*#__PURE__*/ __getExports(__component__)`)
Copy link
Author

@thebanjomatic thebanjomatic Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also have not exported the getExports function from the normalizer module and instead just used an IIFE:

output.push(`export default /*#__PURE__*/ (function() { return __component__.exports })()`)

But I thought the exported function was more clear. That said, the IIFE approach wouldn't require touching the NORMALIZER_ID module at all, so it might be preferable for that reason.


// handle TS transpilation
let resolvedCode = output.join('\n')
Expand Down
7 changes: 6 additions & 1 deletion src/utils/componentNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ export const NORMALIZER_ID = '\0plugin-vue2:normalizer'
// This module is a runtime utility for cleaner component module output and will
// be included in the final webpack user bundle.
export const normalizerCode = `
export default function normalizeComponent (
// Used to facilitate tree-shaking since property access is not guaranteed to be pure
export function getExports(component) {
return component.exports
}

export function normalizeComponent (
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched this to be a named export instead out of habit since I've run into issues in the past with mixing named and default exports for commonjs modules.

I can switch this back to the default export if you'd prefer.

scriptExports,
render,
staticRenderFns,
Expand Down