Skip to content

Commit c5f0394

Browse files
committed
Make work using webpack ID
1 parent 512eaa1 commit c5f0394

File tree

11 files changed

+53
-28
lines changed

11 files changed

+53
-28
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": ["es2016", "flow"],
3-
"plugins": ["syntax-dynamic-import", ["import-inspector", {
3+
"plugins": ["syntax-dynamic-import", ["babel-plugin-import-inspector", {
44
"serverSideRequirePath": true,
55
"webpackRequireWeakId": true
66
}]]
File renamed without changes.
File renamed without changes.

src/components/c.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function() {
2+
console.log("I am C, I will dynamically require A");
3+
import("./a").then(({ default: a }) => a());
4+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function() {
2-
console.log('I am C');
2+
console.log("I am D");
33
}

src/index.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
// @flow
22
import { inspect } from "import-inspector";
33
import path from "path";
4+
import process from "process";
5+
import ManifestInspector from "../tools/manifest-inspector";
46

5-
const stopInspecting = inspect(metadata => {
6-
console.log("Imported", metadata.serverSideRequirePath);
7-
});
7+
const manifest = new ManifestInspector("build/manifest.json");
88

9-
import("./a").then(({ default: a }) => {
10-
a();
11-
});
9+
// Hard require on component
10+
import a from "./components/a";
11+
a();
1212

13-
import("./b").then(({ default: b }) => {
14-
b();
13+
// Look up bundle for a module without importing it
14+
const bundle = manifest.getBundleForWebpackId(
15+
require.resolveWeak("./components/b")
16+
);
17+
console.log(`Weak resolve for ./components/b, provided by ${bundle}`);
18+
19+
// Look up bundles for dynamic imports
20+
inspect(data => {
21+
const bundle = manifest.getBundleForWebpackId(data.webpackRequireWeakId());
22+
console.log(
23+
`Dynamic require for ${data.importedModulePath}, provided by ${bundle}.`
24+
);
1525
});
1626

17-
import("./path/d").then(({ default: b }) => {
27+
import("./components/b").then(({ default: b }) => {
1828
b();
1929
});
2030

21-
import c from "./c";
22-
c();
23-
24-
import d from "./path/d";
25-
d();
26-
27-
setTimeout(stopInspecting);
31+
import("./components/c").then(({ default: c }) => {
32+
c();
33+
});

src/index.test.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/path/d.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

tools/manifest-inspector.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fs from "fs";
2+
3+
export default class ManifestInspector {
4+
constructor(path) {
5+
const manifest = JSON.parse(fs.readFileSync(path));
6+
this.map = [];
7+
Object.keys(manifest).forEach(bundle => {
8+
manifest[bundle].map(module => {
9+
this.map[module.webpackId] = {
10+
bundle,
11+
name: module.name
12+
};
13+
});
14+
});
15+
}
16+
17+
getBundleForWebpackId(webpackId) {
18+
return this.map[webpackId].bundle;
19+
}
20+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ ManifestPlugin.prototype.apply = function(compiler) {
1111
compilation.chunks.forEach(chunk => {
1212
if (chunk.modules && chunk.files.length) {
1313
const file = chunk.files[0];
14+
1415
manifest[file] = chunk.modules
1516
.filter(module => module.libIdent)
1617
.map(module => ({
1718
name: module.libIdent({
1819
context: this.opts.context || compiler.options.context
1920
}),
20-
index: module.index
21+
index: module.index,
22+
index2: module.index2,
23+
webpackId: module.id // Corresponds to require.resolveWeak / webpackRequireWeakId
2124
}));
2225
}
2326
});

0 commit comments

Comments
 (0)