From 637575c7a7a8e6889df639fa4d0f0255649823dd Mon Sep 17 00:00:00 2001 From: Alexey Lavinsky Date: Mon, 28 Sep 2020 20:03:31 +0300 Subject: [PATCH] feat: add support "lineNumbers" option (#278) --- README.md | 10 ++++++ src/index.js | 4 +++ test/__snapshots__/loader.test.js.snap | 21 +++++++++++++ test/fixtures/prefix.styl | 11 +++++++ test/helpers/getCodeFromStylus.js | 12 +++++--- test/loader.test.js | 42 ++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/prefix.styl diff --git a/README.md b/README.md index 15d2663..3aa17d3 100644 --- a/README.md +++ b/README.md @@ -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. * diff --git a/src/index.js b/src/index.js index 2f9c8dc..5b4fe12 100644 --- a/src/index.js +++ b/src/index.js @@ -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); } diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 4f44f66..b5e9e4c 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -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); @@ -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: css 1`] = ` "body { font: 12px Helvetica, Arial, sans-serif; diff --git a/test/fixtures/prefix.styl b/test/fixtures/prefix.styl new file mode 100644 index 0000000..e980488 --- /dev/null +++ b/test/fixtures/prefix.styl @@ -0,0 +1,11 @@ +.foo { + border: 5px; +} + +.baz { + border: 15px; +} + +.bar { + border: 25px; +} diff --git a/test/helpers/getCodeFromStylus.js b/test/helpers/getCodeFromStylus.js index 4e5ec06..c2a8837 100644 --- a/test/helpers/getCodeFromStylus.js +++ b/test/helpers/getCodeFromStylus.js @@ -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, @@ -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()); } diff --git a/test/loader.test.js b/test/loader.test.js index 273c6cf..7b92869 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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, {