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

Add supports for importing a folder with trailing slash on the path #529

Merged

Conversation

jihchi
Copy link
Contributor

@jihchi jihchi commented Jun 23, 2020

Currently, we will always append /index.js to the folder path when we import a directory index.js file, for example:

import components from './Foo';
// becomes
import components from './Foo/index.js';

It will be problematic when we serve the files and load it in browser, for example, say we have following folder structure:

  1. ./src/index.js
import Foo from './Foo/'; // notice that there is a trailing slash
console.log(Foo);
  1. ./src/Foo/index.js
import Bar from '../Bar.js'; // notice that this is a relative import
export default "Foo " + Bar;
  1. ./src/Bar.js
export default "Bar";

now we serve the src folder on http://localhost:8080 and load http://localhost:8080/index.js in browser:

  1. in the ./src/index.js file, syntax import Foo from './Foo/'; becomes import Foo from './Foo//index.js'; and then
  2. browser will fetch the file by the URL http://localhost:8080/Foo//index.js, this will work, but then
  3. browser will fetch ../Bar.js by the URL http://localhost:8080/Foo/Bar.js, this won't work, we will get 404 error.

This PR attempts to fix the issue by simply check whether the path does ends with trailing slash.

@vercel
Copy link

vercel bot commented Jun 23, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/q9559fng6
✅ Preview: https://snowpack-git-fork-jihchi-fixtrailingslashoffolderpath.pikapkg.vercel.app

@vercel vercel bot temporarily deployed to Preview June 23, 2020 14:05 Inactive
@jihchi jihchi force-pushed the fix_trailing_slash_of_folder_path branch from 36891df to 0111d41 Compare June 23, 2020 14:05
@FredKSchott
Copy link
Owner

+1, makes sense to me. Thanks for contributing!

@FredKSchott FredKSchott merged commit 55fb04c into FredKSchott:master Jun 23, 2020
@jihchi jihchi deleted the fix_trailing_slash_of_folder_path branch June 23, 2020 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants