Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using packages in imports resolved by the glob resolver #8097

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
43 changes: 43 additions & 0 deletions packages/core/integration-tests/test/glob.js
Original file line number Diff line number Diff line change
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'],
},
]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should run the resulting bundle here as in the other tests to ensure it actually works


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);
});
});
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,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.

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,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.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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