Skip to content

Commit e3f8ee4

Browse files
refactor: remove export code (#439)
BREAKING CHANGE: loader output only CSS, so you need to use `css-loader`/`file-loader`/`raw-loader` to inject code inside bundle
1 parent 5eec4dd commit e3f8ee4

16 files changed

+145
-47
lines changed

src/index.js

-14
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,6 @@ export default async function loader(content, sourceMap, meta = {}) {
225225

226226
const newMeta = { ...meta, ast, messages };
227227

228-
if (this.loaderIndex === 0) {
229-
/**
230-
* @memberof loader
231-
* @callback callback
232-
*
233-
* @param {Object} null Error
234-
* @param {String} css Result (JS Module)
235-
* @param {Object} map Source Map
236-
*/
237-
callback(null, `module.exports = ${JSON.stringify(css)}`, map);
238-
239-
return;
240-
}
241-
242228
/**
243229
* @memberof loader
244230
* @callback callback

test/__snapshots__/loader.test.js.snap

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ SyntaxError
1616

1717
exports[`loader should emit Syntax Error: warnings 1`] = `Array []`;
1818

19-
exports[`loader should emit warning: css 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
19+
exports[`loader should emit warning: css 1`] = `
20+
"a { color: black }
21+
"
22+
`;
2023

2124
exports[`loader should emit warning: errors 1`] = `Array []`;
2225

@@ -29,7 +32,10 @@ Warning
2932
]
3033
`;
3134
32-
exports[`loader should work: css 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
35+
exports[`loader should work: css 1`] = `
36+
"a { color: black }
37+
"
38+
`;
3339
3440
exports[`loader should work: errors 1`] = `Array []`;
3541

test/helpers/getCodeFromBundle.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
import webpack from 'webpack';
2-
31
import normalizeMap from './normalizeMap';
42

5-
const isWebpack5 = webpack.version[0] === '5';
6-
73
export default (id, stats) => {
84
const { modules } = stats.compilation;
95
const module = modules.find((m) => m.id.endsWith(id));
106
const { _source } = module;
117

12-
return {
13-
// eslint-disable-next-line no-underscore-dangle
14-
css: _source._value || _source._valueAsString,
15-
map: isWebpack5
16-
? // eslint-disable-next-line no-underscore-dangle
17-
normalizeMap(_source._sourceMapAsObject)
18-
: // eslint-disable-next-line no-underscore-dangle
19-
normalizeMap(_source._sourceMap),
20-
};
8+
// eslint-disable-next-line no-underscore-dangle
9+
const code = (_source._value || _source._valueAsString).replace(
10+
'module.exports = ',
11+
''
12+
);
13+
14+
let result;
15+
16+
try {
17+
result = JSON.parse(code);
18+
} catch (error) {
19+
result = { css: code };
20+
}
21+
22+
const { css, map } = result;
23+
24+
return { css, map: normalizeMap(map) };
2125
};

test/helpers/getCompiler.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export default (fixture, loaderOptions = {}, config = {}) => {
2020
{
2121
test: /\.css$/i,
2222
use: [
23+
{
24+
loader: require.resolve('./testLoader'),
25+
options: {},
26+
},
2327
{
2428
loader: path.resolve(__dirname, '../../src'),
2529
options: loaderOptions || {},

test/helpers/testLoader.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function testLoader(content, sourceMap) {
2+
const result = { css: content };
3+
4+
if (sourceMap) {
5+
result.map = sourceMap;
6+
}
7+
8+
return `module.exports = ${JSON.stringify(result)}`;
9+
}
10+
11+
module.exports = testLoader;

test/options/__snapshots__/config.test.js.snap

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Config Options should work Config - {Object}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
3+
exports[`Config Options should work Config - {Object}: css 1`] = `
4+
"a { color: rgba(255, 0, 0, 1.0) }
5+
"
6+
`;
47

58
exports[`Config Options should work Config - {Object}: errors 1`] = `Array []`;
69

710
exports[`Config Options should work Config - {Object}: warnings 1`] = `Array []`;
811

9-
exports[`Config Options should work Config - Context - {Object} - with ident: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
12+
exports[`Config Options should work Config - Context - {Object} - with ident: css 1`] = `
13+
"a { color: rgba(255, 0, 0, 1.0) }
14+
"
15+
`;
1016

1117
exports[`Config Options should work Config - Context - {Object} - with ident: errors 1`] = `Array []`;
1218

1319
exports[`Config Options should work Config - Context - {Object} - with ident: warnings 1`] = `Array []`;
1420

15-
exports[`Config Options should work Config - Context - {Object}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
21+
exports[`Config Options should work Config - Context - {Object}: css 1`] = `
22+
"a { color: rgba(255, 0, 0, 1.0) }
23+
"
24+
`;
1625

1726
exports[`Config Options should work Config - Context - {Object}: errors 1`] = `Array []`;
1827

1928
exports[`Config Options should work Config - Context - {Object}: warnings 1`] = `Array []`;
2029

21-
exports[`Config Options should work Config - Path - {String}: css 1`] = `"module.exports = \\"a { color: black }\\\\n\\""`;
30+
exports[`Config Options should work Config - Path - {String}: css 1`] = `
31+
"a { color: black }
32+
"
33+
`;
2234

2335
exports[`Config Options should work Config - Path - {String}: errors 1`] = `Array []`;
2436

test/options/__snapshots__/exec.test.js.snap

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Exec should work Exec - {Boolean}: css 1`] = `"module.exports = \\"a {\\\\n color: green\\\\n}\\""`;
3+
exports[`Options Exec should work Exec - {Boolean}: css 1`] = `
4+
"a {
5+
color: green
6+
}"
7+
`;
48

59
exports[`Options Exec should work Exec - {Boolean}: errors 1`] = `Array []`;
610

711
exports[`Options Exec should work Exec - {Boolean}: warnings 1`] = `Array []`;
812

913
exports[`Options Exec should work JSS - {String}: css 1`] = `
10-
"module.exports = { a: { color: 'yellow' } }
14+
"{ a: { color: 'yellow' } }
1115
"
1216
`;
1317

test/options/__snapshots__/parser.test.js.snap

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Parser should work Parser - {Object}: css 1`] = `"module.exports = \\"a {\\\\n color: black\\\\n}\\\\n\\""`;
3+
exports[`Options Parser should work Parser - {Object}: css 1`] = `
4+
"a {
5+
color: black
6+
}
7+
"
8+
`;
49

510
exports[`Options Parser should work Parser - {Object}: errors 1`] = `Array []`;
611

712
exports[`Options Parser should work Parser - {Object}: warnings 1`] = `Array []`;
813

9-
exports[`Options Parser should work Parser - {String}: css 1`] = `"module.exports = \\"a {\\\\n color: black\\\\n}\\\\n\\""`;
14+
exports[`Options Parser should work Parser - {String}: css 1`] = `
15+
"a {
16+
color: black
17+
}
18+
"
19+
`;
1020

1121
exports[`Options Parser should work Parser - {String}: errors 1`] = `Array []`;
1222

test/options/__snapshots__/plugins.test.js.snap

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Plugins should work Plugins - {Array}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
3+
exports[`Options Plugins should work Plugins - {Array}: css 1`] = `
4+
"a { color: rgba(255, 0, 0, 1.0) }
5+
"
6+
`;
47

58
exports[`Options Plugins should work Plugins - {Array}: errors 1`] = `Array []`;
69

710
exports[`Options Plugins should work Plugins - {Array}: warnings 1`] = `Array []`;
811

9-
exports[`Options Plugins should work Plugins - {Function} - {Array}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
12+
exports[`Options Plugins should work Plugins - {Function} - {Array}: css 1`] = `
13+
"a { color: rgba(255, 0, 0, 1.0) }
14+
"
15+
`;
1016

1117
exports[`Options Plugins should work Plugins - {Function} - {Array}: errors 1`] = `Array []`;
1218

1319
exports[`Options Plugins should work Plugins - {Function} - {Array}: warnings 1`] = `Array []`;
1420

15-
exports[`Options Plugins should work Plugins - {Function} - {Object}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
21+
exports[`Options Plugins should work Plugins - {Function} - {Object}: css 1`] = `
22+
"a { color: rgba(255, 0, 0, 1.0) }
23+
"
24+
`;
1625

1726
exports[`Options Plugins should work Plugins - {Function} - {Object}: errors 1`] = `Array []`;
1827

1928
exports[`Options Plugins should work Plugins - {Function} - {Object}: warnings 1`] = `Array []`;
2029

21-
exports[`Options Plugins should work Plugins - {Object}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
30+
exports[`Options Plugins should work Plugins - {Object}: css 1`] = `
31+
"a { color: rgba(255, 0, 0, 1.0) }
32+
"
33+
`;
2234

2335
exports[`Options Plugins should work Plugins - {Object}: errors 1`] = `Array []`;
2436

test/options/__snapshots__/sourceMap.test.js.snap

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Sourcemap should work Sourcemap - {Boolean}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\""`;
3+
exports[`Options Sourcemap should work Sourcemap - {Boolean}: css 1`] = `
4+
"a { color: rgba(255, 0, 0, 1.0) }
5+
"
6+
`;
47

58
exports[`Options Sourcemap should work Sourcemap - {Boolean}: errors 1`] = `Array []`;
69

@@ -22,7 +25,11 @@ Object {
2225

2326
exports[`Options Sourcemap should work Sourcemap - {Boolean}: warnings 1`] = `Array []`;
2427

25-
exports[`Options Sourcemap should work Sourcemap - {String}: css 1`] = `"module.exports = \\"a { color: rgba(255, 0, 0, 1.0) }\\\\n\\\\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLDRCQUFhIiwiZmlsZSI6InRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogYmxhY2sgfVxuIl19 */\\""`;
28+
exports[`Options Sourcemap should work Sourcemap - {String}: css 1`] = `
29+
"a { color: rgba(255, 0, 0, 1.0) }
30+
31+
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLDRCQUFhIiwiZmlsZSI6InRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogYmxhY2sgfVxuIl19 */"
32+
`;
2633

2734
exports[`Options Sourcemap should work Sourcemap - {String}: errors 1`] = `Array []`;
2835

test/options/__snapshots__/stringifier.test.js.snap

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Stringifier should work Stringifier - {Object}: css 1`] = `"module.exports = \\"a color: black\\\\n\\""`;
3+
exports[`Options Stringifier should work Stringifier - {Object}: css 1`] = `
4+
"a color: black
5+
"
6+
`;
47

58
exports[`Options Stringifier should work Stringifier - {Object}: errors 1`] = `Array []`;
69

710
exports[`Options Stringifier should work Stringifier - {Object}: warnings 1`] = `Array []`;
811

9-
exports[`Options Stringifier should work Stringifier - {String}: css 1`] = `"module.exports = \\"a color: black\\\\n\\""`;
12+
exports[`Options Stringifier should work Stringifier - {String}: css 1`] = `
13+
"a color: black
14+
"
15+
`;
1016

1117
exports[`Options Stringifier should work Stringifier - {String}: errors 1`] = `Array []`;
1218

test/options/__snapshots__/syntax.test.js.snap

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options Syntax should work Syntax - {Object}: css 1`] = `"module.exports = \\"a\\\\n color: black\\\\n\\""`;
3+
exports[`Options Syntax should work Syntax - {Object}: css 1`] = `
4+
"a
5+
color: black
6+
"
7+
`;
48

59
exports[`Options Syntax should work Syntax - {Object}: errors 1`] = `Array []`;
610

711
exports[`Options Syntax should work Syntax - {Object}: warnings 1`] = `Array []`;
812

9-
exports[`Options Syntax should work Syntax - {String}: css 1`] = `"module.exports = \\"a\\\\n color: black\\\\n\\""`;
13+
exports[`Options Syntax should work Syntax - {String}: css 1`] = `
14+
"a
15+
color: black
16+
"
17+
`;
1018

1119
exports[`Options Syntax should work Syntax - {String}: errors 1`] = `Array []`;
1220

test/options/exec.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('Options Exec', () => {
1919
{
2020
test: /style\.(exec\.js|js)$/i,
2121
use: [
22+
{
23+
loader: require.resolve('../helpers/testLoader'),
24+
options: {},
25+
},
2226
{
2327
loader: path.resolve(__dirname, '../../src'),
2428
options: { exec: true },
@@ -48,6 +52,10 @@ describe('Options Exec', () => {
4852
{
4953
test: /style.\.js$/i,
5054
use: [
55+
{
56+
loader: require.resolve('../helpers/testLoader'),
57+
options: {},
58+
},
5159
{
5260
loader: path.resolve(__dirname, '../../src'),
5361
options: { parser: 'postcss-js' },

test/options/parser.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('Options Parser', () => {
1919
{
2020
test: /\.sss$/i,
2121
use: [
22+
{
23+
loader: require.resolve('../helpers/testLoader'),
24+
options: {},
25+
},
2226
{
2327
loader: path.resolve(__dirname, '../../src'),
2428
options: { parser: 'sugarss' },
@@ -48,6 +52,10 @@ describe('Options Parser', () => {
4852
{
4953
test: /\.sss$/i,
5054
use: [
55+
{
56+
loader: require.resolve('../helpers/testLoader'),
57+
options: {},
58+
},
5159
{
5260
loader: path.resolve(__dirname, '../../src'),
5361
// eslint-disable-next-line global-require

test/options/syntax.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ describe('Options Syntax', () => {
1919
{
2020
test: /\.sss$/i,
2121
use: [
22+
{
23+
loader: require.resolve('../helpers/testLoader'),
24+
options: {},
25+
},
2226
{
2327
loader: path.resolve(__dirname, '../../src'),
2428
options: { syntax: 'sugarss' },
@@ -48,6 +52,10 @@ describe('Options Syntax', () => {
4852
{
4953
test: /\.sss$/i,
5054
use: [
55+
{
56+
loader: require.resolve('../helpers/testLoader'),
57+
options: {},
58+
},
5159
{
5260
loader: path.resolve(__dirname, '../../src'),
5361
options: {

test/validate-options.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ describe('validate options', () => {
7272
{
7373
test: /\.sss$/i,
7474
use: [
75+
{
76+
loader: require.resolve('./helpers/testLoader'),
77+
options: {},
78+
},
7579
{
7680
loader: path.resolve(__dirname, '../src'),
7781
options: { [key]: value },

0 commit comments

Comments
 (0)