Skip to content

Commit

Permalink
test: resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Sep 29, 2020
1 parent fb0501f commit 43f043c
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 108 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ npm-debug.log*
/local
/reports
/node_modules
/test/fixtures/node_modules/like-a-glob*/
/test/fixtures/node_modules/like-a-glob*
/test/fixtures/node_modules/webpack-like-a-glob-package-name*

.DS_Store
Thumbs.db
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"fast-glob": "^3.2.4",
"klona": "^2.0.4",
"loader-utils": "^2.0.0",
"normalize-path": "^3.0.0",
"schema-utils": "^2.7.1"
},
"devDependencies": {
Expand Down
114 changes: 73 additions & 41 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DepsResolver from 'stylus/lib/visitor/deps-resolver';
import { urlToRequest } from 'loader-utils';
import { klona } from 'klona/full';
import fastGlob from 'fast-glob';
import normalizePath from 'normalize-path';

function isProductionLikeMode(loaderContext) {
return loaderContext.mode === 'production' || !loaderContext.mode;
Expand Down Expand Up @@ -40,60 +41,76 @@ function getStylusOptions(loaderContext, loaderOptions) {
return stylusOptions;
}

async function runGlob(patterns, options) {
const paths = await fastGlob(patterns, { absolute: true, ...options });
function getPossibleRequests(loaderContext, filename) {
const request = urlToRequest(
filename,
// eslint-disable-next-line no-undefined
filename.charAt(0) === '/' ? loaderContext.rootContext : undefined
);

return paths.sort().filter((file) => /\.styl$/i.test(file));
return [...new Set([request, filename])];
}

async function resolveFilename(
loaderContext,
webpackFileResolver,
webpackGlobResolver,
fileResolver,
globResolver,
isGlob,
context,
filename
) {
const resolve = isGlob ? webpackGlobResolver : webpackFileResolver;
const possibleRequests = getPossibleRequests(loaderContext, filename);

let globTask;
let result;

if (isGlob) {
[globTask] = fastGlob.generateTasks(filename);
try {
result = await resolveRequests(context, possibleRequests, fileResolver);
} catch (error) {
if (isGlob) {
const [globTask] = fastGlob.generateTasks(filename);

// eslint-disable-next-line no-param-reassign
filename = globTask.base === '.' ? context : globTask.base;
}
if (globTask.base === '.') {
throw new Error(
'Glob resolving without a glob base ("~**/*") is not supported, please specify a glob base ("~package/**/*")'
);
}

const request = urlToRequest(
filename,
// eslint-disable-next-line no-undefined
filename.charAt(0) === '/' ? loaderContext.rootContext : undefined
);
const possibleRequests = [...new Set([request, filename])];
const possibleGlobRequests = getPossibleRequests(
loaderContext,
globTask.base
);

return resolveRequests(context, possibleRequests, resolve)
.then(async (result) => {
if (globTask && result) {
loaderContext.addContextDependency(result);
let globResult;

// TODO improve
const patterns = globTask.patterns.map((item) =>
item.slice(globTask.base.length + 1)
try {
globResult = await resolveRequests(
context,
possibleGlobRequests,
globResolver
);

return runGlob(patterns, { cwd: result });
} catch (globError) {
throw globError;
}

return result;
})
.catch((error) => {
if (globTask) {
return resolveRequests(context, possibleRequests, webpackFileResolver);
}
loaderContext.addContextDependency(globResult);

throw error;
});
const patterns = filename.replace(
new RegExp(`^${globTask.base}`),
normalizePath(globResult)
);

const paths = await fastGlob(patterns, {
absolute: true,
cwd: globResult,
});

return paths.sort().filter((file) => /\.styl$/i.test(file));
}

throw error;
}

return result;
}

function resolveRequests(context, possibleRequests, resolve) {
Expand Down Expand Up @@ -187,8 +204,8 @@ async function getDependencies(
const [globTask] = fastGlob.generateTasks(nodePath);
const context =
globTask.base === '.'
? path.dirname(filename)
: path.join(path.dirname(filename), globTask.base);
? path.dirname(this.filename)
: path.join(path.dirname(this.filename), globTask.base);

loaderContext.addContextDependency(context);
}
Expand Down Expand Up @@ -219,7 +236,7 @@ async function getDependencies(
fileResolver,
globResolver,
isGlob,
path.dirname(filename),
path.dirname(this.filename),
originalNodePath
),
});
Expand Down Expand Up @@ -355,7 +372,13 @@ async function createEvaluator(loaderContext, code, options) {

let webpackResolveError;

if (node.name !== 'url' && nodePath && !URL_RE.test(nodePath)) {
if (
node.name !== 'url' &&
nodePath &&
!URL_RE.test(nodePath) &&
// `imports` is not resolved, let's avoid extra actions
!this.options.imports.includes(nodePath)
) {
const dependencies = resolvedDependencies.get(
path.normalize(node.filename)
);
Expand Down Expand Up @@ -421,8 +444,17 @@ async function createEvaluator(loaderContext, code, options) {
new Error(
`Stylus resolver error: ${error.message}${
webpackResolveError
? `\n\nWebpack resolver error details:\n${webpackResolveError.details}\n\n` +
`Webpack resolver error missing:\n${webpackResolveError.missing}\n\n`
? `\n\nWebpack resolver error: ${webpackResolveError.message}${
webpackResolveError.details
? `\n\nWebpack resolver error details:\n${webpackResolveError.details}`
: ''
}${
webpackResolveError.missing
? `\n\nWebpack resolver error missing:\n${webpackResolveError.missing.join(
'\n'
)}`
: ''
}`
: ''
}`
)
Expand Down
23 changes: 23 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ exports[`loader imports and webpack deps: errors 1`] = `Array []`;

exports[`loader imports and webpack deps: warnings 1`] = `Array []`;

exports[`loader imports files in dir like a glob: css 1`] = `
".like-a-glob {
color: #ff7f50;
}
.entry {
box-sizing: border-box;
}
"
`;

exports[`loader imports files in dir like a glob: errors 1`] = `Array []`;

exports[`loader imports files in dir like a glob: warnings 1`] = `Array []`;

exports[`loader imports files listed in glob **/* with deps: css 1`] = `
".a-deep {
color: #f00;
Expand Down Expand Up @@ -400,6 +414,15 @@ exports[`loader imports the right file based on context: errors 1`] = `Array []`

exports[`loader imports the right file based on context: warnings 1`] = `Array []`;

exports[`loader imports unsupported webpack: errors 1`] = `
Array [
"ModuleError: Module Error (from \`replaced original path\`):
Stylus resolver error: failed to locate @import file ~**/*.styl",
]
`;

exports[`loader imports unsupported webpack: warnings 1`] = `Array []`;

exports[`loader in a nested import load module from node_modules: css 1`] = `
".not-real-nib {
color: #000;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/import-webpack-dir-like-a-glob.styl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@import '~like-a-glob*'
@import '~webpack-like-a-glob-package-name*'

.entry
box-sizing: border-box
1 change: 1 addition & 0 deletions test/fixtures/import-webpack-unsupported.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '~**/*'
9 changes: 8 additions & 1 deletion test/helpers/getCodeFromStylus.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const pathMap = {
'~aliasNested/**/file.styl': 'glob-nested/**/file.styl',
'~globAliasDot/*': 'glob-webpack-2/*',
'glob_package/*': 'node_modules/glob_package/*',
'~like-a-glob*': 'node_modules/like-a-glob*',
'alias/1': path.resolve(fixturesDir, 'alias', '1.styl'),
'~alias/2': path.resolve(fixturesDir, 'alias', '2.styl'),
'globAlias/*': path.resolve(fixturesDir, 'glob-webpack/*'),
Expand Down Expand Up @@ -52,6 +51,14 @@ const pathMap = {
'in-web-modules',
'index.styl'
),
'~webpack-like-a-glob-package-name*': path.resolve(
fixturesDir,
'node_modules',
process.platform === 'win32'
? 'webpack-like-a-glob-package-name'
: 'webpack-like-a-glob-package-name*',
'index.styl'
),
};

function evaluator() {
Expand Down

0 comments on commit 43f043c

Please sign in to comment.