-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-webpack): add package
- Loading branch information
1 parent
2ab8b76
commit f8b5a99
Showing
41 changed files
with
902 additions
and
40 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
# Webpack Builder for Architect | ||
|
||
WIP |
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,15 @@ | ||
{ | ||
"$schema": "../architect/src/builders-schema.json", | ||
"builders": { | ||
"webpack": { | ||
"class": "./src/webpack", | ||
"schema": "./src/webpack/schema.json", | ||
"description": "Build a webpack app." | ||
}, | ||
"webpack-dev-server": { | ||
"class": "./src/webpack-dev-server", | ||
"schema": "./src/webpack-dev-server/schema.json", | ||
"description": "Serve a webpack app." | ||
} | ||
} | ||
} |
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,20 @@ | ||
{ | ||
"name": "@angular-devkit/build-webpack", | ||
"version": "0.0.0", | ||
"description": "Webpack Builder for Architect", | ||
"main": "src/index.js", | ||
"typings": "src/index.d.ts", | ||
"builders": "builders.json", | ||
"scripts": { | ||
"preinstall": "echo DO NOT INSTALL THIS PROJECT, ONLY THE ROOT PROJECT. && exit 1" | ||
}, | ||
"dependencies": { | ||
"@angular-devkit/architect": "0.0.0", | ||
"@angular-devkit/core": "0.0.0", | ||
"rxjs": "^6.0.0" | ||
}, | ||
"peerDependencies": { | ||
"webpack": "~4.6.0", | ||
"webpack-dev-server": "^3.1.4" | ||
} | ||
} |
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,10 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
export * from './webpack'; | ||
export * from './webpack-dev-server'; |
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,17 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { TestProjectHost } from '@angular-devkit/architect/testing'; | ||
import { join, normalize } from '@angular-devkit/core'; | ||
|
||
|
||
const devkitRoot = normalize((global as any)._DevKitRoot); // tslint:disable-line:no-any | ||
const basicWorkspaceRoot = join(devkitRoot, 'tests/@angular_devkit/build_webpack/basic-app/'); | ||
export const basicHost = new TestProjectHost(basicWorkspaceRoot); | ||
const angularWorkspaceRoot = join(devkitRoot, 'tests/@angular_devkit/build_webpack/angular-app/'); | ||
export const angularHost = new TestProjectHost(angularWorkspaceRoot); |
84 changes: 84 additions & 0 deletions
84
packages/angular_devkit/build_webpack/src/webpack-dev-server/index.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,84 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { | ||
BuildEvent, | ||
Builder, | ||
BuilderConfiguration, | ||
BuilderContext, | ||
} from '@angular-devkit/architect'; | ||
import { Path, getSystemPath, normalize, resolve } from '@angular-devkit/core'; | ||
import { Observable, from } from 'rxjs'; | ||
import { concatMap } from 'rxjs/operators'; | ||
import * as webpack from 'webpack'; | ||
import * as WebpackDevServer from 'webpack-dev-server'; | ||
import { LoggingCallback, defaultLoggingCb } from '../webpack'; | ||
import { WebpackDevServerBuilderSchema } from './schema'; | ||
|
||
|
||
export class WebpackDevServerBuilder implements Builder<WebpackDevServerBuilderSchema> { | ||
|
||
constructor(public context: BuilderContext) { } | ||
|
||
run(builderConfig: BuilderConfiguration<WebpackDevServerBuilderSchema>): Observable<BuildEvent> { | ||
const configPath = resolve(this.context.workspace.root, | ||
normalize(builderConfig.options.webpackConfig)); | ||
|
||
return this.loadWebpackConfig(getSystemPath(configPath)).pipe( | ||
concatMap(config => this.runWebpackDevServer(config)), | ||
); | ||
} | ||
|
||
public loadWebpackConfig(webpackConfigPath: string): Observable<webpack.Configuration> { | ||
return from(import(webpackConfigPath)); | ||
} | ||
|
||
public runWebpackDevServer( | ||
webpackConfig: webpack.Configuration, | ||
devServerCfg?: WebpackDevServer.Configuration, | ||
loggingCb: LoggingCallback = defaultLoggingCb, | ||
): Observable<BuildEvent> { | ||
return new Observable(obs => { | ||
const devServerConfig = devServerCfg || webpackConfig.devServer || {}; | ||
devServerConfig.host = devServerConfig.host || 'localhost'; | ||
devServerConfig.port = devServerConfig.port || 8080; | ||
|
||
if (devServerConfig.stats) { | ||
webpackConfig.stats = devServerConfig.stats; | ||
} | ||
// Disable stats reporting by the devserver, we have our own logger. | ||
devServerConfig.stats = false; | ||
|
||
const webpackCompiler = webpack(webpackConfig); | ||
const server = new WebpackDevServer(webpackCompiler, devServerConfig); | ||
|
||
webpackCompiler.hooks.done.tap('build-webpack', (stats) => { | ||
// Log stats. | ||
loggingCb(stats, webpackConfig, this.context.logger); | ||
|
||
obs.next({ success: !stats.hasErrors() }); | ||
}); | ||
|
||
server.listen( | ||
devServerConfig.port, | ||
devServerConfig.host, | ||
(err) => { | ||
if (err) { | ||
obs.error(err); | ||
} | ||
}, | ||
); | ||
|
||
// Teardown logic. Close the server when unsubscribed from. | ||
return () => server.close(); | ||
}); | ||
} | ||
} | ||
|
||
|
||
export default WebpackDevServerBuilder; |
29 changes: 29 additions & 0 deletions
29
packages/angular_devkit/build_webpack/src/webpack-dev-server/index_spec_large.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,29 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { request, runTargetSpec } from '@angular-devkit/architect/testing'; | ||
import { from } from 'rxjs'; | ||
import { concatMap, take, tap } from 'rxjs/operators'; | ||
import { basicHost } from '../test-utils'; | ||
|
||
|
||
describe('Dev Server Builder', () => { | ||
const webpackTargetSpec = { project: 'app', target: 'serve' }; | ||
|
||
beforeEach(done => basicHost.initialize().toPromise().then(done, done.fail)); | ||
afterEach(done => basicHost.restore().toPromise().then(done, done.fail)); | ||
|
||
it('works', (done) => { | ||
runTargetSpec(basicHost, webpackTargetSpec).pipe( | ||
tap((buildEvent) => expect(buildEvent.success).toBe(true)), | ||
concatMap(() => from(request('http://localhost:8080/bundle.js'))), | ||
tap(response => expect(response).toContain(`console.log('hello world')`)), | ||
take(1), | ||
).toPromise().then(done, done.fail); | ||
}, 30000); | ||
}); |
13 changes: 13 additions & 0 deletions
13
packages/angular_devkit/build_webpack/src/webpack-dev-server/schema.d.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,13 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
export interface WebpackDevServerBuilderSchema { | ||
/** | ||
* The path to the Webpack configuration file. | ||
*/ | ||
webpackConfig: string; | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/angular_devkit/build_webpack/src/webpack-dev-server/schema.json
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,15 @@ | ||
{ | ||
"title": "Webpack Dev-Server Builder", | ||
"description": "Webpack Dev-Server Builder schema for Architect.", | ||
"type": "object", | ||
"properties": { | ||
"webpackConfig": { | ||
"type": "string", | ||
"description": "The path to the Webpack configuration file." | ||
} | ||
}, | ||
"additionalProperties": false, | ||
"required": [ | ||
"webpackConfig" | ||
] | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/angular_devkit/build_webpack/src/webpack/index.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,87 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import { | ||
BuildEvent, | ||
Builder, | ||
BuilderConfiguration, | ||
BuilderContext, | ||
} from '@angular-devkit/architect'; | ||
import { Path, getSystemPath, logging, normalize, resolve } from '@angular-devkit/core'; | ||
import { Observable, from } from 'rxjs'; | ||
import { concatMap } from 'rxjs/operators'; | ||
import * as webpack from 'webpack'; | ||
import { WebpackBuilderSchema } from './schema'; | ||
|
||
|
||
export interface LoggingCallback { | ||
(stats: webpack.Stats, config: webpack.Configuration, logger: logging.Logger): void; | ||
} | ||
|
||
export const defaultLoggingCb: LoggingCallback = (stats, config, logger) => | ||
logger.info(stats.toString(config.stats)); | ||
|
||
export class WebpackBuilder implements Builder<WebpackBuilderSchema> { | ||
|
||
constructor(public context: BuilderContext) { } | ||
|
||
run(builderConfig: BuilderConfiguration<WebpackBuilderSchema>): Observable<BuildEvent> { | ||
const configPath = resolve(this.context.workspace.root, | ||
normalize(builderConfig.options.webpackConfig)); | ||
|
||
return this.loadWebpackConfig(getSystemPath(configPath)).pipe( | ||
concatMap(config => this.runWebpack(config)), | ||
); | ||
} | ||
|
||
public loadWebpackConfig(webpackConfigPath: string): Observable<webpack.Configuration> { | ||
return from(import(webpackConfigPath)); | ||
} | ||
|
||
public runWebpack( | ||
config: webpack.Configuration, loggingCb = defaultLoggingCb, | ||
): Observable<BuildEvent> { | ||
return new Observable(obs => { | ||
const webpackCompiler = webpack(config); | ||
|
||
const callback: webpack.compiler.CompilerCallback = (err, stats) => { | ||
if (err) { | ||
return obs.error(err); | ||
} | ||
|
||
// Log stats. | ||
loggingCb(stats, config, this.context.logger); | ||
|
||
obs.next({ success: !stats.hasErrors() }); | ||
|
||
if (!config.watch) { | ||
obs.complete(); | ||
} | ||
}; | ||
|
||
try { | ||
if (config.watch) { | ||
const watchOptions = config.watchOptions || {}; | ||
const watching = webpackCompiler.watch(watchOptions, callback); | ||
|
||
// Teardown logic. Close the watcher when unsubscribed from. | ||
return () => watching.close(() => { }); | ||
} else { | ||
webpackCompiler.run(callback); | ||
} | ||
} catch (err) { | ||
if (err) { | ||
this.context.logger.error( | ||
'\nAn error occured during the build:\n' + ((err && err.stack) || err)); | ||
} | ||
throw err; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default WebpackBuilder; |
Oops, something went wrong.