Skip to content
This repository was archived by the owner on Apr 26, 2020. It is now read-only.

Commit 02499ed

Browse files
committed
add dynamic import support and more ...
1 parent 363a598 commit 02499ed

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

src/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ export function addChunkNameToNode(argPath, chunkName) {
7878
argPath.addComment("leading", validLeadingComment.value)
7979
)
8080
}
81+
82+
export function getAsyncComponentParamter(loaderArguments, name, t) {
83+
const index = loaderArguments.findIndex((property) =>
84+
t.isIdentifier(property.node.key, { name })
85+
)
86+
return [index, index === -1 ? null : loaderArguments[index].node.value.value]
87+
}

src/index.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getMagicCommentChunkName,
55
getImportArgPath,
66
addChunkNameToNode,
7+
getAsyncComponentParamter,
78
} from "./helpers"
89

910
let asyncComponentImportNames = []
@@ -79,32 +80,44 @@ const callVisitor = {
7980

8081
const importVisitor = {
8182
Import(path) {
83+
const t = this.t
8284
const argPath = getImportArgPath(path)
8385
const { node } = argPath
8486
const generatedChunkName = getMagicCommentChunkName(node)
87+
const existingChunkName = existingMagicCommentChunkName(node)
8588

86-
const existingChunkNameFromParams =
87-
(this.parentPath.node.arguments[1] &&
88-
this.parentPath.node.arguments[1].value) ||
89-
null
89+
const loaderArguments = this.parentPath
90+
.get("arguments")[0]
91+
.get("properties")
92+
93+
const [
94+
chunkNameNodeIndex,
95+
existingChunkNameFromParams,
96+
] = getAsyncComponentParamter(loaderArguments, "chunkName", t)
9097

91-
const existingChunkName = existingMagicCommentChunkName(node)
9298
const chunkName = convertChunkName(
9399
existingChunkNameFromParams || existingChunkName || generatedChunkName,
94100
this.prefix
95101
)
96102

97103
addChunkNameToNode(argPath, chunkName)
98104

99-
this.parentPath.node.arguments[1] = this.t.stringLiteral(chunkName)
100-
this.parentPath.node.arguments[1] =
101-
generatedChunkName === "[request]"
102-
? this.t.identifier(node.expressions[0].name)
103-
: this.t.stringLiteral(chunkName)
105+
const objectToAppend = t.objectProperty(
106+
t.identifier("chunkName"),
107+
generatedChunkName === "[request]" // if it was a dynamic route! import("./pages/${foo}")
108+
? t.identifier(node.expressions[0].name) // add variable name as chunkName. chunkName: foo
109+
: t.stringLiteral(chunkName) // if it was static just add import() parameter
110+
)
111+
112+
if (chunkNameNodeIndex !== -1) {
113+
loaderArguments[chunkNameNodeIndex].replaceWith(objectToAppend)
114+
} else {
115+
loaderArguments[0].insertAfter(objectToAppend)
116+
}
104117
},
105118
}
106119

107120
function convertChunkName(chunkName, prefix = "") {
108-
if (chunkName === "[request]") return chunkName
121+
if (chunkName === "[request]") return chunkName // we can't change request it's handled by webpack
109122
return prefix + chunkName.replace("/", "-")
110123
}

0 commit comments

Comments
 (0)