|
4 | 4 | getMagicCommentChunkName,
|
5 | 5 | getImportArgPath,
|
6 | 6 | addChunkNameToNode,
|
| 7 | + getAsyncComponentParamter, |
7 | 8 | } from "./helpers"
|
8 | 9 |
|
9 | 10 | let asyncComponentImportNames = []
|
@@ -79,32 +80,44 @@ const callVisitor = {
|
79 | 80 |
|
80 | 81 | const importVisitor = {
|
81 | 82 | Import(path) {
|
| 83 | + const t = this.t |
82 | 84 | const argPath = getImportArgPath(path)
|
83 | 85 | const { node } = argPath
|
84 | 86 | const generatedChunkName = getMagicCommentChunkName(node)
|
| 87 | + const existingChunkName = existingMagicCommentChunkName(node) |
85 | 88 |
|
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) |
90 | 97 |
|
91 |
| - const existingChunkName = existingMagicCommentChunkName(node) |
92 | 98 | const chunkName = convertChunkName(
|
93 | 99 | existingChunkNameFromParams || existingChunkName || generatedChunkName,
|
94 | 100 | this.prefix
|
95 | 101 | )
|
96 | 102 |
|
97 | 103 | addChunkNameToNode(argPath, chunkName)
|
98 | 104 |
|
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 | + } |
104 | 117 | },
|
105 | 118 | }
|
106 | 119 |
|
107 | 120 | 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 |
109 | 122 | return prefix + chunkName.replace("/", "-")
|
110 | 123 | }
|
0 commit comments