Skip to content

Commit 9037f0e

Browse files
committed
more test cases and better naming
1 parent df866df commit 9037f0e

21 files changed

+120
-12
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
2-
/dist
2+
/dist
3+
/test/cases

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ npm-debug.log*
44
.eslintcache
55
/coverage
66
/dist
7-
/js
7+
/test/js
88
/local
99
/reports
1010
/node_modules

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ module.exports = {
33
'/node_modules/',
44
'<rootDir>/dist/',
55
],
6+
watchPathIgnorePatterns: [
7+
'<rootDir>/test/js',
8+
],
69
};

src/index.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CssDependency extends webpack.Dependency {
1818
}
1919

2020
getResourceIdentifier() {
21-
return `cssmodule${this.identifier}`;
21+
return `css-module-${this.identifier}`;
2222
}
2323
}
2424

@@ -45,14 +45,14 @@ class CssModule extends webpack.Module {
4545
}
4646

4747
identifier() {
48-
return `css-module ${this._identifier}`;
48+
return `css ${this._identifier}`;
4949
}
5050

5151
readableIdentifier(requestShortener) {
52-
return `css-modules ${requestShortener.shorten(this._identifier)}`;
52+
return `css ${requestShortener.shorten(this._identifier)}`;
5353
}
5454

55-
build(options, compilation, resolver, fs, callback) {
55+
build(options, compilation, resolver, fileSystem, callback) {
5656
this.buildInfo = {};
5757
this.buildMeta = {};
5858
callback();
@@ -101,7 +101,20 @@ class MiniCssExtractPlugin {
101101
pathOptions: {
102102
chunk,
103103
},
104-
identifier: `extract-text-webpack-plugin.${chunk.id}`,
104+
identifier: `mini-css-extract-plugin.${chunk.id}`,
105+
});
106+
}
107+
});
108+
compilation.chunkTemplate.hooks.renderManifest.tap('mini-css-extract-plugin', (result, { chunk }) => {
109+
const renderedModules = Array.from(chunk.modulesIterable).filter(module => module.type === NS);
110+
if (renderedModules.length > 0) {
111+
result.push({
112+
render: () => this.renderContentAsset(renderedModules),
113+
filenameTemplate: this.options.chunkFilename,
114+
pathOptions: {
115+
chunk,
116+
},
117+
identifier: `mini-css-extract-plugin.${chunk.id}`,
105118
});
106119
}
107120
});
@@ -112,13 +125,14 @@ class MiniCssExtractPlugin {
112125
// TODO put @import on top
113126
const source = new ConcatSource();
114127
for (const m of modules) {
128+
if (m.media) {
129+
source.add(`@media ${m.media} {\n`);
130+
}
115131
source.add(m.content);
116132
source.add('\n');
117133
if (m.media) {
118-
source.prepend(`@media ${m.media} {\n`);
119134
source.add('}\n');
120135
}
121-
return source;
122136
}
123137
return source;
124138
}

test/TestCases.test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import webpack from 'webpack';
44

55
describe('TestCases', () => {
66
const casesDirectory = path.resolve(__dirname, 'cases');
7-
const outputDirectory = path.resolve(__dirname, '../js');
7+
const outputDirectory = path.resolve(__dirname, 'js');
88
for (const directory of fs.readdirSync(casesDirectory)) {
99
if (!/^(\.|_)/.test(directory)) {
1010
// eslint-disable-next-line no-loop-func
@@ -26,9 +26,16 @@ describe('TestCases', () => {
2626
}
2727
done();
2828
// eslint-disable-next-line no-console
29-
console.log(stats.toString());
29+
console.log(stats.toString({
30+
context: path.resolve(__dirname, '..'),
31+
chunks: true,
32+
chunkModules: true,
33+
modules: false,
34+
}));
3035
if (stats.hasErrors()) {
31-
done(new Error(stats.toString()));
36+
done(new Error(stats.toString({
37+
context: path.resolve(__dirname, '..'),
38+
})));
3239
return;
3340
}
3441
const expectedDirectory = path.resolve(directoryForCase, 'expected');

test/cases/nested/a.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: red; }

test/cases/nested/b.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: green; }

test/cases/nested/component.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.component { background: blue; }

test/cases/nested/component.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './component.css';

test/cases/nested/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './a.css';
2+
import './component';
3+
import './b.css';

test/cases/nested/webpack.config.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Self = require('../../../');
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
'css-loader',
12+
],
13+
},
14+
],
15+
},
16+
plugins: [
17+
new Self({
18+
filename: '[name].css',
19+
}),
20+
],
21+
};

test/cases/simple-async/async.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.async { background: blue; }

test/cases/simple-async/async.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './in-async.css';

test/cases/simple-async/in-async.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.in-async { background: green; }

test/cases/simple-async/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './main.css';
2+
3+
import('./async');
4+
5+
import('./async.css');

test/cases/simple-async/main.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: red; }
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Self = require('../../../');
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
'css-loader',
12+
],
13+
},
14+
],
15+
},
16+
plugins: [
17+
new Self({
18+
filename: '[name].css',
19+
}),
20+
],
21+
};

test/cases/simple-multiple/a.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: red; }

test/cases/simple-multiple/b.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body { background: green; }

test/cases/simple-multiple/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './a.css';
2+
import './b.css';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Self = require('../../../');
2+
3+
module.exports = {
4+
entry: './index.js',
5+
module: {
6+
rules: [
7+
{
8+
test: /\.css$/,
9+
use: [
10+
Self.loader,
11+
'css-loader',
12+
],
13+
},
14+
],
15+
},
16+
plugins: [
17+
new Self({
18+
filename: '[name].css',
19+
}),
20+
],
21+
};

0 commit comments

Comments
 (0)