Skip to content

Commit

Permalink
Support using packages in imports resolved by the glob resolver (#8097)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcins committed May 23, 2022
1 parent 2dc06ab commit 03fb352
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 18 deletions.
43 changes: 43 additions & 0 deletions packages/core/integration-tests/test/glob.js
Expand Up @@ -203,4 +203,47 @@ describe('glob', function () {
],
});
});

it('should require a glob of files from a package', async function () {
let b = await bundle(
path.join(__dirname, '/integration/glob-package/index.js'),
);
await assertBundles(b, [
{
name: 'index.js',
assets: ['*.js', '*.js', 'a.js', 'b.js', 'x.js', 'y.js', 'index.js'],
},
]);

let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(await output(), 10);
});

it('should require a glob of files from a package async', async function () {
let b = await bundle(
path.join(__dirname, '/integration/glob-package-async/index.js'),
);
await assertBundles(b, [
{
name: 'index.js',
assets: [
'*.js',
'*.js',
'bundle-url.js',
'cacheLoader.js',
'index.js',
'js-loader.js',
],
},
{type: 'js', assets: ['a.js']},
{type: 'js', assets: ['b.js']},
{type: 'js', assets: ['x.js']},
{type: 'js', assets: ['y.js']},
]);

let output = await run(b);
assert.equal(typeof output, 'function');
assert.equal(await output(), 10);
});
});
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["@parcel/resolver-glob", "..."]
}
@@ -0,0 +1,6 @@
const scoped = import('@scope/pkg/foo/*.js');
const unscoped = import('pkg/bar/*.js');

module.exports = async function () {
return await scoped.a() + await scoped.b() + await unscoped.x() + await unscoped.y();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["@parcel/resolver-glob", "..."]
}
@@ -0,0 +1,6 @@
const scoped = require('@scope/pkg/foo/*.js');
const unscoped = require('pkg/bar/*.js');

module.exports = function () {
return scoped.a + scoped.b + unscoped.x + unscoped.y;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
1 change: 1 addition & 0 deletions packages/resolvers/glob/package.json
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@parcel/diagnostic": "2.5.0",
"@parcel/node-resolver-core": "2.5.0",
"@parcel/plugin": "2.5.0",
"@parcel/utils": "2.5.0",
"nullthrows": "^1.1.1"
Expand Down
97 changes: 79 additions & 18 deletions packages/resolvers/glob/src/GlobResolver.js
Expand Up @@ -10,9 +10,31 @@ import {
import path from 'path';
import nullthrows from 'nullthrows';
import ThrowableDiagnostic from '@parcel/diagnostic';
import NodeResolver from '@parcel/node-resolver-core';
import invariant from 'assert';

function errorToThrowableDiagnostic(error, dependency): ThrowableDiagnostic {
return new ThrowableDiagnostic({
diagnostic: {
message: error,
codeFrames: dependency.loc
? [
{
codeHighlights: [
{
start: dependency.loc.start,
end: dependency.loc.end,
},
],
},
]
: undefined,
},
});
}

export default (new Resolver({
async resolve({dependency, options, specifier, pipeline}) {
async resolve({dependency, options, specifier, pipeline, logger}) {
if (!isGlob(specifier)) {
return;
}
Expand All @@ -33,26 +55,65 @@ export default (new Resolver({
}

if (error) {
throw new ThrowableDiagnostic({
diagnostic: {
message: error,
codeFrames: dependency.loc
? [
{
codeHighlights: [
{
start: dependency.loc.start,
end: dependency.loc.end,
},
],
},
]
: undefined,
},
throw errorToThrowableDiagnostic(error, dependency);
}

// 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);
}

// 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,
// Extensions are always required in URL dependencies.
extensions:
dependency.specifierType === 'commonjs' ||
dependency.specifierType === 'esm'
? ['ts', 'tsx', 'js', 'jsx', 'json']
: [],
mainFields: ['source', 'browser', 'module', 'main'],
packageManager: options.shouldAutoInstall
? options.packageManager
: undefined,
logger,
});

const result = await resolver.resolve({
filename: pkg,
specifierType: dependency.specifierType,
parent: dependency.resolveFrom,
env: dependency.env,
sourcePath: dependency.sourcePath,
loc: dependency.loc,
});

if (!result || !result.filePath) {
throw errorToThrowableDiagnostic(
`Unable to resolve ${pkg} from ${sourceFile} when evaluating specifier ${specifier}`,
dependency,
);
} else if (result.diagnostics) {
throw new ThrowableDiagnostic({diagnostic: result.diagnostics});
}

specifier = path.resolve(path.dirname(result.filePath), rest);
} else {
specifier = path.resolve(path.dirname(sourceFile), specifier);
}

specifier = path.resolve(path.dirname(sourceFile), specifier);
let normalized = normalizeSeparators(specifier);
let files = await glob(normalized, options.inputFS, {
onlyFiles: true,
Expand Down

0 comments on commit 03fb352

Please sign in to comment.