-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx): generating apps works without ng-add (#1287)
- Loading branch information
1 parent
422fef7
commit a3be21c
Showing
18 changed files
with
305 additions
and
129 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
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
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
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,102 @@ | ||
import { normalizeBuildOptions } from './normalize'; | ||
import { BuildBuilderOptions } from './types'; | ||
import { Path, normalize } from '@angular-devkit/core'; | ||
|
||
import * as fs from 'fs'; | ||
|
||
describe('normalizeBuildOptions', () => { | ||
let testOptions: BuildBuilderOptions; | ||
let root: string; | ||
let sourceRoot: Path; | ||
|
||
beforeEach(() => { | ||
testOptions = { | ||
main: 'apps/nodeapp/src/main.ts', | ||
tsConfig: 'apps/nodeapp/tsconfig.app.json', | ||
outputPath: 'dist/apps/nodeapp', | ||
fileReplacements: [ | ||
{ | ||
replace: 'apps/environment/environment.ts', | ||
with: 'apps/environment/environment.prod.ts' | ||
}, | ||
{ | ||
replace: 'module1.ts', | ||
with: 'module2.ts' | ||
} | ||
], | ||
assets: [], | ||
statsJson: false | ||
}; | ||
root = '/root'; | ||
sourceRoot = normalize('apps/nodeapp/src'); | ||
}); | ||
it('should add the root', () => { | ||
const result = normalizeBuildOptions(testOptions, root, sourceRoot); | ||
expect(result.root).toEqual('/root'); | ||
}); | ||
|
||
it('should resolve main from root', () => { | ||
const result = normalizeBuildOptions(testOptions, root, sourceRoot); | ||
expect(result.main).toEqual('/root/apps/nodeapp/src/main.ts'); | ||
}); | ||
|
||
it('should resolve the output path', () => { | ||
const result = normalizeBuildOptions(testOptions, root, sourceRoot); | ||
expect(result.outputPath).toEqual('/root/dist/apps/nodeapp'); | ||
}); | ||
|
||
it('should resolve the tsConfig path', () => { | ||
const result = normalizeBuildOptions(testOptions, root, sourceRoot); | ||
expect(result.tsConfig).toEqual('/root/apps/nodeapp/tsconfig.app.json'); | ||
}); | ||
|
||
it('should normalize asset patterns', () => { | ||
spyOn(fs, 'statSync').and.returnValue({ | ||
isDirectory: () => true | ||
}); | ||
const result = normalizeBuildOptions( | ||
<BuildBuilderOptions>{ | ||
...testOptions, | ||
root, | ||
assets: [ | ||
'apps/nodeapp/src/assets', | ||
{ | ||
input: '/outsideroot', | ||
output: 'output', | ||
glob: '**/*', | ||
ignore: ['**/*.json'] | ||
} | ||
] | ||
}, | ||
root, | ||
sourceRoot | ||
); | ||
expect(result.assets).toEqual([ | ||
{ | ||
input: '/root/apps/nodeapp/src/assets', | ||
output: 'assets', | ||
glob: '**/*' | ||
}, | ||
{ | ||
input: '/outsideroot', | ||
output: 'output', | ||
glob: '**/*', | ||
ignore: ['**/*.json'] | ||
} | ||
]); | ||
}); | ||
|
||
it('should resolve the file replacement paths', () => { | ||
const result = normalizeBuildOptions(testOptions, root, sourceRoot); | ||
expect(result.fileReplacements).toEqual([ | ||
{ | ||
replace: '/root/apps/environment/environment.ts', | ||
with: '/root/apps/environment/environment.prod.ts' | ||
}, | ||
{ | ||
replace: '/root/module1.ts', | ||
with: '/root/module2.ts' | ||
} | ||
]); | ||
}); | ||
}); |
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,86 @@ | ||
import { Path, normalize } from '@angular-devkit/core'; | ||
import { resolve, dirname, relative, basename } from 'path'; | ||
import { | ||
AssetPattern, | ||
AssetPatternObject | ||
} from '@angular-devkit/build-angular'; | ||
import { BuildBuilderOptions } from './types'; | ||
import { statSync } from 'fs'; | ||
|
||
export interface FileReplacement { | ||
replace: string; | ||
with: string; | ||
} | ||
|
||
export function normalizeBuildOptions<T extends BuildBuilderOptions>( | ||
options: T, | ||
root: string, | ||
sourceRoot: Path | ||
): T { | ||
return { | ||
...options, | ||
root: root, | ||
sourceRoot: sourceRoot, | ||
main: resolve(root, options.main), | ||
outputPath: resolve(root, options.outputPath), | ||
tsConfig: resolve(root, options.tsConfig), | ||
fileReplacements: normalizeFileReplacements(root, options.fileReplacements), | ||
assets: normalizeAssets(options.assets, root, sourceRoot), | ||
webpackConfig: options.webpackConfig | ||
? resolve(root, options.webpackConfig) | ||
: options.webpackConfig | ||
}; | ||
} | ||
|
||
function normalizeAssets( | ||
assets: AssetPattern[], | ||
root: string, | ||
sourceRoot: Path | ||
): AssetPatternObject[] { | ||
return assets.map(asset => { | ||
if (typeof asset === 'string') { | ||
const assetPath = normalize(asset); | ||
const resolvedAssetPath = resolve(root, assetPath); | ||
const resolvedSourceRoot = resolve(root, sourceRoot); | ||
|
||
if (!resolvedAssetPath.startsWith(resolvedSourceRoot)) { | ||
throw new Error( | ||
`The ${resolvedAssetPath} asset path must start with the project source root: ${sourceRoot}` | ||
); | ||
} | ||
|
||
const isDirectory = statSync(resolvedAssetPath).isDirectory(); | ||
const input = isDirectory | ||
? resolvedAssetPath | ||
: dirname(resolvedAssetPath); | ||
const output = relative(resolvedSourceRoot, resolve(root, input)); | ||
const glob = isDirectory ? '**/*' : basename(resolvedAssetPath); | ||
return { | ||
input, | ||
output, | ||
glob | ||
}; | ||
} else { | ||
if (asset.output.startsWith('..')) { | ||
throw new Error( | ||
'An asset cannot be written to a location outside of the output path.' | ||
); | ||
} | ||
return { | ||
...asset, | ||
// Now we remove starting slash to make Webpack place it from the output root. | ||
output: asset.output.replace(/^\//, '') | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
function normalizeFileReplacements( | ||
root: string, | ||
fileReplacements: FileReplacement[] | ||
): FileReplacement[] { | ||
return fileReplacements.map(fileReplacement => ({ | ||
replace: resolve(root, fileReplacement.replace), | ||
with: resolve(root, fileReplacement.with) | ||
})); | ||
} |
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
Oops, something went wrong.