Skip to content

Commit edcc5c8

Browse files
committed
added runtime code to load css
1 parent 9037f0e commit edcc5c8

File tree

8 files changed

+88
-1
lines changed

8 files changed

+88
-1
lines changed

src/index.js

+68
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import webpack from 'webpack';
44
import sources from 'webpack-sources';
55

66
const { RawSource, ConcatSource } = sources;
7+
const { Template } = webpack;
78

89
const NS = path.dirname(fs.realpathSync(__filename));
910

@@ -118,8 +119,75 @@ class MiniCssExtractPlugin {
118119
});
119120
}
120121
});
122+
const { mainTemplate } = compilation;
123+
mainTemplate.hooks.requireEnsure.tap(
124+
'mini-css-extract-plugin',
125+
(source, chunk, hash) => {
126+
const chunkMap = this.getCssChunkObject(chunk);
127+
if (Object.keys(chunkMap).length > 0) {
128+
const chunkMaps = chunk.getChunkMaps();
129+
const linkHrefPath = mainTemplate.getAssetPath(
130+
JSON.stringify(this.options.chunkFilename),
131+
{
132+
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
133+
hashWithLength: length =>
134+
`" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
135+
chunk: {
136+
id: '" + chunkId + "',
137+
hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
138+
hashWithLength(length) {
139+
const shortChunkHashMap = Object.create(null);
140+
for (const chunkId of Object.keys(chunkMaps.hash)) {
141+
if (typeof chunkMaps.hash[chunkId] === 'string') {
142+
shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
143+
}
144+
}
145+
return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
146+
},
147+
name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`,
148+
},
149+
},
150+
);
151+
return Template.asString([
152+
source,
153+
'',
154+
'// mini-css-extract-plugin CSS loading',
155+
`var cssChunks = ${JSON.stringify(chunkMap)};`,
156+
'if(cssChunks[chunkId]) {',
157+
Template.indent([
158+
'promises.push(new Promise(function(resolve, reject) {',
159+
Template.indent([
160+
'var linkTag = document.createElement("link");',
161+
'linkTag.rel = "stylesheet";',
162+
'linkTag.onload = resolve;',
163+
'linkTag.onerror = reject;',
164+
`linkTag.href = ${linkHrefPath};`,
165+
'var head = document.getElementsByTagName("head")[0];',
166+
'head.appendChild(linkTag);',
167+
]),
168+
'}));',
169+
]),
170+
'}',
171+
]);
172+
}
173+
return source;
174+
});
121175
});
122176
}
177+
178+
getCssChunkObject(mainChunk) {
179+
const obj = {};
180+
for (const chunk of mainChunk.getAllAsyncChunks()) {
181+
for (const module of chunk.modulesIterable) {
182+
if (module.type === NS) {
183+
obj[chunk.id] = 1;
184+
break;
185+
}
186+
}
187+
}
188+
return obj;
189+
}
190+
123191
renderContentAsset(modules) {
124192
modules.sort((a, b) => a.index2 - b.index2);
125193
// TODO put @import on top

test/TestCases.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ describe('TestCases', () => {
3535
if (stats.hasErrors()) {
3636
done(new Error(stats.toString({
3737
context: path.resolve(__dirname, '..'),
38+
errorDetails: true,
3839
})));
3940
return;
4041
}
4142
const expectedDirectory = path.resolve(directoryForCase, 'expected');
4243
for (const file of fs.readdirSync(expectedDirectory)) {
4344
const content = fs.readFileSync(path.resolve(expectedDirectory, file), 'utf-8');
4445
const actualContent = fs.readFileSync(path.resolve(outputDirectoryForCase, file), 'utf-8');
45-
expect(actualContent).toBeEqual(content);
46+
expect(actualContent).toEqual(content);
4647
}
4748
done();
4849
});

test/cases/nested/expected/main.css

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body { background: red; }
2+
3+
.component { background: blue; }
4+
5+
body { background: green; }
6+
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.in-async { background: green; }
2+
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.async { background: blue; }
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
body { background: red; }
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
body { background: red; }
2+
3+
body { background: green; }
4+

test/cases/simple/expected/main.css

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

0 commit comments

Comments
 (0)