Skip to content

Commit

Permalink
fix: error where not all files were watched
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Mar 20, 2017
1 parent cd1734d commit 53c90fc
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/processResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ function processResult(loaderContext, resultPromise) {
const { callback } = loaderContext;

resultPromise
.then(({ css, map }) => {
.then(({ css, map, imports }) => {
imports.forEach(loaderContext.addDependency, loaderContext);
return {
// Removing the sourceMappingURL comment.
// See removeSourceMappingUrl.js for the reasoning behind this.
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/less/error-import-file-with-error.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "./error-syntax";
1 change: 1 addition & 0 deletions test/helpers/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ignore = [
'error-import-not-existing',
'error-mixed-resolvers',
'error-syntax',
'error-import-file-with-error',
];
const lessReplacements = [
[/~some\//g, '../node_modules/some/'],
Expand Down
106 changes: 106 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ async function compileAndCompare(fixture, { lessLoaderOptions, lessLoaderContext
expect(actualCss).toBe(expectedCss);
}

function lessFixturePath(fixture) {
return path.resolve(__dirname, 'fixtures', 'less', fixture);
}

test('should compile simple less without errors', async () => {
await compileAndCompare('basic');
});
Expand All @@ -28,10 +32,50 @@ test('should resolve all imports', async () => {
await compileAndCompare('import');
});

test('should add all resolved imports as dependencies', async () => {
const dependencies = [];

await compileAndCompare('import', {
lessLoaderContext: {
addDependency(dep) {
if (dependencies.indexOf(dep) === -1) {
dependencies.push(dep);
}
},
},
});

expect(dependencies.sort()).toEqual([
lessFixturePath('import.less'),
lessFixturePath('css.css'),
lessFixturePath('basic.less'),
].sort());
});

test('should resolve all imports from node_modules using webpack\'s resolver', async () => {
await compileAndCompare('import-webpack');
});

test('should add all resolved imports as dependencies, including node_modules', async () => {
const dependencies = [];

await compileAndCompare('import-webpack', {
lessLoaderContext: {
addDependency(dep) {
if (dependencies.indexOf(dep) === -1) {
dependencies.push(dep);
}
},
},
});

expect(dependencies.sort()).toEqual([
lessFixturePath('import-webpack.less'),
lessFixturePath('../node_modules/some/module.less'),
lessFixturePath('../node_modules/some/css.css'),
].sort());
});

test('should resolve aliases as configured', async () => {
await compileAndCompare('import-webpack-alias', {
resolveAlias: {
Expand All @@ -40,12 +84,54 @@ test('should resolve aliases as configured', async () => {
});
});

test('should add all resolved imports as dependencies, including aliased ones', async () => {
const dependencies = [];

await compileAndCompare('import-webpack-alias', {
resolveAlias: {
'aliased-some': 'some',
},
lessLoaderContext: {
addDependency(dep) {
if (dependencies.indexOf(dep) === -1) {
dependencies.push(dep);
}
},
},
});

expect(dependencies.sort()).toEqual([
lessFixturePath('import-webpack-alias.less'),
lessFixturePath('../node_modules/some/module.less'),
].sort());
});

test('should resolve all imports from the given paths using Less\' resolver', async () => {
await compileAndCompare('import-paths', {
lessLoaderOptions: { paths: [__dirname, nodeModulesPath] },
});
});

test('should add all resolved imports as dependencies, including those from the Less resolver', async () => {
const dependencies = [];

await compileAndCompare('import-paths', {
lessLoaderOptions: { paths: [__dirname, nodeModulesPath] },
lessLoaderContext: {
addDependency(dep) {
if (dependencies.indexOf(dep) === -1) {
dependencies.push(dep);
}
},
},
});

expect(dependencies.sort()).toEqual([
lessFixturePath('import-paths.less'),
lessFixturePath('../node_modules/some/module.less'),
].sort());
});

test('should allow to disable webpack\'s resolver by passing an empty paths array', async () => {
const err = await compile('import-webpack', moduleRules.basic({ paths: [] }))
.catch(e => e);
Expand Down Expand Up @@ -140,3 +226,23 @@ test('should provide a useful error message if there was a syntax error', async
expect(err).toBeInstanceOf(Error);
compareErrorMessage(err.message);
});

test('should add a file with an error as dependency so that the watcher is triggered when the error is fixed', async () => {
const dependencies = [];
const lessLoaderContext = {
addDependency(dep) {
if (dependencies.indexOf(dep) === -1) {
dependencies.push(dep);
}
},
};
const rules = moduleRules.basic({}, lessLoaderContext);

await compile('error-import-file-with-error', rules)
.catch(e => e);

expect(dependencies.sort()).toEqual([
lessFixturePath('error-import-file-with-error.less'),
lessFixturePath('error-syntax.less'),
].sort());
});

0 comments on commit 53c90fc

Please sign in to comment.