Skip to content

Commit

Permalink
Add supports for importing a folder with trailing slash on the path (#…
Browse files Browse the repository at this point in the history
…529)

* Avoid duplicated trailing slash on the folder path

* Add a test case for trailing slash on a path of folder

* Add an usage of import relative path in directory index file
  • Loading branch information
jihchi committed Jun 23, 2020
1 parent d203b02 commit 55fb04c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/commands/import-resolver.ts
Expand Up @@ -29,7 +29,8 @@ export function getImportStats(dirLoc: string, spec: string): Stats | false {
/** Resolve an import based on the state of the file/folder found on disk. */
function resolveSourceSpecifier(spec: string, stats: Stats | false, isBundled: boolean) {
if (stats && stats.isDirectory()) {
spec = spec + '/index.js';
const trailingSlash = spec.endsWith('/') ? '' : '/';
spec = spec + trailingSlash + 'index.js';
} else if (!stats && !spec.endsWith('.js')) {
spec = spec + '.js';
}
Expand Down
@@ -1 +1,4 @@
import sort2 from "../sort.js";
console.log(sort2);

export default "Button";
3 changes: 2 additions & 1 deletion test/build/resolve-imports/expected-build/_dist_/index.js
Expand Up @@ -5,12 +5,13 @@ import sort_ from "/_dist_/sort.js";
import sort__ from "/_dist_/sort.js";
console.log(sort2, sort_, sort__);
import components2 from "./components/index.js";
import components______ from "./components/index.js";
import components_ from "./components/index.js";
import components__ from "./components/index.js";
import components___ from "/_dist_/components/index.js";
import components____ from "/_dist_/components/index.js";
import components_____ from "/_dist_/components/index.js";
console.log(components2, components_, components__, components___, components____, components_____);
console.log(components2, components_, components__, components___, components____, components_____, components______);
import styles from "./components/style.css.proxy.js";
import styles_ from "/_dist_/components/style.css.proxy.js";
console.log(styles, styles_);
5 changes: 4 additions & 1 deletion test/build/resolve-imports/src/components/index.js
@@ -1 +1,4 @@
export default 'Button';
import sort from '../sort';
console.log(sort);

export default 'Button';
5 changes: 3 additions & 2 deletions test/build/resolve-imports/src/index.js
Expand Up @@ -10,15 +10,16 @@ console.log(sort, sort_, sort__);

// Importing a directory index.js file
import components from './components'; // relative import
import components______ from './components/'; // relative import with trailing slash
import components_ from './components/index'; // relative import with index appended
import components__ from './components/index.js'; // relative import with index appended
import components___ from 'src/components'; // bare import using mount
import components____ from 'src/components/index'; // bare import using mount and index appended
import components_____ from 'src/components/index.js'; // bare import using mount and index.js appended
console.log(components, components_, components__, components___, components____, components_____);
console.log(components, components_, components__, components___, components____, components_____, components______);


// Importing something that isn't JS
import styles from './components/style.css'; // relative import
import styles_ from 'src/components/style.css'; // relative import
console.log(styles, styles_);
console.log(styles, styles_);

1 comment on commit 55fb04c

@vercel
Copy link

@vercel vercel bot commented on 55fb04c Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.