|
| 1 | +const path = require('path'); |
| 2 | + |
| 3 | +const { webpack, evaluated } = require('./helpers'); |
| 4 | + |
| 5 | +describe('localIdentName option', () => { |
| 6 | + it('should have hash', async () => { |
| 7 | + const config = { |
| 8 | + loader: { |
| 9 | + options: { |
| 10 | + localIdentName: '[name]--[local]--[hash:base64:5]', |
| 11 | + context: path.resolve(__dirname), |
| 12 | + }, |
| 13 | + }, |
| 14 | + }; |
| 15 | + const testId = './modules/localIdentName.css'; |
| 16 | + const stats = await webpack(testId, config); |
| 17 | + const { modules } = stats.toJson(); |
| 18 | + const module = modules.find((m) => m.id === testId); |
| 19 | + const evaluatedModule = evaluated(module.source, modules); |
| 20 | + |
| 21 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 22 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 23 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 24 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should have path naming with context', async () => { |
| 28 | + const config = { |
| 29 | + loader: { |
| 30 | + options: { |
| 31 | + localIdentName: '[path]-[name]--[local]', |
| 32 | + context: path.resolve(__dirname), |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | + const testId = './modules/localIdentName.css'; |
| 37 | + const stats = await webpack(testId, config); |
| 38 | + const { modules } = stats.toJson(); |
| 39 | + const module = modules.find((m) => m.id === testId); |
| 40 | + const evaluatedModule = evaluated(module.source, modules); |
| 41 | + |
| 42 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 43 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 44 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 45 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should use hash prefix', async () => { |
| 49 | + const config = { |
| 50 | + loader: { |
| 51 | + options: { localIdentName: '[local]--[hash]', hashPrefix: 'x' }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + const testId = './modules/localIdentName.css'; |
| 55 | + const stats = await webpack(testId, config); |
| 56 | + const { modules } = stats.toJson(); |
| 57 | + const module = modules.find((m) => m.id === testId); |
| 58 | + const evaluatedModule = evaluated(module.source, modules); |
| 59 | + |
| 60 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 61 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 62 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 63 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should prefixes leading hyphen + digit with underscore', async () => { |
| 67 | + const config = { loader: { options: { localIdentName: '-1[local]' } } }; |
| 68 | + const testId = './modules/localIdentName.css'; |
| 69 | + const stats = await webpack(testId, config); |
| 70 | + const { modules } = stats.toJson(); |
| 71 | + const module = modules.find((m) => m.id === testId); |
| 72 | + const evaluatedModule = evaluated(module.source, modules); |
| 73 | + |
| 74 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 75 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 76 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 77 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should prefixes two leading hyphens with underscore', async () => { |
| 81 | + const config = { loader: { options: { localIdentName: '--[local]' } } }; |
| 82 | + const testId = './modules/localIdentName.css'; |
| 83 | + const stats = await webpack(testId, config); |
| 84 | + const { modules } = stats.toJson(); |
| 85 | + const module = modules.find((m) => m.id === testId); |
| 86 | + const evaluatedModule = evaluated(module.source, modules); |
| 87 | + |
| 88 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 89 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 90 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 91 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should saves underscore prefix in exported class names', async () => { |
| 95 | + const config = { loader: { options: { localIdentName: '[local]' } } }; |
| 96 | + const testId = './modules/localIdentName.css'; |
| 97 | + const stats = await webpack(testId, config); |
| 98 | + const { modules } = stats.toJson(); |
| 99 | + const module = modules.find((m) => m.id === testId); |
| 100 | + const evaluatedModule = evaluated(module.source, modules); |
| 101 | + |
| 102 | + expect(evaluatedModule).toMatchSnapshot('module (evaluated)'); |
| 103 | + expect(evaluatedModule.locals).toMatchSnapshot('locals'); |
| 104 | + expect(stats.compilation.warnings).toMatchSnapshot('warnings'); |
| 105 | + expect(stats.compilation.errors).toMatchSnapshot('errors'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments