Skip to content

feature(import resolving): ability to import (ts, sass etc.) with path alias #2392

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

Closed
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
11 changes: 10 additions & 1 deletion packages/angular-cli/blueprints/ng2/files/__path__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"baseUrl": ".",
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand All @@ -12,6 +13,14 @@
"target": "es5",
"typeRoots": [
"../node_modules/@types"
]
],
"paths": {
"@app": [
"app"
],
"@app/*": [
"app/*"
]
}
}
}
3 changes: 3 additions & 0 deletions packages/angular-cli/blueprints/ng2/files/angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
},
"alias": {
"@app": "app"
}
}
],
Expand Down
6 changes: 6 additions & 0 deletions packages/angular-cli/lib/config/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export interface CliConfig {
environments?: {
[name: string]: any;
};
/**
* Name and corresponding path.
*/
alias?: {
[name: string]: string;
};
}[];
/**
* Configuration reserved for installed third party addons.
Expand Down
5 changes: 5 additions & 0 deletions packages/angular-cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
"description": "Name and corresponding file for environment config.",
"type": "object",
"additionalProperties": true
},
"alias": {
"description": "Aliases for resolve imports in webpack",
"type": "object",
"additionalProperties": true
}
},
"additionalProperties": false
Expand Down
19 changes: 19 additions & 0 deletions packages/angular-cli/models/get-aliases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as path from 'path';

export function getAliases(
projectRoot: string,
appConfig: any
) {
const aliases: any = {};

const appRoot = path.resolve(projectRoot, appConfig.root);

if (appConfig.alias) {
for (let aliasKey in appConfig.alias) {
let alias = path.resolve(appRoot, appConfig.alias[aliasKey]);
aliases[aliasKey] = alias;
}
}

return aliases;
}
6 changes: 5 additions & 1 deletion packages/angular-cli/models/webpack-build-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as webpack from 'webpack';
import * as path from 'path';
import {BaseHrefWebpackPlugin} from '@angular-cli/base-href-webpack';

import { getAliases } from './get-aliases';

const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

Expand All @@ -21,6 +23,7 @@ export function getWebpackCommonConfig(
const scripts = appConfig.scripts
? appConfig.scripts.map((script: string) => path.resolve(appRoot, script))
: [];
const aliases = getAliases(projectRoot, appConfig);

let entry: { [key: string]: string[] } = {
main: [appMain]
Expand All @@ -33,7 +36,8 @@ export function getWebpackCommonConfig(
return {
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.js']
extensions: ['.ts', '.js'],
alias: aliases
},
context: path.resolve(__dirname, './'),
entry: entry,
Expand Down
5 changes: 4 additions & 1 deletion packages/angular-cli/models/webpack-build-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// this config must be JS so that the karma plugin can load it
const getAliases = require('./get-aliases').getAliases;

const path = require('path');
const webpack = require('webpack');

const getWebpackTestConfig = function (projectRoot, environment, appConfig) {

const appRoot = path.resolve(projectRoot, appConfig.root);
const aliases = getAliases(projectRoot, appConfig);

return {
devtool: 'inline-source-map',
context: path.resolve(__dirname, './'),
resolve: {
extensions: ['.ts', '.js']
extensions: ['.ts', '.js'],
alias: aliases
},
entry: {
test: path.resolve(appRoot, appConfig.test)
Expand Down