-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ability to merge ng cli webpack config
- Loading branch information
Showing
4 changed files
with
82 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
export function getAngularCliWebpackConfigOptions(dirToSearch, appIndex = 0) { | ||
const fname = path.join(dirToSearch, '.angular-cli.json'); | ||
if (!fs.existsSync(fname)) { | ||
return null; | ||
} | ||
const cliConfig = JSON.parse(fs.readFileSync(fname, 'utf8')); | ||
if (!cliConfig.apps || !cliConfig.apps.length) { | ||
throw new Error('.angular-cli.json must have apps entry.'); | ||
} | ||
const appConfig = cliConfig.apps[appIndex]; | ||
|
||
// FIXME dummy value | ||
const cliWebpackConfigOptions = { | ||
projectRoot: dirToSearch, | ||
appConfig, | ||
buildOptions: { | ||
outputPath: 'outputPath', | ||
}, | ||
supportES2015: false, | ||
}; | ||
|
||
return cliWebpackConfigOptions; | ||
} | ||
|
||
export function applyAngularCliWebpackConfig(baseConfig, cliWebpackConfigOptions) { | ||
if (!cliWebpackConfigOptions) return baseConfig; | ||
|
||
let ngcliConfigFactory; | ||
try { | ||
// We should not require('@angular/cli') at the top script level because user project might not have `@angular/cli`. | ||
// eslint-disable-next-line global-require, import/no-extraneous-dependencies | ||
ngcliConfigFactory = require('@angular/cli/models/webpack-configs'); | ||
} catch (e) { | ||
return baseConfig; | ||
} | ||
|
||
const cliCommonConfig = ngcliConfigFactory.getCommonConfig(cliWebpackConfigOptions); | ||
const cliStyleConfig = ngcliConfigFactory.getStylesConfig(cliWebpackConfigOptions); | ||
|
||
// Don't use storybooks .css rules because we have to use .css rules created by @angualr/cli | ||
// because @angular/cli created has include/exclude rules of global .css files. | ||
const styleRules = baseConfig.module.rules.filter( | ||
rule => !rule.test || rule.test.toString() !== '/\\.css$/' | ||
); | ||
|
||
// cliStyleConfig.entry adds global style files to the webpack context | ||
const entry = { | ||
...baseConfig.entry, | ||
...cliStyleConfig.entry, | ||
}; | ||
|
||
const mod = { | ||
...baseConfig.module, | ||
rules: [...cliStyleConfig.module.rules, ...styleRules], | ||
}; | ||
|
||
// We use cliCommonConfig plugins to serve static assets files. | ||
const plugins = [...cliStyleConfig.plugins, ...cliCommonConfig.plugins, ...baseConfig.plugins]; | ||
|
||
return { | ||
...baseConfig, | ||
entry, | ||
module: mod, | ||
plugins, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters