With Webpack bundling how to either import a file dynamically or not, depending on some process.env.VARIABLE

18 views Asked by At

I would like to be able to tell the webpack bundler to import some file right away if process.env.SOME_VAR evaluates to true and to lazy-load (dynamically import) that file otherwise.

Something like this:

import A from "@/views/A.vue";
let B;
if (process.env.SOME_VAR) {
    import B from "@/views/B.vue";
} else {
    B = () => import("@/views/B.vue");
}
const vueRouterConfig = {
    // ...
    b: {
        component: B
    }
};

The about fails, because import may only appear at the top level of a file.

Does someone have a workaround

0

There are 0 answers