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

Feat: add support "lineNumbers" option #278

Merged
merged 2 commits into from Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -715,6 +715,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 @@ -735,6 +739,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 @@ -124,6 +120,14 @@ async function getCodeFromStylus(testId, options = {}) {

styl.set('filename', pathToFile);

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 @@ -1284,6 +1284,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