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: __VITE_PRELOAD__ replacement error, fix #3051 #4163

Merged
merged 5 commits into from Jul 8, 2021

Conversation

ygj6
Copy link
Member

@ygj6 ygj6 commented Jul 7, 2021

Description

fix #3051

When there is only one dynamic import, the input parameter deps of the preload function,

/**
* Helper for preloading CSS and direct imports of async chunks in parallel to
* the async chunk itself.
*/
function preload(baseModule: () => Promise<{}>, deps?: string[]) {
// @ts-ignore

when terser compresses for the third time, it will think that deps is a constant string, move directly to the place of call and delete the input parameters.

Before terser minify,complete file,before.txt:

import { o as openBlock, c as createBlock, a as createVNode, w as withCtx, b as createTextVNode, r as resolveComponent, d as createRouter, e as createWebHashHistory, f as createApp } from "./vendor.30acece7.js";
let scriptRel;
const seen = {};
const __vitePreload = function preload(baseModule, deps) {
  if (!deps) {
    return baseModule();
  }
  if (scriptRel === void 0) {
    const relList = document.createElement("link").relList;
    scriptRel = relList && relList.supports && relList.supports("modulepreload") ? "modulepreload" : "preload";
  }
  return Promise.all(deps.map((dep) => {
    ......
  })).then(() => baseModule());
};
......
const routes = [
  { path: "/", name: "Home", component: _sfc_main$1 },
  {
    path: "/about",
    name: "About",
    component: () => __vitePreload(() => import("./About.d4daf36e.js"), true ? "__VITE_PRELOAD__" : void 0)
  }
];
......

After terser minify,complete file,after.txt:

......
var E = a({
    routes: [ {
    ......
    }, {
        path: "/about",
        name: "About",
        component: () => function(e, t) {
           ......
            return Promise.all("__VITE_PRELOAD__".map((e => {
               ......
            }))).then((() => import("./About.d4daf36e.js")));
        }()
    } ],
    history: u()
});
......

The above behavior will cause the __VITE_PRELOAD__ marker to move from the back of the import statement to the front, causing the code.indexOf(preloadMarker, end) to not find the mark,resulting in no replacement of the __VITE_PRELOAD__

const markPos = code.indexOf(preloadMarker, end)
if (markPos > 0) {
s.overwrite(
markPos - 1,
markPos + preloadMarker.length + 1,
// the dep list includes the main chunk, so only need to
// preload when there are actual other deps.
deps.size > 1
? `[${[...deps].map((d) => JSON.stringify(d)).join(',')}]`
: `void 0`
)

Finally, __VITE_PRELOAD__ was incorrectly replaced with void 0 by the following code

// there may still be markers due to inlined dynamic imports, remove
// all the markers regardless
chunk.code = chunk.code.replace(preloadMarkerRE, 'void 0')


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 added the p3-minor-bug An edge case that only affects very specific usage (priority) label Jul 7, 2021
ygj6 and others added 3 commits July 7, 2021 20:51
Co-authored-by: patak <matias.capeletto@gmail.com>
Co-authored-by: patak <matias.capeletto@gmail.com>
@patak-dev patak-dev requested a review from sodatea July 8, 2021 06:12
@sodatea sodatea merged commit d377aae into vitejs:main Jul 8, 2021
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minifying the code in 3 passes leads to a syntax error
4 participants