|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, Parse, LLC |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the license found in the LICENSE file in |
| 6 | + * the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +const webpack = require('webpack'); |
| 10 | +const path = require('path'); |
| 11 | +const fs = require('fs'); |
| 12 | +const os = require('os'); |
| 13 | + |
| 14 | +function runWebpack(config) { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + webpack(config, (err, stats) => { |
| 17 | + if (err) { |
| 18 | + return reject(err); |
| 19 | + } |
| 20 | + if (stats.hasErrors()) { |
| 21 | + return reject(new Error(stats.toJson().errors.map(e => e.message).join('\n'))); |
| 22 | + } |
| 23 | + resolve(stats); |
| 24 | + }); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +describe('CSS modules', () => { |
| 29 | + let tmpDir; |
| 30 | + |
| 31 | + beforeAll(() => { |
| 32 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'css-modules-test-')); |
| 33 | + |
| 34 | + fs.writeFileSync( |
| 35 | + path.join(tmpDir, 'test.scss'), |
| 36 | + '.test_underscore_class { color: red; }\n.testCamelCase { color: blue; }\n' |
| 37 | + ); |
| 38 | + |
| 39 | + fs.writeFileSync( |
| 40 | + path.join(tmpDir, 'entry.js'), |
| 41 | + 'const styles = require("./test.scss");\n' |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + afterAll(() => { |
| 46 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('preserves underscore class names in exports', async () => { |
| 50 | + const baseConfig = require('../../../webpack/base.config.js'); |
| 51 | + const scssRule = baseConfig.module.rules.find(r => r.test.toString().includes('scss')); |
| 52 | + |
| 53 | + await runWebpack({ |
| 54 | + mode: 'production', |
| 55 | + entry: path.join(tmpDir, 'entry.js'), |
| 56 | + output: { |
| 57 | + path: tmpDir, |
| 58 | + filename: 'bundle.js', |
| 59 | + }, |
| 60 | + module: { |
| 61 | + rules: [{ test: /\.scss$/, use: scssRule.use }], |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const bundle = fs.readFileSync(path.join(tmpDir, 'bundle.js'), 'utf8'); |
| 66 | + |
| 67 | + // Class names with underscores must be preserved as-is in the JS exports, |
| 68 | + // not converted to camelCase (e.g. testUnderscoreClass). |
| 69 | + // This broke when css-loader was bumped from v6 to v7, which changed the |
| 70 | + // default exportLocalsConvention from 'asIs' to 'camelCaseOnly'. |
| 71 | + expect(bundle).toContain('test_underscore_class'); |
| 72 | + expect(bundle).toContain('testCamelCase'); |
| 73 | + }, 30000); |
| 74 | +}); |
0 commit comments