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 support for "baseUrl" and "paths" from angular-cli #4162

Merged
merged 21 commits into from
Oct 10, 2018
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
bc8b20c
Add support for baseUrl in angular-cli
igor-dv Sep 11, 2018
837eb3c
Merge remote-tracking branch 'origin/master' into angular/support-bas…
igor-dv Sep 12, 2018
e2b275b
Merge branch 'master' into angular/support-base-url
igor-dv Sep 13, 2018
1135bfe
Merge branch 'master' into angular/support-base-url
igor-dv Sep 14, 2018
e2070b5
Merge branch 'master' into angular/support-base-url
igor-dv Sep 14, 2018
4bfb192
Naming
igor-dv Sep 25, 2018
6b72b9d
Merge remote-tracking branch 'origin/master' into angular/support-bas…
igor-dv Sep 25, 2018
9260fe4
Added basePath support
kroeder Oct 8, 2018
2c560ee
Moved plugin code usage
kroeder Oct 8, 2018
7d36825
Merge remote-tracking branch 'upstream/angular/support-base-url' into…
kroeder Oct 8, 2018
0ed5808
yarn.lock
kroeder Oct 8, 2018
1af1744
dependency order
kroeder Oct 8, 2018
4013f10
Changed static tsconfig.app.json to angular.json tsconfig path
kroeder Oct 8, 2018
3436512
Merge branch 'master' into angular/support-base-url
igor-dv Oct 8, 2018
ffee031
Merge remote-tracking branch 'origin/angular/support-base-url' into s…
igor-dv Oct 8, 2018
18daee8
Mock environments/environment in jest (it's a workaround)
igor-dv Oct 9, 2018
113bdb8
Change .storybook/tsconfig.json to extend the src/tsconfig.json
igor-dv Oct 9, 2018
4c52733
Merge branch 'angular/support-base-url' of https://github.com/storybo…
kroeder Oct 9, 2018
43e9edd
Updated tsconfig 'extends' path for the storybook cli
kroeder Oct 9, 2018
9129064
Merge branch 'support-base-url' of https://github.com/kroeder/storybo…
kroeder Oct 9, 2018
5d277d3
Merge pull request #4323 from kroeder/support-base-url
igor-dv Oct 9, 2018
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
79 changes: 61 additions & 18 deletions app/angular/src/server/angular-cli_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@ import fs from 'fs';
import { logger } from '@storybook/node-logger';
import { isBuildAngularInstalled, normalizeAssetPatterns } from './angular-cli_utils';

function getTsConfigOptions(tsConfigPath) {
const basicOptions = {
options: {},
fileNames: [],
errors: [],
};

if (!fs.existsSync(tsConfigPath)) {
return basicOptions;
}

const tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf8'));
const { baseUrl } = tsConfig.compilerOptions || {};

if (baseUrl) {
const tsConfigDirName = path.dirname(tsConfigPath);
basicOptions.options.baseUrl = path.resolve(tsConfigDirName, baseUrl);
}

return basicOptions;
}

function getAngularCliParts(cliWebpackConfigOptions) {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const ngcliConfigFactory = require('@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ngcliConfigFactory or ngCliConfigFactory?


try {
return {
cliCommonConfig: ngcliConfigFactory.getCommonConfig(cliWebpackConfigOptions),
cliStyleConfig: ngcliConfigFactory.getStylesConfig(cliWebpackConfigOptions),
};
} catch (e) {
return null;
}
}

export function getAngularCliWebpackConfigOptions(dirToSearch) {
const fname = path.join(dirToSearch, 'angular.json');

Expand Down Expand Up @@ -31,16 +67,16 @@ export function getAngularCliWebpackConfigOptions(dirToSearch) {
project.sourceRoot
);

const projectRoot = path.resolve(dirToSearch, project.root);
const tsConfigPath = path.resolve(dirToSearch, 'src/tsconfig.app.json');
igor-dv marked this conversation as resolved.
Show resolved Hide resolved
const tsConfig = getTsConfigOptions(tsConfigPath);

return {
root: dirToSearch,
projectRoot: path.resolve(dirToSearch, project.root),
projectRoot,
tsConfigPath,
tsConfig,
supportES2015: false,
tsConfig: {
options: {},
fileNames: [],
errors: [],
},
tsConfigPath: path.resolve(dirToSearch, 'src/tsconfig.app.json'),
buildOptions: {
...projectOptions,
assets: normalizedAssets,
Expand All @@ -49,27 +85,26 @@ export function getAngularCliWebpackConfigOptions(dirToSearch) {
}

export function applyAngularCliWebpackConfig(baseConfig, cliWebpackConfigOptions) {
if (!cliWebpackConfigOptions) return baseConfig;
if (!cliWebpackConfigOptions) {
return baseConfig;
}

if (!isBuildAngularInstalled()) {
logger.info('=> Using base config because @angular-devkit/build-angular is not installed.');
return baseConfig;
}

// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const ngcliConfigFactory = require('@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs');
const cliParts = getAngularCliParts(cliWebpackConfigOptions);

let cliCommonConfig;
let cliStyleConfig;
try {
cliCommonConfig = ngcliConfigFactory.getCommonConfig(cliWebpackConfigOptions);
cliStyleConfig = ngcliConfigFactory.getStylesConfig(cliWebpackConfigOptions);
} catch (e) {
if (!cliParts) {
logger.warn('=> Failed to get angular-cli webpack config.');
return baseConfig;
}

logger.info('=> Get angular-cli webpack config.');

const { cliCommonConfig, cliStyleConfig } = cliParts;

// Don't use storybooks .css/.scss rules because we have to use rules created by @angular-devkit/build-angular
// because @angular-devkit/build-angular created rules have include/exclude for global style files.
const rulesExcludingStyles = baseConfig.module.rules.filter(
Expand All @@ -85,19 +120,27 @@ export function applyAngularCliWebpackConfig(baseConfig, cliWebpackConfigOptions
.concat(Object.values(cliStyleConfig.entry).reduce((acc, item) => acc.concat(item), [])),
};

const mod = {
const module = {
...baseConfig.module,
rules: [...cliStyleConfig.module.rules, ...rulesExcludingStyles],
};

// We use cliCommonConfig plugins to serve static assets files.
const plugins = [...cliStyleConfig.plugins, ...cliCommonConfig.plugins, ...baseConfig.plugins];

const resolve = {
...baseConfig.resolve,
modules: Array.from(
new Set([...baseConfig.resolve.modules, ...cliCommonConfig.resolve.modules])
),
};

return {
...baseConfig,
entry,
module: mod,
module,
plugins,
resolve,
resolveLoader: cliCommonConfig.resolveLoader,
};
}