Skip to content

Commit 7714e1f

Browse files
authoredMar 29, 2017
feat(loader): Added support for plain JavaScript async require statement (#69)
1 parent 8616eae commit 7714e1f

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed
 

‎spec/index.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ describe('Loader', function() {
9696
checkResult(loadedString, result);
9797
});
9898

99+
it('should return a plain javascript loadChildren async require statement', function() {
100+
var result = [
101+
'loadChildren: () => new Promise(function (resolve) {',
102+
' require.ensure([], function (require) {',
103+
' resolve(require(\'./path/to/file.module\')[\'FileModule\']);',
104+
' });',
105+
'})'
106+
];
107+
108+
var loadedString = loader.call({
109+
resourcePath: resourcePath.replace('.ts', '.js'),
110+
query: query
111+
}, `loadChildren: '${modulePath}'`);
112+
113+
checkResult(loadedString, result);
114+
});
115+
99116
it('should return a loadChildren sync require statement', function() {
100117
var result = [
101118
'loadChildren: function() {',

‎spec/utils.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ describe('Utils', function() {
5252
getRequireLoader('path', undefined, 'name', true).should.eql(result.join(''));
5353
});
5454

55+
it('should return an asynchronous require loadChildren statement with vanilla javascript', function() {
56+
var result = [
57+
'loadChildren: () => new Promise(function (resolve) {',
58+
' require.ensure([], function (require) {',
59+
' resolve(' + getRequireString(path, name) + ');',
60+
' }, \'name\');',
61+
'})'
62+
];
63+
getRequireLoader('path', 'name', 'name', true, true).should.eql(result.join(''));
64+
});
65+
5566
});
5667

5768
describe('getSystemLoader', function() {

‎src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module.exports = function(source, sourcemap) {
2525
// get the filename path
2626
var resourcePath = this.resourcePath;
2727
var filename = utils.getFilename(resourcePath);
28+
var isJs = path.extname(resourcePath).toLowerCase() === '.js';
2829

2930
var replacedSource = source.replace(loadChildrenRegex, function(match, loadString) {
3031
// check for query string in loadString
@@ -90,7 +91,7 @@ module.exports = function(source, sourcemap) {
9091
} else if (loader === 'system') {
9192
replacement = utils.getSystemLoader(filePath, moduleName, inline);
9293
} else {
93-
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline);
94+
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline, isJs);
9495
}
9596

9697
if (debug) {

‎src/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ module.exports.getSyncLoader = function(filePath, moduleName, inline) {
1717
return inline ? result.join('') : result.join('\n');
1818
};
1919

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

2424
var result = [
2525
'loadChildren: () => new Promise(function (resolve) {',
26-
' (require as any).ensure([], function (require: any) {',
26+
' ' + (isJs ? 'require' : '(require as any)') + '.ensure([], function (' + (isJs ? 'require' : 'require: any') + ') {',
2727
' resolve(' + requireString + ');',
2828
' }' + webpackChunkName + ');',
2929
'})'

0 commit comments

Comments
 (0)