Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions packages/core/integration-tests/test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('glob', function () {
it('should require a glob of files', async function () {
let b = await bundle(path.join(__dirname, '/integration/glob/index.js'));

await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js'],
Expand All @@ -31,7 +31,7 @@ describe('glob', function () {
path.join(__dirname, '/integration/glob-deep/index.js'),
);

await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js', 'c.js', 'z.js'],
Expand All @@ -48,7 +48,7 @@ describe('glob', function () {
path.join(__dirname, '/integration/glob-css/index.js'),
);

await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js'],
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('glob', function () {
path.join(__dirname, '/integration/glob-pipeline/index.js'),
);

await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'bundle-url.js'],
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('glob', function () {
path.join(__dirname, '/integration/glob-async/index.js'),
);

await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: [
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('glob', function () {
let b = await bundle(
path.join(__dirname, '/integration/glob-package/index.js'),
);
await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: ['*.js', '*.js', 'a.js', 'b.js', 'x.js', 'y.js', 'index.js'],
Expand All @@ -226,7 +226,7 @@ describe('glob', function () {
let b = await bundle(
path.join(__dirname, '/integration/glob-package-async/index.js'),
);
await assertBundles(b, [
assertBundles(b, [
{
name: 'index.js',
assets: [
Expand All @@ -248,4 +248,35 @@ describe('glob', function () {
assert.equal(typeof output, 'function');
assert.equal(await output(), 10);
});

it('should resolve a glob with ~', async function () {
let b = await bundle(
path.join(__dirname, '/integration/glob-tilde/packages/child/index.js'),
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js'],
},
]);
let output = await run(b);
assert.equal(output, 3);
});

it('should resolve an absolute glob', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/glob-absolute/packages/child/index.js',
),
);
assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js'],
},
]);
let output = await run(b);
assert.equal(output, 3);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["@parcel/resolver-glob", "..."]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const rootVars = require('/dir/*.js');

module.exports = rootVars.a + rootVars.b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["@parcel/resolver-glob", "..."]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const childVars = require('~/dir/*.js');

module.exports = childVars.a + childVars.b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
148 changes: 90 additions & 58 deletions packages/resolvers/glob/src/GlobResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,73 +60,105 @@ export default (new Resolver({
let invalidateOnFileCreate = [];
let invalidateOnFileChange = new Set();

// if the specifier does not start with /, ~, or . then it's not a path but package-ish - we resolve
// the package first, and then append the rest of the path
if (!/^[/~.]/.test(specifier)) {
// Globs are not paths - so they always use / (see https://github.com/micromatch/micromatch#backslashes)
let splitOn = specifier.indexOf('/');
if (specifier.charAt(0) === '@') {
splitOn = specifier.indexOf('/', splitOn + 1);
switch (specifier[0]) {
// Path specifier
case '.': {
specifier = path.resolve(path.dirname(sourceFile), specifier);
break;
}

// Since we've already asserted earlier that there is a glob present, it shouldn't be
// possible for there to be only a package here without any other path parts (e.g. `import('pkg')`)
invariant(splitOn !== -1);

let pkg = specifier.substring(0, splitOn);
let rest = specifier.substring(splitOn + 1);

// This initialisation code is copied from the DefaultResolver
const resolver = new NodeResolver({
fs: options.inputFS,
projectRoot: options.projectRoot,
packageManager: options.shouldAutoInstall
? options.packageManager
: undefined,
mode: options.mode,
logger,
});

let result;
try {
result = await resolver.resolve({
filename: pkg + '/package.json',
parent: dependency.resolveFrom,
specifierType: 'esm',
env: dependency.env,
sourcePath: dependency.sourcePath,
});
} catch (err) {
if (err instanceof ThrowableDiagnostic) {
// Return instead of throwing so we can provide invalidations.
return {
diagnostics: err.diagnostics,
invalidateOnFileCreate,
invalidateOnFileChange: [...invalidateOnFileChange],
};
} else {
throw err;
}
// Absolute path. Make the glob relative to the project root.
case '/': {
specifier = path.resolve(options.projectRoot, specifier.slice(1));
Comment thread
mischnic marked this conversation as resolved.
break;
}

if (!result || !result.filePath) {
throw errorToThrowableDiagnostic(
`Unable to resolve ${pkg} from ${sourceFile} when resolving specifier ${specifier}`,
dependency,
// Tilde path. Package relative. Resolve relative to nearest node_modules
// directory, the nearest directory with package.json or the project
// root - whichever comes first.
case '~': {
let dir = path.dirname(sourceFile);
let pkgPath = nullthrows(
options.inputFS.findAncestorFile(
['package.json'],
dir,
options.projectRoot,
),
);
specifier = path.resolve(path.dirname(pkgPath), specifier.slice(2));
break;
}

specifier = path.resolve(path.dirname(result.filePath), rest);
if (result.invalidateOnFileChange) {
for (let f of result.invalidateOnFileChange) {
invalidateOnFileChange.add(f);
// Support package-ish specifiers like:
// foo (node_module)
// @foo/bar (scoped node_module)
//
// First we resolve the initial portion using NodeResolver, then we tack
// on the remaining glob.
default: {
// Globs are not paths - so they always use / (see https://github.com/micromatch/micromatch#backslashes)
let splitOn = specifier.indexOf('/');
if (specifier[0] === '@') {
splitOn = specifier.indexOf('/', splitOn + 1);
}

// Since we've already asserted earlier that there is a glob present, it shouldn't be
// possible for there to be only a package here without any other path parts (e.g. `import('pkg')`)
invariant(splitOn !== -1);

let pkg = specifier.substring(0, splitOn);
let rest = specifier.substring(splitOn + 1);

// This initialisation code is copied from the DefaultResolver
const resolver = new NodeResolver({
fs: options.inputFS,
projectRoot: options.projectRoot,
packageManager: options.shouldAutoInstall
? options.packageManager
: undefined,
mode: options.mode,
logger,
});

let result;
try {
result = await resolver.resolve({
filename: pkg + '/package.json',
parent: dependency.resolveFrom,
specifierType: 'esm',
env: dependency.env,
sourcePath: dependency.sourcePath,
});
} catch (err) {
if (err instanceof ThrowableDiagnostic) {
// Return instead of throwing so we can provide invalidations.
return {
diagnostics: err.diagnostics,
invalidateOnFileCreate,
invalidateOnFileChange: [...invalidateOnFileChange],
};
} else {
throw err;
}
}

if (!result || !result.filePath) {
throw errorToThrowableDiagnostic(
`Unable to resolve ${pkg} from ${sourceFile} when resolving specifier ${specifier}`,
dependency,
);
}

specifier = path.resolve(path.dirname(result.filePath), rest);
if (result.invalidateOnFileChange) {
for (let f of result.invalidateOnFileChange) {
invalidateOnFileChange.add(f);
}
}
if (result.invalidateOnFileCreate) {
invalidateOnFileCreate.push(...result.invalidateOnFileCreate);
}
}
if (result.invalidateOnFileCreate) {
invalidateOnFileCreate.push(...result.invalidateOnFileCreate);
}
} else {
specifier = path.resolve(path.dirname(sourceFile), specifier);
}

let normalized = normalizeSeparators(specifier);
Expand Down