Skip to content

Commit

Permalink
fix(@angular/cli): allow node_modules to be linked somewhere else.
Browse files Browse the repository at this point in the history
Fixes #6499.
  • Loading branch information
hansl committed May 31, 2017
1 parent bbe3a35 commit 9c7a7ea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 9 additions & 2 deletions packages/@angular/cli/models/webpack-configs/browser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs';
import * as webpack from 'webpack';
import * as path from 'path';
const HtmlWebpackPlugin = require('html-webpack-plugin');
Expand All @@ -24,14 +25,20 @@ export function getBrowserConfig(wco: WebpackConfigOptions) {
if (buildOptions.vendorChunk) {
// Separate modules from node_modules into a vendor chunk.
const nodeModules = path.resolve(projectRoot, 'node_modules');
// Resolves all symlink to get the actual node modules folder.
const realNodeModules = fs.realpathSync(nodeModules);
// --aot puts the generated *.ngfactory.ts in src/$$_gendir/node_modules.
const genDirNodeModules = path.resolve(appRoot, '$$_gendir', 'node_modules');

extraPlugins.push(new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
chunks: ['main'],
minChunks: (module: any) => module.resource &&
(module.resource.startsWith(nodeModules) || module.resource.startsWith(genDirNodeModules))
minChunks: (module: any) => {
return module.resource
&& ( module.resource.startsWith(nodeModules)
|| module.resource.startsWith(genDirNodeModules)
|| module.resource.startsWith(realNodeModules));
}
}));
}

Expand Down
4 changes: 3 additions & 1 deletion packages/@angular/cli/tasks/eject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ const pree2eNpmScript = `webdriver-manager update --standalone false --gecko fal
class JsonWebpackSerializer {
public imports: {[name: string]: string[]} = {};
public variableImports: {[name: string]: string} = {
'path': 'path'
'fs': 'fs',
'path': 'path',
};
public variables: {[name: string]: string} = {
'nodeModules': `path.join(process.cwd(), 'node_modules')`,
'realNodeModules': `fs.realpathSync(nodeModules)`,
'genDirNodeModules':
`path.join(process.cwd(), '${this._appRoot}', '$$_gendir', 'node_modules')`,
};
Expand Down
22 changes: 22 additions & 0 deletions tests/e2e/tests/build/vendor-chunk-symlink-node-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {ng} from '../../utils/process';
import {deleteFile, expectFileToExist, moveFile, symlinkFile} from '../../utils/fs';


// THIS TEST REQUIRES TO MOVE NODE_MODULES AND MOVE IT BACK.
export default function() {
return Promise.resolve()
.then(() => moveFile('node_modules', '../node_modules'))
.then(() => symlinkFile('../node_modules', 'node_modules', 'dir'))
.then(() => ng('build'))
.then(() => expectFileToExist('dist/vendor.bundle.js'))
.then(() => expectFileToExist('dist/vendor.bundle.js.map'))
// Cleanup
.then(() => {
return deleteFile('node_modules')
.then(() => moveFile('../node_modules', 'node_modules'));
}, (err: any) => {
return deleteFile('node_modules')
.then(() => moveFile('../node_modules', 'node_modules'))
.then(() => { throw err; });
})
}
13 changes: 13 additions & 0 deletions tests/e2e/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export function moveFile(from: string, to: string) {
});
}


export function symlinkFile(from: string, to: string, type?: string) {
return new Promise<void>((resolve, reject) => {
fs.symlink(from, to, type, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

export function createDir(path: string) {
_recursiveMkDir(path);
}
Expand Down

0 comments on commit 9c7a7ea

Please sign in to comment.