Skip to content

Commit d09693e

Browse files
feat: added cache serializer for webpack@5 (#581)
1 parent 84933cc commit d09693e

File tree

7 files changed

+204
-50
lines changed

7 files changed

+204
-50
lines changed

src/CssDependency.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import webpack from 'webpack';
22

3-
export default class CssDependency extends webpack.Dependency {
3+
class CssDependency extends webpack.Dependency {
44
constructor(
55
{ identifier, content, media, sourceMap },
66
context,
@@ -19,4 +19,53 @@ export default class CssDependency extends webpack.Dependency {
1919
getResourceIdentifier() {
2020
return `css-module-${this.identifier}-${this.identifierIndex}`;
2121
}
22+
23+
serialize(context) {
24+
const { write } = context;
25+
26+
write(this.identifier);
27+
write(this.content);
28+
write(this.media);
29+
write(this.sourceMap);
30+
write(this.context);
31+
write(this.identifierIndex);
32+
33+
super.serialize(context);
34+
}
35+
36+
deserialize(context) {
37+
super.deserialize(context);
38+
}
2239
}
40+
41+
if (webpack.util && webpack.util.serialization) {
42+
webpack.util.serialization.register(
43+
CssDependency,
44+
'mini-css-extract-plugin/src/CssDependency',
45+
null,
46+
{
47+
serialize(instance, context) {
48+
instance.serialize(context);
49+
},
50+
deserialize(context) {
51+
const { read } = context;
52+
const dep = new CssDependency(
53+
{
54+
identifier: read(),
55+
content: read(),
56+
media: read(),
57+
sourceMap: read(),
58+
},
59+
read(),
60+
read()
61+
);
62+
63+
dep.deserialize(context);
64+
65+
return dep;
66+
},
67+
}
68+
);
69+
}
70+
71+
export default CssDependency;

src/CssModule.js

+65-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ import webpack from 'webpack';
22

33
import { MODULE_TYPE } from './utils';
44

5-
export default class CssModule extends webpack.Module {
6-
constructor(dependency) {
7-
super(MODULE_TYPE, dependency.context);
5+
class CssModule extends webpack.Module {
6+
constructor({
7+
context,
8+
identifier,
9+
identifierIndex,
10+
content,
11+
media,
12+
sourceMap,
13+
}) {
14+
super(MODULE_TYPE, context);
815

916
this.id = '';
10-
this._identifier = dependency.identifier;
11-
this._identifierIndex = dependency.identifierIndex;
12-
this.content = dependency.content;
13-
this.media = dependency.media;
14-
this.sourceMap = dependency.sourceMap;
17+
this._context = context;
18+
this._identifier = identifier;
19+
this._identifierIndex = identifierIndex;
20+
this.content = content;
21+
this.media = media;
22+
this.sourceMap = sourceMap;
1523
}
1624

1725
// no source() so webpack doesn't do add stuff to the bundle
@@ -55,6 +63,7 @@ export default class CssModule extends webpack.Module {
5563
build(options, compilation, resolver, fileSystem, callback) {
5664
this.buildInfo = {};
5765
this.buildMeta = {};
66+
5867
callback();
5968
}
6069

@@ -65,4 +74,52 @@ export default class CssModule extends webpack.Module {
6574
hash.update(this.media || '');
6675
hash.update(this.sourceMap ? JSON.stringify(this.sourceMap) : '');
6776
}
77+
78+
serialize(context) {
79+
const { write } = context;
80+
81+
write(this._context);
82+
write(this._identifier);
83+
write(this._identifierIndex);
84+
write(this.content);
85+
write(this.media);
86+
write(this.sourceMap);
87+
88+
super.serialize(context);
89+
}
90+
91+
deserialize(context) {
92+
super.deserialize(context);
93+
}
6894
}
95+
96+
if (webpack.util && webpack.util.serialization) {
97+
webpack.util.serialization.register(
98+
CssModule,
99+
'mini-css-extract-plugin/src/CssModule',
100+
null,
101+
{
102+
serialize(instance, context) {
103+
instance.serialize(context);
104+
},
105+
deserialize(context) {
106+
const { read } = context;
107+
108+
const dep = new CssModule({
109+
context: read(),
110+
identifier: read(),
111+
identifierIndex: read(),
112+
content: read(),
113+
media: read(),
114+
sourceMap: read(),
115+
});
116+
117+
dep.deserialize(context);
118+
119+
return dep;
120+
},
121+
}
122+
);
123+
}
124+
125+
export default CssModule;

test/TestCache.test.js

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
5+
import path from 'path';
6+
7+
import webpack from 'webpack';
8+
import del from 'del';
9+
10+
const fileSystemCacheDirectory = path.resolve(
11+
__dirname,
12+
'./outputs/cache/type-filesystem'
13+
);
14+
15+
del.sync(fileSystemCacheDirectory);
16+
17+
describe('TestCache', () => {
18+
it('should work', async () => {
19+
if (webpack.version[0] !== '4') {
20+
const casesDirectory = path.resolve(__dirname, 'cases');
21+
const directoryForCase = path.resolve(casesDirectory, 'simple');
22+
// eslint-disable-next-line import/no-dynamic-require, global-require
23+
const webpackConfig = require(path.resolve(
24+
directoryForCase,
25+
'webpack.config.js'
26+
));
27+
28+
const compiler1 = webpack({
29+
...webpackConfig,
30+
mode: 'development',
31+
context: directoryForCase,
32+
cache: {
33+
type: 'filesystem',
34+
cacheDirectory: fileSystemCacheDirectory,
35+
idleTimeout: 0,
36+
idleTimeoutForInitialStore: 0,
37+
},
38+
});
39+
40+
await new Promise((resolve, reject) => {
41+
compiler1.run((error, stats) => {
42+
if (error) {
43+
reject(error);
44+
45+
return;
46+
}
47+
48+
expect(stats.compilation.warnings).toHaveLength(0);
49+
expect(stats.compilation.errors).toHaveLength(0);
50+
51+
compiler1.close(() => {
52+
resolve();
53+
});
54+
});
55+
});
56+
57+
const compiler2 = webpack({
58+
...webpackConfig,
59+
mode: 'development',
60+
context: directoryForCase,
61+
cache: {
62+
type: 'filesystem',
63+
cacheDirectory: fileSystemCacheDirectory,
64+
idleTimeout: 0,
65+
idleTimeoutForInitialStore: 0,
66+
},
67+
});
68+
69+
await new Promise((resolve, reject) => {
70+
compiler2.run((error, stats) => {
71+
if (error) {
72+
reject(error);
73+
74+
return;
75+
}
76+
77+
expect(stats.compilation.warnings).toHaveLength(0);
78+
expect(stats.compilation.errors).toHaveLength(0);
79+
80+
compiler2.close(() => {
81+
resolve();
82+
});
83+
});
84+
});
85+
} else {
86+
expect(true).toBe(true);
87+
}
88+
});
89+
});

test/cases/cache/expected/main.css

-4
This file was deleted.

test/cases/cache/index.js

-1
This file was deleted.

test/cases/cache/style.css

-3
This file was deleted.

test/cases/cache/webpack.config.js

-33
This file was deleted.

0 commit comments

Comments
 (0)