Skip to content

Commit

Permalink
Do not exclude folders starting with a dot
Browse files Browse the repository at this point in the history
This prevents folders that start with a `.` from being excluded from being imported by modules.

Fixes #2961
  • Loading branch information
matthewp committed Mar 22, 2021
1 parent faa0788 commit 3284e76
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion snowpack/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export async function build(commandOptions: CommandOptions): Promise<SnowpackBui
for (const [mountKey, mountEntry] of Object.entries(config.mount)) {
logger.debug(`Mounting directory: '${mountKey}' as URL '${mountEntry.url}'`);
const files = (await new fdir().withFullPaths().crawl(mountKey).withPromise()) as string[];
const excludePrivate = new RegExp(`\\${path.sep}\\.`);
const excludePrivate = new RegExp(`\\${path.sep}\\..+(?!\\${path.sep})`);
const excludeGlobs = [...config.exclude, ...config.testOptions.files];
const foundExcludeMatch = picomatch(excludeGlobs);

for (const f of files) {
if (excludePrivate.test(f) || foundExcludeMatch(f)) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export async function startServer(
...config.exclude,
...(process.env.NODE_ENV === 'test' ? [] : config.testOptions.files),
];
const excludePrivate = new RegExp(`\\${path.sep}\\.`);

const foundExcludeMatch = picomatch(excludeGlobs);

for (const [mountKey, mountEntry] of Object.entries(config.mount)) {
Expand All @@ -305,7 +305,7 @@ export async function startServer(
// Note: exclude() only matches directories, and not files. However, the cost
// of false positives here is minor, so do this as a quick check to possibly
// skip scanning into entire folder trees.
.exclude((_, dirPath) => excludePrivate.test(dirPath) || foundExcludeMatch(dirPath))
.exclude((_, dirPath) => foundExcludeMatch(dirPath))
.crawl(mountKey)
.withPromise()) as string[];

Expand Down
17 changes: 17 additions & 0 deletions test/build/import-dot-folder/import-dot-folder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path');
const {setupBuildTest, readFiles, stripWS} = require('../../test-utils');

const cwd = path.join(__dirname, 'build');
let files = {};

describe('import-dot-folder', () => {
beforeAll(() => {
setupBuildTest(__dirname);
files = readFiles(cwd);
console.log(files);
});

it('importing files in a dot folder works', () => {
expect(files['/_dist_/.dot/file.js']).toBeTruthy();
});
});
13 changes: 13 additions & 0 deletions test/build/import-dot-folder/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"private": true,
"version": "1.0.1",
"name": "@snowpack/test-import-dot-folder",
"description": "A test to make sure importing folders with dots in them works",
"scripts": {
"start": "snowpack dev",
"testbuild": "snowpack build"
},
"devDependencies": {
"snowpack": "^3.0.0"
}
}
5 changes: 5 additions & 0 deletions test/build/import-dot-folder/snowpack.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"mount": {
"./src": "/_dist_"
}
}
1 change: 1 addition & 0 deletions test/build/import-dot-folder/src/.dot/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 'b';
3 changes: 3 additions & 0 deletions test/build/import-dot-folder/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {a} from './.dot/file.js';

console.log('i got', a);
1 change: 1 addition & 0 deletions test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function readFiles(directory, {ignore} = {}) {
const allFiles = glob.sync(`**/*.{${UTF8_FRIENDLY_EXTS.join(',')}}`, {
cwd: directory,
nodir: true,
dot: true,
ignore,
});

Expand Down

0 comments on commit 3284e76

Please sign in to comment.