From 1a174c27ad6da5ae117a90425a3719ec5d7a1334 Mon Sep 17 00:00:00 2001 From: Dave Poulter Date: Tue, 3 Nov 2020 14:27:09 +0000 Subject: [PATCH] feat: Add option for custom banners (#14) Useful when working with tools that can differentiate between generated files and regular files. --- index.js | 4 ++-- test/banner-custom/.gitignore | 1 + .../__snapshots__/banner-custom.test.js.snap | 11 +++++++++++ test/banner-custom/banner-custom.test.js | 15 +++++++++++++++ test/banner-custom/index.css | 3 +++ test/banner-custom/index.js | 1 + 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/banner-custom/.gitignore create mode 100644 test/banner-custom/__snapshots__/banner-custom.test.js.snap create mode 100644 test/banner-custom/banner-custom.test.js create mode 100644 test/banner-custom/index.css create mode 100644 test/banner-custom/index.js diff --git a/index.js b/index.js index c1df2d3..0c04807 100644 --- a/index.js +++ b/index.js @@ -76,7 +76,7 @@ module.exports = function(content, ...rest) { const { failed, success } = makeDoneHandlers(this.async(), content, rest); const filename = this.resourcePath; - const { mode = 'emit' } = loaderUtils.getOptions(this) || {}; + const { mode = 'emit', banner = bannerMessage } = loaderUtils.getOptions(this) || {}; if (!validModes.includes(mode)) { return failed(new Error(`Invalid mode option: ${mode}`)); } @@ -96,7 +96,7 @@ module.exports = function(content, ...rest) { } } - const cssModuleDefinition = `${bannerMessage}\n${cssModuleToInterface(cssModuleKeys)}\n${cssModuleExport}`; + const cssModuleDefinition = `${banner}\n${cssModuleToInterface(cssModuleKeys)}\n${cssModuleExport}`; if (mode === 'verify') { read((err, fileContents) => { diff --git a/test/banner-custom/.gitignore b/test/banner-custom/.gitignore new file mode 100644 index 0000000..cc5d7db --- /dev/null +++ b/test/banner-custom/.gitignore @@ -0,0 +1 @@ +index.css.d.ts diff --git a/test/banner-custom/__snapshots__/banner-custom.test.js.snap b/test/banner-custom/__snapshots__/banner-custom.test.js.snap new file mode 100644 index 0000000..65ed1f8 --- /dev/null +++ b/test/banner-custom/__snapshots__/banner-custom.test.js.snap @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Can add custom banners 1`] = ` +"// @generated +interface CssExports { + 'foo': string; +} +export const cssExports: CssExports; +export default cssExports; +" +`; diff --git a/test/banner-custom/banner-custom.test.js b/test/banner-custom/banner-custom.test.js new file mode 100644 index 0000000..3d8ff90 --- /dev/null +++ b/test/banner-custom/banner-custom.test.js @@ -0,0 +1,15 @@ +const fs = require('fs'); +const compiler = require('../compiler.js'); + +test('Can add custom banners', async () => { + await compiler(require.resolve('./index.js'), { + banner: "// @generated", + }); + + const declaration = fs.readFileSync( + require.resolve('./index.css.d.ts'), + 'utf-8' + ); + + expect(declaration).toMatchSnapshot(); +}); diff --git a/test/banner-custom/index.css b/test/banner-custom/index.css new file mode 100644 index 0000000..40167ad --- /dev/null +++ b/test/banner-custom/index.css @@ -0,0 +1,3 @@ +.foo { + display: block; +} diff --git a/test/banner-custom/index.js b/test/banner-custom/index.js new file mode 100644 index 0000000..14a65bf --- /dev/null +++ b/test/banner-custom/index.js @@ -0,0 +1 @@ +import styles from './index.css';