diff --git a/test/__snapshots__/localIdentName-option.test.js.snap b/test/__snapshots__/localIdentName-option.test.js.snap new file mode 100644 index 000000000..591109662 --- /dev/null +++ b/test/__snapshots__/localIdentName-option.test.js.snap @@ -0,0 +1,169 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`localIdentName option should have hash: errors 1`] = `Array []`; + +exports[`localIdentName option should have hash: locals 1`] = ` +Object { + "_test": "localIdentName--_test--3Q--B", + "test": "localIdentName--test--1Os7J", +} +`; + +exports[`localIdentName option should have hash: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + ".localIdentName--test--1Os7J { + background: red; +} + +.localIdentName--_test--3Q--B { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should have hash: warnings 1`] = `Array []`; + +exports[`localIdentName option should have path naming with context: errors 1`] = `Array []`; + +exports[`localIdentName option should have path naming with context: locals 1`] = ` +Object { + "_test": "fixtures-modules--localIdentName--_test", + "test": "fixtures-modules--localIdentName--test", +} +`; + +exports[`localIdentName option should have path naming with context: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + ".fixtures-modules--localIdentName--test { + background: red; +} + +.fixtures-modules--localIdentName--_test { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should have path naming with context: warnings 1`] = `Array []`; + +exports[`localIdentName option should prefixes leading hyphen + digit with underscore: errors 1`] = `Array []`; + +exports[`localIdentName option should prefixes leading hyphen + digit with underscore: locals 1`] = ` +Object { + "_test": "_-1_test", + "test": "_-1test", +} +`; + +exports[`localIdentName option should prefixes leading hyphen + digit with underscore: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + "._-1test { + background: red; +} + +._-1_test { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should prefixes leading hyphen + digit with underscore: warnings 1`] = `Array []`; + +exports[`localIdentName option should prefixes two leading hyphens with underscore: errors 1`] = `Array []`; + +exports[`localIdentName option should prefixes two leading hyphens with underscore: locals 1`] = ` +Object { + "_test": "_--_test", + "test": "_--test", +} +`; + +exports[`localIdentName option should prefixes two leading hyphens with underscore: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + "._--test { + background: red; +} + +._--_test { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should prefixes two leading hyphens with underscore: warnings 1`] = `Array []`; + +exports[`localIdentName option should saves underscore prefix in exported class names: errors 1`] = `Array []`; + +exports[`localIdentName option should saves underscore prefix in exported class names: locals 1`] = ` +Object { + "_test": "_test", + "test": "test", +} +`; + +exports[`localIdentName option should saves underscore prefix in exported class names: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + ".test { + background: red; +} + +._test { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should saves underscore prefix in exported class names: warnings 1`] = `Array []`; + +exports[`localIdentName option should use hash prefix: errors 1`] = `Array []`; + +exports[`localIdentName option should use hash prefix: locals 1`] = ` +Object { + "_test": "_test--d745495d407559ef605c9072243801fd", + "test": "test--307c32aa793aaec9aecded85a9fdd448", +} +`; + +exports[`localIdentName option should use hash prefix: module (evaluated) 1`] = ` +Array [ + Array [ + 1, + ".test--307c32aa793aaec9aecded85a9fdd448 { + background: red; +} + +._test--d745495d407559ef605c9072243801fd { + background: blue; +} +", + "", + ], +] +`; + +exports[`localIdentName option should use hash prefix: warnings 1`] = `Array []`; diff --git a/test/fixtures/modules/localIdentName.css b/test/fixtures/modules/localIdentName.css new file mode 100644 index 000000000..cc5433be3 --- /dev/null +++ b/test/fixtures/modules/localIdentName.css @@ -0,0 +1,7 @@ +:local(.test) { + background: red; +} + +:local(._test) { + background: blue; +} diff --git a/test/localIdentName-option.test.js b/test/localIdentName-option.test.js new file mode 100644 index 000000000..1444b8bf2 --- /dev/null +++ b/test/localIdentName-option.test.js @@ -0,0 +1,107 @@ +const path = require('path'); + +const { webpack, evaluated } = require('./helpers'); + +describe('localIdentName option', () => { + it('should have hash', async () => { + const config = { + loader: { + options: { + localIdentName: '[name]--[local]--[hash:base64:5]', + context: path.resolve(__dirname), + }, + }, + }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); + + it('should have path naming with context', async () => { + const config = { + loader: { + options: { + localIdentName: '[path]-[name]--[local]', + context: path.resolve(__dirname), + }, + }, + }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); + + it('should use hash prefix', async () => { + const config = { + loader: { + options: { localIdentName: '[local]--[hash]', hashPrefix: 'x' }, + }, + }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); + + it('should prefixes leading hyphen + digit with underscore', async () => { + const config = { loader: { options: { localIdentName: '-1[local]' } } }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); + + it('should prefixes two leading hyphens with underscore', async () => { + const config = { loader: { options: { localIdentName: '--[local]' } } }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); + + it('should saves underscore prefix in exported class names', async () => { + const config = { loader: { options: { localIdentName: '[local]' } } }; + const testId = './modules/localIdentName.css'; + const stats = await webpack(testId, config); + const { modules } = stats.toJson(); + const module = modules.find((m) => m.id === testId); + const evaluatedModule = evaluated(module.source, modules); + + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); + expect(evaluatedModule.locals).toMatchSnapshot('locals'); + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); + expect(stats.compilation.errors).toMatchSnapshot('errors'); + }); +}); diff --git a/test/modules-option.test.js b/test/modules-option.test.js index 4a47f93b0..6c39e6791 100644 --- a/test/modules-option.test.js +++ b/test/modules-option.test.js @@ -21,8 +21,6 @@ describe('modules option', () => { const module = modules.find((m) => m.id === testId); const evaluatedModule = evaluated(module.source, modules); - // console.log(module) - expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); expect(evaluatedModule.locals).toMatchSnapshot('locals'); expect(stats.compilation.warnings).toMatchSnapshot('warnings');