Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
27 changes: 27 additions & 0 deletions packages/core/integration-tests/test/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,31 @@ 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'),
);
await assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js'],
},
]);
});

it('should resolve an absolute glob', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/glob-absolute/packages/child/index.js',
),
);
await assertBundles(b, [
{
name: 'index.js',
assets: ['index.js', '*.js', 'a.js', 'b.js'],
},
]);
});
});
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,5 @@
const rootVars = require('/dir/*.js');

module.expors = function () {
return 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,5 @@
const childVars = require('~/dir/*.js');

module.expors = function () {
return childVars.a + childVars.b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
159 changes: 100 additions & 59 deletions packages/resolvers/glob/src/GlobResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,73 +60,114 @@ 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 '~': {
const insideNodeModules = options.projectRoot.includes('node_modules');
let dir = path.dirname(sourceFile);

specifier = path.resolve(path.dirname(result.filePath), rest);
if (result.invalidateOnFileChange) {
for (let f of result.invalidateOnFileChange) {
invalidateOnFileChange.add(f);
while (
Comment thread
mischnic marked this conversation as resolved.
Outdated
dir !== options.projectRoot &&
path.basename(path.dirname(dir)) !== 'node_modules' &&
(insideNodeModules ||
!(await options.inputFS.exists(path.join(dir, 'package.json'))))
) {
dir = path.dirname(dir);

if (dir === path.dirname(dir)) {
dir = options.projectRoot;
break;
}
}

specifier = path.resolve(dir, specifier.slice(2));
break;
}
if (result.invalidateOnFileCreate) {
invalidateOnFileCreate.push(...result.invalidateOnFileCreate);

// 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);
}
}
} else {
specifier = path.resolve(path.dirname(sourceFile), specifier);
}

let normalized = normalizeSeparators(specifier);
Expand Down