Skip to content

revert: fix(@ngtools/webpack): fix paths-plugin to allow '*' as alias #6463

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

Merged
merged 1 commit into from
May 25, 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"diff": "^3.1.0",
"ember-cli-normalize-entity-name": "^1.0.0",
"ember-cli-string-utils": "^1.0.0",
"enhanced-resolve": "^3.1.0",
"exports-loader": "^0.6.3",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.10.0",
Expand Down
1 change: 1 addition & 0 deletions packages/@ngtools/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"npm": ">= 3.0.0"
},
"dependencies": {
"enhanced-resolve": "^3.1.0",
"loader-utils": "^1.0.2",
"magic-string": "^0.19.0",
"source-map": "^0.5.6"
Expand Down
123 changes: 88 additions & 35 deletions packages/@ngtools/webpack/src/paths-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import * as path from 'path';
import * as ts from 'typescript';
import {Callback, Tapable, NormalModuleFactory, NormalModuleFactoryRequest} from './webpack';
import {Request, ResolverPlugin, Callback, Tapable} from './webpack';


const ModulesInRootPlugin: new (a: string, b: string, c: string) => ResolverPlugin
= require('enhanced-resolve/lib/ModulesInRootPlugin');

interface CreateInnerCallback {
(callback: Callback<any>,
options: Callback<any>,
message?: string,
messageOptional?: string): Callback<any>;
}

const createInnerCallback: CreateInnerCallback
= require('enhanced-resolve/lib/createInnerCallback');
const getInnerRequest: (resolver: ResolverPlugin, request: Request) => string
= require('enhanced-resolve/lib/getInnerRequest');


function escapeRegExp(str: string): string {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}


export interface PathsPluginOptions {
nmf: NormalModuleFactory;
tsConfigPath: string;
compilerOptions?: ts.CompilerOptions;
compilerHost?: ts.CompilerHost;
}

export class PathsPlugin implements Tapable {
private _nmf: NormalModuleFactory;
private _tsConfigPath: string;
private _compilerOptions: ts.CompilerOptions;
private _host: ts.CompilerHost;

source: string;
target: string;

private mappings: any;

private _absoluteBaseUrl: string;
private _mappings: any[] = [];

private static _loadOptionsFromTsConfig(tsConfigPath: string, host?: ts.CompilerHost):
ts.CompilerOptions {
Expand Down Expand Up @@ -55,13 +76,15 @@ export class PathsPlugin implements Tapable {
this._host = ts.createCompilerHost(this._compilerOptions, false);
}

this.source = 'described-resolve';
this.target = 'resolve';

this._absoluteBaseUrl = path.resolve(
path.dirname(this._tsConfigPath),
this._compilerOptions.baseUrl || '.'
);

this._nmf = options.nmf;

this.mappings = [];
let paths = this._compilerOptions.paths || {};
Object.keys(paths).forEach(alias => {
let onlyModule = alias.indexOf('*') === -1;
Expand All @@ -76,7 +99,7 @@ export class PathsPlugin implements Tapable {
aliasPattern = new RegExp(`^${withStarCapturing}`);
}

this._mappings.push({
this.mappings.push({
onlyModule,
alias,
aliasPattern,
Expand All @@ -86,36 +109,66 @@ export class PathsPlugin implements Tapable {
});
}

apply(): void {
this._nmf.plugin('before-resolve', (request: NormalModuleFactoryRequest,
callback: Callback<any>) => {
for (let mapping of this._mappings) {
const match = request.request.match(mapping.aliasPattern);
if (!match) { continue; }
apply(resolver: ResolverPlugin): void {
let baseUrl = this._compilerOptions.baseUrl || '.';

let newRequestStr = mapping.target;
if (!mapping.onlyModule) {
newRequestStr = newRequestStr.replace('*', match[1]);
}

const moduleResolver: ts.ResolvedModuleWithFailedLookupLocations =
ts.nodeModuleNameResolver(
newRequestStr,
this._absoluteBaseUrl,
this._compilerOptions,
this._host
);
const moduleFilePath = moduleResolver.resolvedModule ?
moduleResolver.resolvedModule.resolvedFileName : '';

if (moduleFilePath) {
return callback(null, Object.assign({}, request, {
request: moduleFilePath.includes('.d.ts') ? newRequestStr : moduleFilePath
}));
}
}
if (baseUrl) {
resolver.apply(new ModulesInRootPlugin('module', this._absoluteBaseUrl, 'resolve'));
}

return callback(null, request);
this.mappings.forEach((mapping: any) => {
resolver.plugin(this.source, this.createPlugin(resolver, mapping));
});
}

resolve(resolver: ResolverPlugin, mapping: any, request: any, callback: Callback<any>): any {
let innerRequest = getInnerRequest(resolver, request);
if (!innerRequest) {
return callback();
}

let match = innerRequest.match(mapping.aliasPattern);
if (!match) {
return callback();
}

let newRequestStr = mapping.target;
if (!mapping.onlyModule) {
newRequestStr = newRequestStr.replace('*', match[1]);
}
if (newRequestStr[0] === '.') {
newRequestStr = path.resolve(this._absoluteBaseUrl, newRequestStr);
}

let newRequest = Object.assign({}, request, {
request: newRequestStr
}) as Request;

return resolver.doResolve(
this.target,
newRequest,
`aliased with mapping '${innerRequest}': '${mapping.alias}' to '${newRequestStr}'`,
createInnerCallback(
function(err, result) {
if (arguments.length > 0) {
return callback(err, result);
}

// don't allow other aliasing or raw request
callback(null, null);
},
callback
)
);
}

createPlugin(resolver: ResolverPlugin, mapping: any): any {
return (request: any, callback: Callback<any>) => {
try {
this.resolve(resolver, mapping, request, callback);
} catch (err) {
callback(err);
}
};
}
}
4 changes: 0 additions & 4 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,7 @@ export class AotPlugin implements Tapable {
cb();
}
});
});

compiler.plugin('normal-module-factory', (nmf: any) => {
compiler.resolvers.normal.apply(new PathsPlugin({
nmf,
tsConfigPath: this._tsConfigPath,
compilerOptions: this._compilerOptions,
compilerHost: this._compilerHost
Expand Down
12 changes: 0 additions & 12 deletions packages/@ngtools/webpack/src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ export interface NormalModule {
resource: string;
}

export interface NormalModuleFactory {
plugin(
event: string,
callback: (data: NormalModuleFactoryRequest, callback: Callback<any>) => void
): any;
}

export interface NormalModuleFactoryRequest {
request: string;
contextInfo: { issuer: string };
}

export interface LoaderContext {
_module: NormalModule;

Expand Down
6 changes: 0 additions & 6 deletions tests/e2e/tests/build/ts-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export default function() {
],
'@shared/*': [
'app/shared/*'
],
'*': [
'*',
'app/shared/*'
]
};
})
Expand All @@ -29,14 +25,12 @@ export default function() {
import { meaning } from 'app/shared/meaning';
import { meaning as meaning2 } from '@shared';
import { meaning as meaning3 } from '@shared/meaning';
import { meaning as meaning4 } from 'meaning';
// need to use imports otherwise they are ignored and
// no error is outputted, even if baseUrl/paths don't work
console.log(meaning)
console.log(meaning2)
console.log(meaning3)
console.log(meaning4)
`))
.then(() => ng('build'));
}