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

fix(@angular/cli): stabilize webpack module identifiers #4733

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@angular/cli/models/webpack-configs/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const getProdConfig = function (wco: WebpackConfigOptions) {
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new (<any>webpack).HashedModuleIdsPlugin(),
new webpack.optimize.UglifyJsPlugin(<any>{
mangle: { screw_ie8: true },
compress: { screw_ie8: true, warnings: buildOptions.verbose },
Expand Down
7 changes: 5 additions & 2 deletions packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ class JsonWebpackSerializer {
case webpack.NoEmitOnErrorsPlugin:
this._addImport('webpack', 'NoEmitOnErrorsPlugin');
break;
case (<any>webpack).HashedModuleIdsPlugin:
this._addImport('webpack', 'HashedModuleIdsPlugin');
break;
case webpack.optimize.UglifyJsPlugin:
this._addImport('webpack.optimize', 'UglifyJsPlugin');
break;
Expand Down Expand Up @@ -487,13 +490,13 @@ export default Task.extend({
console.log(yellow(stripIndent`
==========================================================================================
Ejection was successful.

To run your builds, you now need to do the following commands:
- "npm run build" to build.
- "npm run test" to run unit tests.
- "npm start" to serve the app using webpack-dev-server.
- "npm run e2e" to run protractor.

Running the equivalent CLI commands will result in an error.

==========================================================================================
Expand Down
104 changes: 72 additions & 32 deletions tests/e2e/tests/build/chunk-hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,92 @@ import {ng} from '../../utils/process';
import {writeFile} from '../../utils/fs';
import {addImportToModule} from '../../utils/ast';

const OUTPUT_RE = /(main|polyfills|vendor|inline|styles|\d+)\.[a-z0-9]+\.(chunk|bundle)\.(js|css)$/;

function generateFileHashMap(): Map<string, string> {
const hashes = new Map<string, string>();

fs.readdirSync('./dist')
.forEach(name => {
if (!name.match(OUTPUT_RE)) {
return;
}

const [module, hash] = name.split('.');
hashes.set(module, hash);
});

return hashes;
}

function validateHashes(
oldHashes: Map<string, string>,
newHashes: Map<string, string>,
shouldChange: Array<string>): void {

console.log(' Validating hashes...');
console.log(` Old hashes: ${JSON.stringify([...oldHashes])}`);
console.log(` New hashes: ${JSON.stringify([...newHashes])}`);

oldHashes.forEach((hash, module) => {
if (hash == newHashes.get(module)) {
if (shouldChange.includes(module)) {
throw new Error(`Module "${module}" did not change hash (${hash})...`);
}
} else if (!shouldChange.includes(module)) {
throw new Error(`Module "${module}" changed hash (${hash})...`);
}
});
}

export default function() {
const oldHashes: {[module: string]: string} = {};
const newHashes: {[module: string]: string} = {};
let oldHashes: Map<string, string>;
let newHashes: Map<string, string>;
// First, collect the hashes.
return Promise.resolve()
.then(() => ng('generate', 'module', 'lazy', '--routing'))
.then(() => addImportToModule('src/app/app.module.ts', oneLine`
RouterModule.forRoot([{ path: "lazy", loadChildren: "./lazy/lazy.module#LazyModule" }])
`, '@angular/router'))
.then(() => addImportToModule(
'src/app/app.module.ts', 'ReactiveFormsModule', '@angular/forms'))
.then(() => ng('build', '--prod'))
.then(() => {
fs.readdirSync('./dist')
.forEach(name => {
if (!name.match(/(main|inline|styles|\d+)\.[a-z0-9]+\.bundle\.(js|css)/)) {
return;
}

const [module, hash] = name.split('.');
oldHashes[module] = hash;
});
oldHashes = generateFileHashMap();
})
.then(() => writeFile('src/app/app.component.css', 'h1 { margin: 5px; }'))
.then(() => writeFile('src/styles.css', 'body { background: red; }'))
.then(() => ng('build', '--prod'))
.then(() => {
fs.readdirSync('./dist')
.forEach(name => {
if (!name.match(/(main|inline|styles|\d+)\.[a-z0-9]+\.bundle\.(js|css)/)) {
return;
}

const [module, hash] = name.split('.');
newHashes[module] = hash;
});
newHashes = generateFileHashMap();
})
.then(() => {
console.log(' Validating hashes...');
console.log(` Old hashes: ${JSON.stringify(oldHashes)}`);
console.log(` New hashes: ${JSON.stringify(newHashes)}`);

Object.keys(oldHashes)
.forEach(module => {
if (oldHashes[module] == newHashes[module]) {
throw new Error(`Module "${module}" did not change hash (${oldHashes[module]})...`);
}
});
validateHashes(oldHashes, newHashes, []);
oldHashes = newHashes;
})
.then(() => writeFile('src/styles.css', 'body { background: blue; }'))
.then(() => ng('build', '--prod'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['styles']);
oldHashes = newHashes;
})
.then(() => writeFile('src/app/app.component.css', 'h1 { margin: 10px; }'))
.then(() => ng('build', '--prod'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['inline', 'main']);
oldHashes = newHashes;
})
.then(() => addImportToModule(
'src/app/lazy/lazy.module.ts', 'ReactiveFormsModule', '@angular/forms'))
.then(() => ng('build', '--prod'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['inline', '0']);
});
}