Skip to content

Commit df345fc

Browse files
authoredNov 17, 2017
feat(loader): Add support for chunk name to system && import loaders (#93)
1 parent 7ca06b0 commit df345fc

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed
 

‎docs/options.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ replacement
151151

152152
## Lazy Loading Options
153153

154-
### chunkName (require loader only)
154+
### chunkName
155155

156156
Allows you to provide [named chunks](http://webpack.github.io/docs/code-splitting.html#named-chunks) for code splitting.
157157

@@ -163,7 +163,7 @@ original
163163
}
164164
```
165165

166-
replacement
166+
replacement (require loader)
167167
```ts
168168
{
169169
path: 'lazy',
@@ -174,3 +174,19 @@ replacement
174174
})
175175
}
176176
```
177+
178+
replacement (system loader)
179+
```ts
180+
{
181+
path: 'lazy',
182+
loadChildren: () => System.import(/* webpackChunkName: "MyChunk" */ './lazy/lazy.module').then(module => module['LazyModule'])
183+
}
184+
```
185+
186+
replacement (import loader)
187+
```ts
188+
{
189+
path: 'lazy',
190+
loadChildren: () => import(/* webpackChunkName: "MyChunk" */ './lazy/lazy.module').then(module => module['LazyModule'])
191+
}
192+
```

‎spec/index.spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,20 @@ describe('Loader', function() {
173173
checkResult(loadedString, result);
174174
});
175175

176+
it ('should return a loadChildren chunkName System.import statement', function() {
177+
var result = [
178+
'loadChildren: () => System.import(/* webpackChunkName: "name" */ \'./path/to/file.module\')',
179+
' .then(module => module[\'FileModule\'])'
180+
];
181+
182+
var loadedString = loader.call({
183+
resourcePath: resourcePath,
184+
query: '?loader=system'
185+
}, `loadChildren: '${modulePath}?chunkName=name'`);
186+
187+
checkResult(loadedString, result);
188+
});
189+
176190
it ('should return a loadChildren dynamic import statement', function() {
177191
var result = [
178192
'loadChildren: () => import(\'./path/to/file.module\')',
@@ -187,6 +201,20 @@ describe('Loader', function() {
187201
checkResult(loadedString, result);
188202
});
189203

204+
it ('should return a loadChildren chunkName dynamic import statement', function() {
205+
var result = [
206+
'loadChildren: () => import(/* webpackChunkName: "name" */ \'./path/to/file.module\')',
207+
' .then(module => module[\'FileModule\'])'
208+
];
209+
210+
var loadedString = loader.call({
211+
resourcePath: resourcePath,
212+
query: '?loader=import'
213+
}, `loadChildren: '${modulePath}?chunkName=name'`);
214+
215+
checkResult(loadedString, result);
216+
});
217+
190218
it('should return a loadChildren async require statement with default', function() {
191219
var modulePath = './path/to/file.module';
192220

‎spec/utils.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,24 @@ describe('Utils', function() {
148148
getFilename('path/to/module.ngfactory.ts').should.eql('module.ngfactory');
149149
});
150150
});
151+
152+
describe('getChunkName', function() {
153+
var getChunkName = utils.getChunkName;
154+
155+
it('should return the chunkName string for a system loader and provided chunkName', function() {
156+
getChunkName('system', 'name').should.eql('/* webpackChunkName: "name" */ ');
157+
});
158+
159+
it('should return the chunkName string for a import loader and provided chunkName', function() {
160+
getChunkName('import', 'name').should.eql('/* webpackChunkName: "name" */ ');
161+
});
162+
163+
it('should return the chunkName string for a require loader and provided chunkName', function() {
164+
getChunkName('require', 'name').should.eql(', \'name\'');
165+
});
166+
167+
it('should return an empty chunkName string for a loader and an empty chunkName', function() {
168+
getChunkName('require', '').should.eql('');
169+
});
170+
});
151171
});

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ module.exports = function(source, sourcemap) {
8989
if (sync) {
9090
replacement = utils.getSyncLoader(filePath, moduleName, inline);
9191
} else if (loader === 'system') {
92-
replacement = utils.getSystemLoader(filePath, moduleName, inline);
92+
replacement = utils.getSystemLoader(filePath, moduleName, inline, chunkName);
9393
} else if (loader === 'import') {
94-
replacement = utils.getImportLoader(filePath, moduleName, inline);
94+
replacement = utils.getImportLoader(filePath, moduleName, inline, chunkName);
9595
} else {
9696
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline, isJs);
9797
}

‎src/utils.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,30 @@ module.exports.getSyncLoader = function(filePath, moduleName, inline) {
1919

2020
module.exports.getRequireLoader = function(filePath, chunkName, moduleName, inline, isJs) {
2121
var requireString = module.exports.getRequireString(filePath, moduleName);
22-
var webpackChunkName = chunkName ? ', \'' + chunkName + '\'' : '';
2322

2423
var result = [
2524
'loadChildren: () => new Promise(function (resolve) {',
2625
' ' + (isJs ? 'require' : '(require as any)') + '.ensure([], function (' + (isJs ? 'require' : 'require: any') + ') {',
2726
' resolve(' + requireString + ');',
28-
' }' + webpackChunkName + ');',
27+
' }' + module.exports.getChunkName('require', chunkName) + ');',
2928
'})'
3029
];
3130

3231
return inline ? result.join('') : result.join('\n');
3332
};
3433

35-
module.exports.getSystemLoader = function(filePath, moduleName, inline) {
34+
module.exports.getSystemLoader = function(filePath, moduleName, inline, chunkName) {
3635
var result = [
37-
'loadChildren: () => System.import(\'' + filePath + '\')',
36+
'loadChildren: () => System.import(' + module.exports.getChunkName('system', chunkName) + '\'' + filePath + '\')',
3837
' .then(module => module[\'' + moduleName + '\'])'
3938
];
4039

4140
return inline ? result.join('') : result.join('\n');
4241
};
4342

44-
module.exports.getImportLoader = function(filePath, moduleName, inline) {
43+
module.exports.getImportLoader = function(filePath, moduleName, inline, chunkName) {
4544
var result = [
46-
'loadChildren: () => import(\'' + filePath + '\')',
45+
'loadChildren: () => import(' + module.exports.getChunkName('import', chunkName) + '\'' + filePath + '\')',
4746
' .then(module => module[\'' + moduleName + '\'])'
4847
];
4948

@@ -70,3 +69,13 @@ module.exports.normalizeFilePath = function(filePath, relativePathMatch) {
7069

7170
return newPath;
7271
}
72+
73+
module.exports.getChunkName = function (loader, chunkName) {
74+
if (chunkName && (loader === 'import' || loader === 'system')) {
75+
return '/* webpackChunkName: "' + chunkName + '" */ ';
76+
} else if (chunkName && loader == 'require') {
77+
return ', \'' + chunkName + '\'';
78+
}
79+
80+
return '';
81+
}

0 commit comments

Comments
 (0)