Skip to content

Commit a5962d5

Browse files
author
light.liang
committed
FOR MPX ONLY(WIP)
1 parent ccfac5a commit a5962d5

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

lib/loader.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = function (content, map) {
4848
subPackagesMap,
4949
mainResourceMap,
5050
additionalAssets,
51+
toBeRewriteSrcSet,
5152
} = compilationMpx;
5253

5354
const appRequestpath = mainCompilation._preparedEntrypoints[0].request;
@@ -90,6 +91,30 @@ module.exports = function (content, map) {
9091
projectRoot,
9192
);
9293

94+
function retrieveSubPackRoot(originOutputPath, requestPath) {
95+
let subPackageRoot = '';
96+
// node_modules引用的是同一路径,只有子包引用时,这种在小程序引用不合法但对于npm引用合法,会根据源的打包输出路径来决定是否应该在子包打几份输出
97+
if (processingSubPackages) {
98+
for (let src in subPackagesMap) {
99+
// 分包引用且主包未引用的资源,需打入分包目录中
100+
if (originOutputPath.startsWith(src) && !mainResourceMap[requestPath]) {
101+
subPackageRoot = subPackagesMap[src];
102+
break;
103+
}
104+
}
105+
} else {
106+
mainResourceMap[requestPath] = true;
107+
}
108+
return subPackageRoot;
109+
}
110+
111+
function genImportOutputFileName(subPackageRoot, requestPath) {
112+
const selfResourceName = path.parse(requestPath).name;
113+
const requestHash = hash(requestPath);
114+
const outputFilename = toPosix(path.join(subPackageRoot, 'wxss', selfResourceName + requestHash + typeExtMap.styles));
115+
return outputFilename;
116+
}
117+
93118
if (sourceMap) {
94119
if (map) {
95120
if (typeof map === 'string') {
@@ -154,35 +179,29 @@ module.exports = function (content, map) {
154179
if (loaderUtils.isUrlRequest(url, root)) {
155180
const originalPathWithoutExt = stripExtension(originFilePath);
156181
// Injected in child compilation to memorize origin component/page/app and parent css output location
157-
let originOutputName =
182+
let originOutputName;
183+
184+
// output name which was be rewritten from mpx file should be regenerated
185+
if (toBeRewriteSrcSet.has(originFilePath)) {
186+
const originSubPackageRoot = retrieveSubPackRoot(originFilePath, originFilePath);
187+
originOutputName = genImportOutputFileName(originSubPackageRoot, originFilePath);
188+
} else {
158189
// memorized origin
159-
thisCompilation.$originalOutputName
160-
// page
161-
|| pagesMap[originalPathWithoutExt]
162-
// component
163-
|| componentsMap[originalPathWithoutExt]
164-
// app
165-
|| path.relative(projectRoot, appRequestpath);
190+
originOutputName = thisCompilation.$originalOutputName
191+
// page
192+
|| pagesMap[originalPathWithoutExt]
193+
// component
194+
|| componentsMap[originalPathWithoutExt]
195+
// app
196+
|| path.relative(projectRoot, appRequestpath);
197+
}
198+
166199
const originOutputPath = path.resolve(projectRoot, originOutputName);
167200

168201
// Part of these codes are copied from extract function
169202
const requestPath = path.resolve(path.dirname(originFilePath), url);
170-
const selfResourceName = path.parse(requestPath).name;
171-
let subPackageRoot = '';
172-
// node_modules引用的是同一路径,只有子包引用时,这种在小程序引用不合法但对于npm引用合法,会根据源的打包输出路径来决定是否应该在子包打几份输出
173-
if (compilationMpx.processingSubPackages) {
174-
for (let src in subPackagesMap) {
175-
// 分包引用且主包未引用的资源,需打入分包目录中
176-
if (originOutputPath.startsWith(src) && !mainResourceMap[requestPath]) {
177-
subPackageRoot = subPackagesMap[src];
178-
break;
179-
}
180-
}
181-
} else {
182-
mainResourceMap[requestPath] = true;
183-
}
184-
const requestHash = hash(requestPath);
185-
const outputFilename = toPosix(path.join(subPackageRoot, 'wxss', selfResourceName + requestHash + typeExtMap.styles));
203+
const outputSubPackageRoot = retrieveSubPackRoot(originOutputPath, requestPath);
204+
const outputFilename = genImportOutputFileName(outputSubPackageRoot, requestPath);
186205

187206
const parentOutputName = thisCompilation.$parentOutputName || originOutputName;
188207

@@ -207,7 +226,7 @@ module.exports = function (content, map) {
207226
new NodeTemplatePlugin(outputOptions),
208227
new LibraryTemplatePlugin(null, 'commonjs2'),
209228
new NodeTargetPlugin(),
210-
new SingleEntryPlugin(this.context, childRequest, childFilename),
229+
new SingleEntryPlugin(loaderContext.context, childRequest, childFilename),
211230
new LimitChunkCountPlugin({ maxChunks: 1 })
212231
]);
213232

@@ -247,7 +266,7 @@ module.exports = function (content, map) {
247266
}
248267

249268
try {
250-
let text = this.exec(source, childRequest);
269+
let text = loaderContext.exec(source, childRequest);
251270
if (Array.isArray(text)) {
252271
text = text.map((item) => {
253272
return item[1];

0 commit comments

Comments
 (0)