Skip to content

Commit

Permalink
Fix import sources needing trailing slash for file aliases (#1385)
Browse files Browse the repository at this point in the history
* Fix import sources needing trailing slash for file aliases

* Remove use of removeTrailingSlash from findMatchingAliasEntry
  • Loading branch information
novaluke authored Oct 27, 2020
1 parent eb51fa0 commit f9ab0cf
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ function normalizeAlias(config: SnowpackConfig, cwd: string, createMountAlias: b
replacement.startsWith('/')
) {
delete cleanAlias[target];
cleanAlias[addTrailingSlash(target)] = addTrailingSlash(path.resolve(cwd, replacement));
cleanAlias[target] = addTrailingSlash(path.resolve(cwd, replacement));
}
}
return cleanAlias;
Expand Down
2 changes: 1 addition & 1 deletion snowpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export function findMatchingAliasEntry(

for (const [from, to] of Object.entries(config.alias)) {
let foundType: 'package' | 'path' = isPackageAliasEntry(to) ? 'package' : 'path';
const isExactMatch = spec === removeTrailingSlash(from);
const isExactMatch = spec === from;
const isDeepMatch = spec.startsWith(addTrailingSlash(from));
if (isExactMatch || isDeepMatch) {
return {
Expand Down
3 changes: 2 additions & 1 deletion test/build/resolve-imports/__snapshots__
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ import sort_ from './sort.js'; // absolute import
import sort__ from './sort.js'; // bare import using alias
import sort___ from './sort.js'; // bare import using alias + extension
import sort____ from './sort.js'; // bare import using alias with trailing slash
console.log(sort, sort_, sort__, sort___, sort___, sort____);
import sort_____ from './sort.js'; // bare import using file alias
console.log(sort, sort_, sort__, sort___, sort___, sort____, sort_____);
// Note: file does not need to exist for these checks:
import svelteFile from './foo.js'; // plugin-provided file extension
import svelteFile_ from './foo.js'; // plugin-provided, missing file extension
Expand Down
1 change: 1 addition & 0 deletions test/build/resolve-imports/snowpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'@app': './src',
'@/': './src/',
'%': '.',
"@sort": "./src/sort.js",
},
mount: {
'./src': '/_dist_',
Expand Down
3 changes: 2 additions & 1 deletion test/build/resolve-imports/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import sort_ from '/_dist_/sort.js'; // absolute import
import sort__ from '@app/sort'; // bare import using alias
import sort___ from '@app/sort.js'; // bare import using alias + extension
import sort____ from '@/sort'; // bare import using alias with trailing slash
console.log(sort, sort_, sort__, sort___, sort___, sort____);
import sort_____ from '@sort'; // bare import using file alias
console.log(sort, sort_, sort__, sort___, sort___, sort____, sort_____);

// Note: file does not need to exist for these checks:
import svelteFile from './foo.svelte'; // plugin-provided file extension
Expand Down

1 comment on commit f9ab0cf

@vercel
Copy link

@vercel vercel bot commented on f9ab0cf Oct 27, 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.