Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add import loader (since webpack v2.1.0-beta.28) #90

Merged
merged 3 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ replacement
}
```

If you prefer to use `System.import`, set the `loader` to `system`
To use `System.import`, set the `loader` to `system`

**NOTE:** Using `system` only works with Webpack 2. Webpack 1 users should use the default.

replacement
```ts
{
path: 'lazy',
loadChildren: () => System.import('./lazy/lazy.module').then(function(module) {
return module['LazyModule'];
})
loadChildren: () => System.import('./lazy/lazy.module').then(module => module['LazyModule'])
}
```

To use `dynamic import`, set the `loader` to `import`

**NOTE:** Using `import` only works with Webpack >= 2.1.0.

replacement
```ts
{
path: 'lazy',
loadChildren: () => import('./lazy/lazy.module').then(module => module['LazyModule'])
}
```

Expand Down
36 changes: 30 additions & 6 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ describe('Loader', function() {
it ('should return a loadChildren System.import statement', function() {
var result = [
'loadChildren: () => System.import(\'./path/to/file.module\')',
' .then(function(module) {',
' return module[\'FileModule\'];',
' })'
' .then(module => module[\'FileModule\'])'
];

var loadedString = loader.call({
Expand All @@ -161,6 +159,20 @@ describe('Loader', function() {
checkResult(loadedString, result);
});

it ('should return a loadChildren dynamic import statement', function() {
var result = [
'loadChildren: () => import(\'./path/to/file.module\')',
' .then(module => module[\'FileModule\'])'
];

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

checkResult(loadedString, result);
});

it('should return a loadChildren async require statement with default', function() {
var modulePath = './path/to/file.module';

Expand Down Expand Up @@ -276,9 +288,7 @@ describe('Loader', function() {
it ('should return a loadChildren System.import statement', function() {
var result = [
'loadChildren: () => System.import(\'./path/to/file.module.ngfactory\')',
' .then(function(module) {',
' return module[\'FileModuleNgFactory\'];',
' })'
' .then(module => module[\'FileModuleNgFactory\'])'
];

var loadedString = loader.call({
Expand All @@ -289,6 +299,20 @@ describe('Loader', function() {
checkResult(loadedString, result);
});

it ('should return a loadChildren dynamic import statement', function() {
var result = [
'loadChildren: () => import(\'./path/to/file.module.ngfactory\')',
' .then(module => module[\'FileModuleNgFactory\'])'
];

var loadedString = loader.call({
resourcePath: resourcePath,
query: query + '&loader=import'
}, `loadChildren: '${modulePath}'`);

checkResult(loadedString, result);
});

it('should support a custom moduleSuffix', function() {
var moduleSuffix = '.ngfile';

Expand Down
17 changes: 14 additions & 3 deletions spec/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,26 @@ describe('Utils', function() {
it('should return an asynchronous System.import loadChildren statement', function() {
var result = [
'loadChildren: () => System.import(\'' + path + '\')',
' .then(function(module) {',
' return module[\'' + name + '\'];',
' })'
' .then(module => module[\'' + name + '\'])'
];

getSystemLoader('path', 'name', true).should.eql(result.join(''));
});
});

describe('getImportLoader', function() {
var getImportLoader = utils.getImportLoader;

it('should return an asynchronous dynamic import loadChildren statement', function() {
var result = [
'loadChildren: () => import(\'' + path + '\')',
' .then(module => module[\'' + name + '\'])'
];

getImportLoader('path', 'name', true).should.eql(result.join(''));
});
});

describe('normalizeFilePath', function() {
var pmock = require('pmock');
var normalizeFilePath = utils.normalizeFilePath;
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ module.exports = function(source, sourcemap) {
replacement = utils.getSyncLoader(filePath, moduleName, inline);
} else if (loader === 'system') {
replacement = utils.getSystemLoader(filePath, moduleName, inline);
} else if (loader === 'import') {
replacement = utils.getImportLoader(filePath, moduleName, inline);
} else {
replacement = utils.getRequireLoader(filePath, chunkName, moduleName, inline, isJs);
}
Expand Down
13 changes: 10 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ module.exports.getRequireLoader = function(filePath, chunkName, moduleName, inli
module.exports.getSystemLoader = function(filePath, moduleName, inline) {
var result = [
'loadChildren: () => System.import(\'' + filePath + '\')',
' .then(function(module) {',
' return module[\'' + moduleName + '\'];',
' })'
' .then(module => module[\'' + moduleName + '\'])'
];

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

module.exports.getImportLoader = function(filePath, moduleName, inline) {
var result = [
'loadChildren: () => import(\'' + filePath + '\')',
' .then(module => module[\'' + moduleName + '\'])'
];

return inline ? result.join('') : result.join('\n');
Expand Down