Skip to content

Commit

Permalink
feat(loader): Added support for plain JavaScript async require statem…
Browse files Browse the repository at this point in the history
…ent (#69)
  • Loading branch information
brandonroberts authored Mar 29, 2017
1 parent 8616eae commit 7714e1f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
17 changes: 17 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ describe('Loader', function() {
checkResult(loadedString, result);
});

it('should return a plain javascript loadChildren async require statement', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' require.ensure([], function (require) {',
' resolve(require(\'./path/to/file.module\')[\'FileModule\']);',
' });',
'})'
];

var loadedString = loader.call({
resourcePath: resourcePath.replace('.ts', '.js'),
query: query
}, `loadChildren: '${modulePath}'`);

checkResult(loadedString, result);
});

it('should return a loadChildren sync require statement', function() {
var result = [
'loadChildren: function() {',
Expand Down
11 changes: 11 additions & 0 deletions spec/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ describe('Utils', function() {
getRequireLoader('path', undefined, 'name', true).should.eql(result.join(''));
});

it('should return an asynchronous require loadChildren statement with vanilla javascript', function() {
var result = [
'loadChildren: () => new Promise(function (resolve) {',
' require.ensure([], function (require) {',
' resolve(' + getRequireString(path, name) + ');',
' }, \'name\');',
'})'
];
getRequireLoader('path', 'name', 'name', true, true).should.eql(result.join(''));
});

});

describe('getSystemLoader', function() {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module.exports = function(source, sourcemap) {
// get the filename path
var resourcePath = this.resourcePath;
var filename = utils.getFilename(resourcePath);
var isJs = path.extname(resourcePath).toLowerCase() === '.js';

var replacedSource = source.replace(loadChildrenRegex, function(match, loadString) {
// check for query string in loadString
Expand Down Expand Up @@ -90,7 +91,7 @@ module.exports = function(source, sourcemap) {
} else if (loader === 'system') {
replacement = utils.getSystemLoader(filePath, moduleName, inline);
} else {
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline);
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline, isJs);
}

if (debug) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ module.exports.getSyncLoader = function(filePath, moduleName, inline) {
return inline ? result.join('') : result.join('\n');
};

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

var result = [
'loadChildren: () => new Promise(function (resolve) {',
' (require as any).ensure([], function (require: any) {',
' ' + (isJs ? 'require' : '(require as any)') + '.ensure([], function (' + (isJs ? 'require' : 'require: any') + ') {',
' resolve(' + requireString + ');',
' }' + webpackChunkName + ');',
'})'
Expand Down

0 comments on commit 7714e1f

Please sign in to comment.