Skip to content

Commit bbb5808

Browse files
committed
feat(server/pack): 支持入口为数组
1 parent 9d21d95 commit bbb5808

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

src/models/Config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class Config {
5656
}
5757
setExports(files) {
5858
[].concat(files).forEach((file) => {
59-
var name = file.replace(/\.\w+$/g, '');
59+
const entryFile = Array.isArray(file) ? file[file.length - 1] : file
60+
61+
var name = entryFile.replace(/\.\w+$/g, '');
6062
if (name.indexOf('./') == 0) {
6163
name = name.substring(2);
6264
} else if (name[0] == '/') {

src/models/Project.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,46 @@ class Project {
121121
}
122122
fixCss() {
123123
let config = this.config.getConfig(),
124-
entry = config.entry,
124+
entries = config.entry,
125125
cssExtNames = config.entryExtNames.css,
126126
fps = [];
127127

128128
const contextPathRelativeToCwd = sysPath.relative(config.context, this.cwd) || '.'
129129

130-
for (let key in entry) {
131-
let extName = sysPath.extname(entry[key]);
130+
for (let key in entries) {
131+
const entryItem = entries[key],
132+
entry = Array.isArray(entryItem) ? entryItem[entryItem.length - 1] : entryItem,
133+
extName = sysPath.extname(entry);
134+
132135
if (cssExtNames.indexOf(extName) > -1) {
133-
let name = sysPath.basename(entry[key], extName),
134-
ofp = sysPath.join(config.context, entry[key]),
135-
np = entry[key] = './' + sysPath.join(contextPathRelativeToCwd, '/.cache', entry[key] + '.js'),
136-
fp = sysPath.join(config.context, np);
137-
138-
mkdirp.sync(sysPath.dirname(fp));
139-
fs.writeFileSync(fp, 'require("' + sysPath.relative(sysPath.dirname(fp), ofp) + '");', 'utf-8');
140-
fps.push(fp);
136+
let requireFilePath = entries[key] = './' + sysPath.join(contextPathRelativeToCwd, '/.cache', entry + '.js'),
137+
cacheFilePath = sysPath.join(config.context, requireFilePath);
138+
139+
mkdirp.sync(sysPath.dirname(cacheFilePath));
140+
141+
// 将原有entry的css路径写到js中
142+
if(Array.isArray(entryItem)) {
143+
// clear
144+
fs.writeFileSync(cacheFilePath, '', 'utf-8');
145+
146+
entryItem.forEach((cssPath, i) => {
147+
const originCssPath = sysPath.join(config.context, cssPath)
148+
fs.appendFileSync(
149+
cacheFilePath,
150+
'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");',
151+
'utf-8'
152+
);
153+
})
154+
} else {
155+
const originCssPath = sysPath.join(config.context, entry)
156+
fs.writeFileSync(
157+
cacheFilePath,
158+
'require("' + sysPath.relative(sysPath.dirname(cacheFilePath), originCssPath) + '");',
159+
'utf-8'
160+
);
161+
}
162+
163+
fps.push(cacheFilePath);
141164
}
142165
}
143166
config.plugins.push(new ExtractTextPlugin(config.output.filename.replace('[ext]', '.css')));

src/plugins/extTemplatedPathPlugin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ module.exports = {
88
compilation.mainTemplate.plugin("asset-path", function(path, data) {
99
let extName = '[ext]';
1010
if (data.chunk) {
11-
let rawRequest = data.chunk.origins[0].module.rawRequest;
11+
let module = data.chunk.origins[0].module,
12+
rawRequest = module.rawRequest
13+
? module.rawRequest
14+
: module.dependencies[module.dependencies.length - 1].userRequest;
15+
1216
extName = sysPath.extname(rawRequest);
1317

1418
if (entryExtNames.css.indexOf(sysPath.extname(sysPath.basename(rawRequest, '.js'))) > -1) {
@@ -22,6 +26,7 @@ module.exports = {
2226
}
2327
})
2428
}
29+
2530
return path.replace(/\[ext\]/g, extName);
2631
});
2732
});

0 commit comments

Comments
 (0)