IndexXuan / vue-cli-plugin-vite

Use vite today, with vue-cli.

Home Page:https://github.com/IndexXuan/vue-cli-plugin-vite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

require.context replacement doesn't work for nested paths on Windows

danielelkington opened this issue · comments

This plugin will rewrite the following

const customTabsContext = require.context('@/components/tabs', true, /^.*(.vue)$/)

to something like this when using Windows:

const customTabsContext = 
  (function() {
    var map = {
      './category1\Tab1.vue': import_context_module_0_0,
'./category1\Tab2.vue': import_context_module_0_1,
'./category2\Tab3.vue': import_context_module_0_2,

    };
    var req = function req(key) {
      return map[key] || (function() { throw new Error("Cannot find module '" + key + "'.") }());
    }
    req.keys = function() {
      return Object.keys(map);
    }
    return req;
  })()

Note that the keys in the map use backslashes \ for nested paths. However to put a backslash into a string literal you need to escape it \\. Therefore require.context won't work for nested paths on Windows.

This works fine on Mac OS, as the keys in the map will use forward slashes /.

@danielelkington dont't have Windows PC, would you mind give a PR ?
I think https://vitejs.dev/guide/api-plugin.html#path-normalization maybe help.

https://github.com/IndexXuan/vite-plugin-vue-cli/blob/main/src/lib/resolve.ts#L99
https://nodejs.org/api/path.html#path_path_relative_from_to
L99 wrapped in vite#normalizePath will help ? can you do it locally(./node_modules/vite-plugin-vue-cli/dist/index.js) and give a feedback ?

Will give this a go tomorrow. Thanks for creating this awesome plug-in!

You're right - normalizePath fixed the problem. Have raised a PR.

Release: v0.3.2

BTW I change src/lib/resolve.ts#L100 to pass my testcase, if something wrong, please feedback.