Skip to content

Commit

Permalink
feat: add support "lineNumbers" option (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Sep 28, 2020
1 parent d4e87c1 commit 637575c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -145,6 +145,16 @@ module.exports = {
resolveURL: true,
// resolveURL: { nocheck: true },

/**
* Emits comments in the generated CSS indicating the corresponding Stylus line.
*
* @see https://stylus-lang.com/docs/executable.html
*
* @type {boolean}
* @default false
*/
lineNumbers: true,

/**
* Move @import and @charset to the top.
*
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Expand Up @@ -45,6 +45,10 @@ export default async function stylusLoader(source) {
styl.set('hoist atrules', true);
}

if (stylusOptions.lineNumbers) {
styl.set('linenos', true);
}

if (stylusOptions.disableCache) {
styl.set('cache', false);
}
Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -693,6 +693,10 @@ exports[`loader should work "include" option: errors 1`] = `Array []`;

exports[`loader should work "include" option: warnings 1`] = `Array []`;

exports[`loader should work "lineNumbers" option: errors 1`] = `Array []`;

exports[`loader should work "lineNumbers" option: warnings 1`] = `Array []`;

exports[`loader should work "nib": css 1`] = `
"body {
background: -webkit-linear-gradient(top, #fff, #000);
Expand All @@ -713,6 +717,23 @@ exports[`loader should work "nib": errors 1`] = `Array []`;

exports[`loader should work "nib": warnings 1`] = `Array []`;

exports[`loader should work "prefix" option: css 1`] = `
".prefix-foo {
border: 5px;
}
.prefix-baz {
border: 15px;
}
.prefix-bar {
border: 25px;
}
"
`;

exports[`loader should work "prefix" option: errors 1`] = `Array []`;

exports[`loader should work "prefix" option: warnings 1`] = `Array []`;

exports[`loader should work "use" option as Array<string>: css 1`] = `
"body {
font: 12px Helvetica, Arial, sans-serif;
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/prefix.styl
@@ -0,0 +1,11 @@
.foo {
border: 5px;
}

.baz {
border: 15px;
}

.bar {
border: 25px;
}
12 changes: 8 additions & 4 deletions test/helpers/getCodeFromStylus.js
Expand Up @@ -111,10 +111,6 @@ async function getCodeFromStylus(testId, options = {}) {
: `${options.additionalData}\n${data}`;
}

if (typeof stylusOptions.hoistAtrules === 'boolean') {
stylusOptions['hoist atrules'] = stylusOptions.hoistAtrules;
}

const mergedOptions = {
...defaultOptions,
...stylusOptions,
Expand All @@ -125,6 +121,14 @@ async function getCodeFromStylus(testId, options = {}) {

const styl = stylus(data.toString(), mergedOptions);

if (stylusOptions.hoistAtrules) {
styl.set('hoist atrules', true);
}

if (stylusOptions.lineNumbers) {
styl.set('linenos', true);
}

if (mergedOptions.shouldUseWebpackImporter) {
styl.set('Evaluator', evaluator());
}
Expand Down
42 changes: 42 additions & 0 deletions test/loader.test.js
Expand Up @@ -1274,6 +1274,48 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work "prefix" option', async () => {
const testId = './prefix.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
prefix: 'prefix-',
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, {
stylusOptions: {
prefix: 'prefix-',
},
});

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

it('should work "lineNumbers" option', async () => {
const testId = './basic.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
lineNumbers: true,
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, {
stylusOptions: {
lineNumbers: true,
},
});

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

it('should use .json file', async () => {
const testId = './json/index.styl';
const compiler = getCompiler(testId, {
Expand Down

0 comments on commit 637575c

Please sign in to comment.