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

Replacement for template strings does not work #8

Closed
leifmarcus opened this issue Jun 20, 2022 · 5 comments
Closed

Replacement for template strings does not work #8

leifmarcus opened this issue Jun 20, 2022 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@leifmarcus
Copy link

When bundling the application for production with the terser minifier, the __dynamic_base__ is not overridden, when it appears inside a template string like /__dynamic_base__/${e}. (After the compression, the preload helper will have a code like that for saving deps, that are already loaded).

This is related to the replaceQuotes function inside the plugin utils.

export function replaceQuotes(mark: string, placeholder: string, code: string) {

A suggested change would be:

function replaceQuotes(mark, placeholder, code) {
    const singleMark = `'${mark}`;
    const singlePlaceholder = `${placeholder}+'/`;
    const doubleMark = `"${mark}`;
    const doublePlaceholder = `${placeholder}+"/`;
    const templateMark = `\`${mark}`;
    const templatePlaceholder = `\`\$\{${placeholder}\}/`;
    return replace(doubleMark, doublePlaceholder, replace(singleMark, singlePlaceholder, replace(templateMark, templatePlaceholder, code)));
}
@chenxch chenxch added good first issue Good for newcomers bug Something isn't working labels Jun 21, 2022
@chenxch
Copy link
Owner

chenxch commented Jun 21, 2022

@leifmarcus Thanks for your feedback, the fix version 0.4.3 has been released.

@chenxch chenxch closed this as completed Jun 23, 2022
@modten
Copy link

modten commented Jan 3, 2024

Still encounter this problem with 1.0.0.
The ast function doesn't seem to handle TemplateLiteral.

@joarfish
Copy link
Contributor

joarfish commented Jan 8, 2024

@modten Yes, you might be right. Can you open a new issue with some example code and your build target?

@modten
Copy link

modten commented Jan 11, 2024

@joarfish Sorry, I think there is no need to delve into this problem, because after I use the latest versions of vite and terser, this problem no longer exists.

The steps that caused the original problem were to use vite@2.9.16 and @vitejs/plugin-vue@2.3.4, then use defineAsyncComponent to load the component asynchronously, and finally use terser for compression at build time. Then you can get the code where the problem occurred:

e=`/__dynamic_base__/${e}`
const HelloWorld = defineAsyncComponent(() => import(`./components/HelloWorld.vue`))
{
   build: {
     minify: "terser"
   }
}

@joarfish
Copy link
Contributor

@modten Thanks for the info. Glad that it works for you with the upgraded versions 👍

@chenxch I can still have a look at template literals next week because I think they are not taken into account.

joarfish added a commit to joarfish/vite-plugin-dynamic-base that referenced this issue Jan 26, 2024
This addresses issue chenxch#8.

Signed-off-by: Jonas Klein <giga@jonasklein.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants