Skip to content

Commit 0ac7524

Browse files
committed
fix(@angular/cli): fix asset watching
https://github.com/kevlened/copy-webpack-plugin is now used instead of the custom plugin, it has since implemented the features we needed. Fix angular#7521
1 parent 7b8e0fa commit 0ac7524

File tree

11 files changed

+168
-948
lines changed

11 files changed

+168
-948
lines changed

package-lock.json

+107-796
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444
"@angular-devkit/build-optimizer": "~0.0.15",
4545
"@angular-devkit/schematics": "~0.0.18",
4646
"@schematics/angular": "~0.0.30",
47+
"@types/copy-webpack-plugin": "^4.0.0",
4748
"autoprefixer": "^6.5.3",
4849
"chalk": "^2.0.1",
4950
"circular-dependency-plugin": "^3.0.0",
5051
"common-tags": "^1.3.1",
52+
"copy-webpack-plugin": "^4.0.1",
5153
"core-object": "^3.1.0",
5254
"css-loader": "^0.28.1",
5355
"cssnano": "^3.10.0",

packages/@angular/cli/models/webpack-configs/common.ts

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as webpack from 'webpack';
22
import * as path from 'path';
3-
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
3+
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
44
import { NamedLazyChunksWebpackPlugin } from '../../plugins/named-lazy-chunks-webpack-plugin';
55
import { InsertConcatAssetsWebpackPlugin } from '../../plugins/insert-concat-assets-webpack-plugin';
6-
import { extraEntryParser, getOutputHashFormat } from './utils';
6+
import { extraEntryParser, getOutputHashFormat, AssetPattern } from './utils';
7+
import { isDirectory } from '../../utilities/is-directory';
78
import { WebpackConfigOptions } from '../webpack-config';
89

910
const ConcatPlugin = require('webpack-concat-plugin');
@@ -84,10 +85,37 @@ export function getCommonConfig(wco: WebpackConfigOptions) {
8485

8586
// process asset entries
8687
if (appConfig.assets) {
87-
extraPlugins.push(new GlobCopyWebpackPlugin({
88-
patterns: appConfig.assets,
89-
globOptions: { cwd: appRoot, dot: true, ignore: '**/.gitkeep' }
90-
}));
88+
extraPlugins.push(new CopyWebpackPlugin(
89+
appConfig.assets.map((asset: string | AssetPattern) => {
90+
// Convert all string assets to object notation.
91+
asset = typeof asset === 'string' ? { glob: asset } : asset;
92+
// Add defaults.
93+
// Input is always resolved relative to the appRoot.
94+
asset.input = path.resolve(appRoot, asset.input || '');
95+
asset.output = asset.output || '';
96+
asset.glob = asset.glob || '';
97+
98+
// Ensure trailing slash.
99+
if (isDirectory(path.resolve(asset.input))) {
100+
asset.input += '/';
101+
}
102+
103+
// Convert dir patterns to globs.
104+
if (isDirectory(path.resolve(asset.input, asset.glob))) {
105+
asset.glob = asset.glob + '/**/*';
106+
}
107+
108+
return {
109+
context: asset.input,
110+
to: asset.output,
111+
from: {
112+
glob: asset.glob,
113+
dot: true
114+
}
115+
};
116+
}),
117+
{ ignore: ['.gitkeep'] }
118+
));
91119
}
92120

93121
if (buildOptions.progress) {

packages/@angular/cli/models/webpack-configs/production.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import * as path from 'path';
22
import * as webpack from 'webpack';
33
import * as fs from 'fs';
44
import * as semver from 'semver';
5+
import * as CopyWebpackPlugin from 'copy-webpack-plugin';
56
import { stripIndent } from 'common-tags';
67
import { LicenseWebpackPlugin } from 'license-webpack-plugin';
78
import { PurifyPlugin } from '@angular-devkit/build-optimizer';
89
import { StaticAssetPlugin } from '../../plugins/static-asset';
9-
import { GlobCopyWebpackPlugin } from '../../plugins/glob-copy-webpack-plugin';
1010
import { WebpackConfigOptions } from '../webpack-config';
1111

1212

@@ -58,16 +58,10 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
5858
`);
5959
}
6060

61-
extraPlugins.push(new GlobCopyWebpackPlugin({
62-
patterns: [
63-
'ngsw-manifest.json',
64-
{glob: 'ngsw-manifest.json', input: path.resolve(projectRoot, appConfig.root), output: ''}
65-
],
66-
globOptions: {
67-
cwd: projectRoot,
68-
optional: true,
69-
},
70-
}));
61+
extraPlugins.push(new CopyWebpackPlugin([{
62+
context: path.resolve(projectRoot, appConfig.root),
63+
from: { glob: 'ngsw-manifest.json', }
64+
}]));
7165

7266
// Load the Webpack plugin for manifest generation and install it.
7367
const AngularServiceWorkerPlugin = require('@angular/service-worker/build/webpack')

packages/@angular/cli/models/webpack-configs/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ export function getOutputHashFormat(option: string, length = 20): HashFormat {
8787
/* tslint:enable:max-line-length */
8888
return hashFormats[option] || hashFormats['none'];
8989
}
90+
91+
export interface AssetPattern {
92+
glob: string;
93+
input?: string;
94+
output?: string;
95+
}

packages/@angular/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"chalk": "^2.0.1",
3737
"circular-dependency-plugin": "^3.0.0",
3838
"common-tags": "^1.3.1",
39+
"copy-webpack-plugin": "^4.0.1",
3940
"core-object": "^3.1.0",
4041
"css-loader": "^0.28.1",
4142
"cssnano": "^3.10.0",

packages/@angular/cli/plugins/glob-copy-webpack-plugin.ts

-105
This file was deleted.

packages/@angular/cli/plugins/karma.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as glob from 'glob';
44
import * as webpack from 'webpack';
55
const webpackDevMiddleware = require('webpack-dev-middleware');
66

7-
import { Pattern } from './glob-copy-webpack-plugin';
7+
import { AssetPattern } from '../models/webpack-configs/utils';
8+
import { isDirectory } from '../utilities/is-directory';
89
import { WebpackTestConfig, WebpackTestOptions } from '../models/webpack-test-config';
910
import { KarmaWebpackThrowError } from './karma-webpack-throw-error';
1011

@@ -22,14 +23,6 @@ const getAppFromConfig = require('../utilities/app-utils').getAppFromConfig;
2223
let blocked: any[] = [];
2324
let isBlocked = false;
2425

25-
function isDirectory(path: string) {
26-
try {
27-
return fs.statSync(path).isDirectory();
28-
} catch (_) {
29-
return false;
30-
}
31-
}
32-
3326
// Add files to the Karma files array.
3427
function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
3528
const defaults = {
@@ -82,7 +75,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
8275
// Add assets. This logic is mimics the one present in GlobCopyWebpackPlugin.
8376
if (appConfig.assets) {
8477
config.proxies = config.proxies || {};
85-
appConfig.assets.forEach((pattern: Pattern) => {
78+
appConfig.assets.forEach((pattern: AssetPattern) => {
8679
// Convert all string patterns to Pattern type.
8780
pattern = typeof pattern === 'string' ? { glob: pattern } : pattern;
8881
// Add defaults.

packages/@angular/cli/plugins/webpack.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module.exports = {
44
BaseHrefWebpackPlugin:
55
require('../lib/base-href-webpack/base-href-webpack-plugin').BaseHrefWebpackPlugin,
6-
GlobCopyWebpackPlugin: require('../plugins/glob-copy-webpack-plugin').GlobCopyWebpackPlugin,
76
SuppressExtractedTextChunksWebpackPlugin:
87
require('../plugins/suppress-entry-chunks-webpack-plugin')
98
.SuppressExtractedTextChunksWebpackPlugin,

packages/@angular/cli/tasks/eject.ts

+1-19
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,6 @@ class JsonWebpackSerializer {
7777
}
7878
}
7979

80-
private _globCopyWebpackPluginSerialize(value: any): any {
81-
let patterns = value.options.patterns;
82-
let globOptions = value.options.globOptions;
83-
return {
84-
patterns,
85-
globOptions: this._globReplacer(globOptions)
86-
};
87-
}
88-
8980
private _insertConcatAssetsWebpackPluginSerialize(value: any): any {
9081
return value.entryNames;
9182
}
@@ -189,10 +180,6 @@ class JsonWebpackSerializer {
189180
case angularCliPlugins.SuppressExtractedTextChunksWebpackPlugin:
190181
this._addImport('@angular/cli/plugins/webpack', plugin.constructor.name);
191182
break;
192-
case angularCliPlugins.GlobCopyWebpackPlugin:
193-
args = this._globCopyWebpackPluginSerialize(plugin);
194-
this._addImport('@angular/cli/plugins/webpack', 'GlobCopyWebpackPlugin');
195-
break;
196183
case angularCliPlugins.InsertConcatAssetsWebpackPlugin:
197184
args = this._insertConcatAssetsWebpackPluginSerialize(plugin);
198185
this._addImport('@angular/cli/plugins/webpack', 'InsertConcatAssetsWebpackPlugin');
@@ -356,12 +343,6 @@ class JsonWebpackSerializer {
356343
});
357344
}
358345

359-
private _globReplacer(value: any) {
360-
return Object.assign({}, value, {
361-
cwd: this._relativePath('process.cwd()', path.relative(this._root, value.cwd))
362-
});
363-
}
364-
365346
private _replacer(_key: string, value: any) {
366347
if (value === undefined) {
367348
return value;
@@ -538,6 +519,7 @@ export default Task.extend({
538519
'url-loader',
539520
'circular-dependency-plugin',
540521
'webpack-concat-plugin',
522+
'copy-webpack-plugin',
541523
].forEach((packageName: string) => {
542524
packageJson['devDependencies'][packageName] = ourPackageJson['dependencies'][packageName];
543525
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as fs from 'fs';
2+
3+
export function isDirectory(path: string) {
4+
try {
5+
return fs.statSync(path).isDirectory();
6+
} catch (_) {
7+
return false;
8+
}
9+
}

0 commit comments

Comments
 (0)