Skip to content

Commit

Permalink
test: resolving (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Sep 17, 2020
1 parent d490b3a commit fa53359
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

import stylus from 'stylus';

import { getOptions } from 'loader-utils';
Expand Down Expand Up @@ -93,15 +95,15 @@ export default async function stylusLoader(source) {
// let stylus do its magic
styl.render(async (error, css) => {
if (error) {
this.addDependency(error.filename);
this.addDependency(path.normalize(error.filename));
return callback(error);
}

// eslint-disable-next-line no-underscore-dangle
if (stylusOptions._imports.length) {
// eslint-disable-next-line no-underscore-dangle
for (const importData of stylusOptions._imports) {
this.addDependency(importData.path);
this.addDependency(path.normalize(importData.path));
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/import-binop.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "deep/" + "import-" + "fakenib"

8 changes: 8 additions & 0 deletions test/fixtures/shallow-indent.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body
color: red

.some-class
@import "basic.styl"

.webpack-import
@import "deep/import-fakenib"
2 changes: 2 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getWarnings from './getWarnings';
import normalizeErrors from './normalizeErrors';
import readAsset from './readAsset';
import readsAssets from './readAssets';
import validateDependencies from './validateDependencies';

export {
compile,
Expand All @@ -18,4 +19,5 @@ export {
normalizeErrors,
readAsset,
readsAssets,
validateDependencies,
};
13 changes: 13 additions & 0 deletions test/helpers/validateDependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const illegalSymbol = process.platform === 'win32' ? '/' : '\\';

export default (dependencies) => {
for (const item of Array.from(dependencies)) {
if (item.includes(illegalSymbol)) {
throw new Error(
`The file path "${item}" should not contain "${illegalSymbol}"`
);
}
}

return true;
};
62 changes: 62 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getCompiler,
getErrors,
getWarnings,
validateDependencies,
} from './helpers';

describe('loader', () => {
Expand Down Expand Up @@ -205,6 +206,67 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('should work indented import', async () => {
const testId = './shallow-indent.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const { fileDependencies } = stats.compilation;

validateDependencies(fileDependencies);

const fixturesDir = path.resolve(__dirname, 'fixtures');
const fixtures = [
path.resolve(fixturesDir, 'basic.styl'),
path.resolve(fixturesDir, 'deep', 'import-fakenib.styl'),
path.resolve(fixturesDir, 'node_modules', 'fakenib', 'index.styl'),
path.resolve(fixturesDir, 'shallow-indent.styl'),
];

/*
* Todo for webpack@5
* when ...node_modules/stylus/lib/functions/index.styl breaks fileDependencies
* */

fixtures.forEach((fixture) => {
expect(fileDependencies.has(fixture)).toBe(true);
});

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('should work binop import', async () => {
const testId = './import-binop.styl';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const { fileDependencies } = stats.compilation;

validateDependencies(fileDependencies);

const fixturesDir = path.resolve(__dirname, 'fixtures');
const fixtures = [
path.resolve(fixturesDir, 'deep', 'import-fakenib.styl'),
path.resolve(fixturesDir, 'node_modules', 'fakenib', 'index.styl'),
path.resolve(fixturesDir, 'import-binop.styl'),
];

/*
* Todo for webpack@5
* when ...node_modules/stylus/lib/functions/index.styl breaks fileDependencies
* */

fixtures.forEach((fixture) => {
expect(fileDependencies.has(fixture)).toBe(true);
});

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('in a nested import load module from node_modules', async () => {
const testId = './shallow-fakenib.styl';
const compiler = getCompiler(testId);
Expand Down

0 comments on commit fa53359

Please sign in to comment.