diff --git a/lib/packages.js b/lib/packages.js index 69b9b378d821..8181203af068 100644 --- a/lib/packages.js +++ b/lib/packages.js @@ -1,26 +1,40 @@ 'use strict'; const fs = require('fs'); +const glob = require('glob'); const path = require('path'); const packageRoot = path.join(__dirname, '../packages'); // All the supported packages. Go through the packages directory and create a map of // name => fullPath. -const packages = fs.readdirSync(packageRoot) - .map(pkgName => ({ name: pkgName, root: path.join(packageRoot, pkgName) })) - .filter(pkg => fs.statSync(pkg.root).isDirectory()) - .reduce((packages, pkg) => { - let pkgJson = JSON.parse(fs.readFileSync(path.join(pkg.root, 'package.json'), 'utf8')); - let name = pkgJson['name']; - packages[name] = { - dist: path.join(__dirname, '../dist', pkg.name), - packageJson: path.join(pkg.root, 'package.json'), - root: pkg.root, - relative: path.relative(path.dirname(__dirname), pkg.root), - main: path.resolve(pkg.root, 'src/index.ts') - }; - return packages; - }, {}); - -module.exports = packages; \ No newline at end of file +const packages = + glob.sync(path.join(packageRoot, '**/package.json')) + .filter(p => !p.match(/blueprints/)) + .map(pkgPath => path.relative(packageRoot, path.dirname(pkgPath))) + .map(pkgName => { + return { name: pkgName, root: path.join(packageRoot, pkgName) }; + }) + .reduce((packages, pkg) => { + let pkgJson = JSON.parse(fs.readFileSync(path.join(pkg.root, 'package.json'), 'utf8')); + let name = pkgJson['name']; + + packages[name] = { + dist: path.join(__dirname, '../dist', pkg.name), + packageJson: path.join(pkg.root, 'package.json'), + root: pkg.root, + relative: path.relative(path.dirname(__dirname), pkg.root), + main: path.resolve(pkg.root, 'src/index.ts') + }; + return packages; + }, {}); + + +module.exports = packages; + + +// If we run this from the command line, just output the list of modules neatly formatted. +if (require.main === module) { + // eslint-disable-next-line no-console + console.log(JSON.stringify(packages, null, 2)); +} diff --git a/packages/ast-tools/package.json b/packages/@angular-cli/ast-tools/package.json similarity index 100% rename from packages/ast-tools/package.json rename to packages/@angular-cli/ast-tools/package.json diff --git a/packages/ast-tools/src/ast-utils.spec.ts b/packages/@angular-cli/ast-tools/src/ast-utils.spec.ts similarity index 100% rename from packages/ast-tools/src/ast-utils.spec.ts rename to packages/@angular-cli/ast-tools/src/ast-utils.spec.ts diff --git a/packages/ast-tools/src/ast-utils.ts b/packages/@angular-cli/ast-tools/src/ast-utils.ts similarity index 100% rename from packages/ast-tools/src/ast-utils.ts rename to packages/@angular-cli/ast-tools/src/ast-utils.ts diff --git a/packages/ast-tools/src/change.spec.ts b/packages/@angular-cli/ast-tools/src/change.spec.ts similarity index 100% rename from packages/ast-tools/src/change.spec.ts rename to packages/@angular-cli/ast-tools/src/change.spec.ts diff --git a/packages/ast-tools/src/change.ts b/packages/@angular-cli/ast-tools/src/change.ts similarity index 100% rename from packages/ast-tools/src/change.ts rename to packages/@angular-cli/ast-tools/src/change.ts diff --git a/packages/ast-tools/src/index.ts b/packages/@angular-cli/ast-tools/src/index.ts similarity index 100% rename from packages/ast-tools/src/index.ts rename to packages/@angular-cli/ast-tools/src/index.ts diff --git a/packages/ast-tools/src/node.ts b/packages/@angular-cli/ast-tools/src/node.ts similarity index 100% rename from packages/ast-tools/src/node.ts rename to packages/@angular-cli/ast-tools/src/node.ts diff --git a/packages/ast-tools/src/route-utils.spec.ts b/packages/@angular-cli/ast-tools/src/route-utils.spec.ts similarity index 100% rename from packages/ast-tools/src/route-utils.spec.ts rename to packages/@angular-cli/ast-tools/src/route-utils.spec.ts diff --git a/packages/ast-tools/src/route-utils.ts b/packages/@angular-cli/ast-tools/src/route-utils.ts similarity index 100% rename from packages/ast-tools/src/route-utils.ts rename to packages/@angular-cli/ast-tools/src/route-utils.ts diff --git a/packages/ast-tools/src/spec-utils.ts b/packages/@angular-cli/ast-tools/src/spec-utils.ts similarity index 100% rename from packages/ast-tools/src/spec-utils.ts rename to packages/@angular-cli/ast-tools/src/spec-utils.ts diff --git a/packages/ast-tools/tsconfig.json b/packages/@angular-cli/ast-tools/tsconfig.json similarity index 89% rename from packages/ast-tools/tsconfig.json rename to packages/@angular-cli/ast-tools/tsconfig.json index 019f268a57b1..584a9436cc7b 100644 --- a/packages/ast-tools/tsconfig.json +++ b/packages/@angular-cli/ast-tools/tsconfig.json @@ -7,7 +7,7 @@ "moduleResolution": "node", "noEmitOnError": true, "noImplicitAny": true, - "outDir": "../../dist/ast-tools", + "outDir": "../../../dist/@angular-cli/ast-tools", "rootDir": ".", "sourceMap": true, "sourceRoot": "/", diff --git a/packages/base-href-webpack/package.json b/packages/@angular-cli/base-href-webpack/package.json similarity index 100% rename from packages/base-href-webpack/package.json rename to packages/@angular-cli/base-href-webpack/package.json diff --git a/packages/base-href-webpack/src/base-href-webpack-plugin.spec.ts b/packages/@angular-cli/base-href-webpack/src/base-href-webpack-plugin.spec.ts similarity index 100% rename from packages/base-href-webpack/src/base-href-webpack-plugin.spec.ts rename to packages/@angular-cli/base-href-webpack/src/base-href-webpack-plugin.spec.ts diff --git a/packages/base-href-webpack/src/base-href-webpack-plugin.ts b/packages/@angular-cli/base-href-webpack/src/base-href-webpack-plugin.ts similarity index 100% rename from packages/base-href-webpack/src/base-href-webpack-plugin.ts rename to packages/@angular-cli/base-href-webpack/src/base-href-webpack-plugin.ts diff --git a/packages/base-href-webpack/src/index.ts b/packages/@angular-cli/base-href-webpack/src/index.ts similarity index 100% rename from packages/base-href-webpack/src/index.ts rename to packages/@angular-cli/base-href-webpack/src/index.ts diff --git a/packages/base-href-webpack/tsconfig.json b/packages/@angular-cli/base-href-webpack/tsconfig.json similarity index 88% rename from packages/base-href-webpack/tsconfig.json rename to packages/@angular-cli/base-href-webpack/tsconfig.json index f5a0408e37ee..2a2f0f7c6e59 100644 --- a/packages/base-href-webpack/tsconfig.json +++ b/packages/@angular-cli/base-href-webpack/tsconfig.json @@ -7,7 +7,7 @@ "moduleResolution": "node", "noEmitOnError": true, "noImplicitAny": true, - "outDir": "../../dist/base-href-webpack", + "outDir": "../../../dist/@angular-cli/base-href-webpack", "rootDir": ".", "sourceMap": true, "sourceRoot": "/", diff --git a/packages/webpack/README.md b/packages/@ngtools/webpack/README.md similarity index 100% rename from packages/webpack/README.md rename to packages/@ngtools/webpack/README.md diff --git a/packages/webpack/package.json b/packages/@ngtools/webpack/package.json similarity index 100% rename from packages/webpack/package.json rename to packages/@ngtools/webpack/package.json diff --git a/packages/webpack/src/compiler_host.ts b/packages/@ngtools/webpack/src/compiler_host.ts similarity index 100% rename from packages/webpack/src/compiler_host.ts rename to packages/@ngtools/webpack/src/compiler_host.ts diff --git a/packages/webpack/src/entry_resolver.ts b/packages/@ngtools/webpack/src/entry_resolver.ts similarity index 100% rename from packages/webpack/src/entry_resolver.ts rename to packages/@ngtools/webpack/src/entry_resolver.ts diff --git a/packages/webpack/src/index.ts b/packages/@ngtools/webpack/src/index.ts similarity index 100% rename from packages/webpack/src/index.ts rename to packages/@ngtools/webpack/src/index.ts diff --git a/packages/webpack/src/loader.ts b/packages/@ngtools/webpack/src/loader.ts similarity index 100% rename from packages/webpack/src/loader.ts rename to packages/@ngtools/webpack/src/loader.ts diff --git a/packages/webpack/src/paths-plugin.ts b/packages/@ngtools/webpack/src/paths-plugin.ts similarity index 100% rename from packages/webpack/src/paths-plugin.ts rename to packages/@ngtools/webpack/src/paths-plugin.ts diff --git a/packages/webpack/src/plugin.ts b/packages/@ngtools/webpack/src/plugin.ts similarity index 100% rename from packages/webpack/src/plugin.ts rename to packages/@ngtools/webpack/src/plugin.ts diff --git a/packages/webpack/src/refactor.ts b/packages/@ngtools/webpack/src/refactor.ts similarity index 100% rename from packages/webpack/src/refactor.ts rename to packages/@ngtools/webpack/src/refactor.ts diff --git a/packages/webpack/src/reflector_host.ts b/packages/@ngtools/webpack/src/reflector_host.ts similarity index 100% rename from packages/webpack/src/reflector_host.ts rename to packages/@ngtools/webpack/src/reflector_host.ts diff --git a/packages/webpack/src/resource_loader.ts b/packages/@ngtools/webpack/src/resource_loader.ts similarity index 100% rename from packages/webpack/src/resource_loader.ts rename to packages/@ngtools/webpack/src/resource_loader.ts diff --git a/packages/webpack/src/utils.ts b/packages/@ngtools/webpack/src/utils.ts similarity index 100% rename from packages/webpack/src/utils.ts rename to packages/@ngtools/webpack/src/utils.ts diff --git a/packages/webpack/src/webpack.ts b/packages/@ngtools/webpack/src/webpack.ts similarity index 100% rename from packages/webpack/src/webpack.ts rename to packages/@ngtools/webpack/src/webpack.ts diff --git a/packages/webpack/tsconfig.json b/packages/@ngtools/webpack/tsconfig.json similarity index 78% rename from packages/webpack/tsconfig.json rename to packages/@ngtools/webpack/tsconfig.json index 68fa995ac3a7..44d39b25f634 100644 --- a/packages/webpack/tsconfig.json +++ b/packages/@ngtools/webpack/tsconfig.json @@ -7,15 +7,15 @@ "moduleResolution": "node", "noEmitOnError": true, "noImplicitAny": true, - "outDir": "../../dist/webpack", + "outDir": "../../../dist/@ngtools/webpack", "rootDir": ".", "lib": ["es2015", "es6", "dom"], "target": "es5", "sourceMap": true, "sourceRoot": "/", - "baseUrl": ".", + "baseUrl": "./", "paths": { - "@angular-cli/ast-tools": [ "../../dist/ast-tools/src" ] + "@angular-cli/ast-tools": [ "../../../dist/ast-tools/src" ] }, "typeRoots": [ "../../node_modules/@types" diff --git a/packages/angular-cli/models/find-lazy-modules.ts b/packages/angular-cli/models/find-lazy-modules.ts deleted file mode 100644 index 37c04c2f637f..000000000000 --- a/packages/angular-cli/models/find-lazy-modules.ts +++ /dev/null @@ -1,77 +0,0 @@ -import * as fs from 'fs'; -import * as glob from 'glob'; -import * as path from 'path'; -import * as ts from 'typescript'; -const resolve = require('resolve'); - -import {getSource, findNodes, getContentOfKeyLiteral} from '../utilities/ast-utils'; - - -export function findLoadChildren(tsFilePath: string): string[] { - const source = getSource(tsFilePath); - const unique: { [path: string]: boolean } = {}; - - return ( - // Find all object literals. - findNodes(source, ts.SyntaxKind.ObjectLiteralExpression) - // Get all their property assignments. - .map(node => findNodes(node, ts.SyntaxKind.PropertyAssignment)) - // Flatten into a single array (from an array of array). - .reduce((prev, curr) => curr ? prev.concat(curr) : prev, []) - // Remove every property assignment that aren't 'loadChildren'. - .filter((node: ts.PropertyAssignment) => { - const key = getContentOfKeyLiteral(source, node.name); - if (!key) { - // key is an expression, can't do anything. - return false; - } - return key == 'loadChildren'; - }) - // Remove initializers that are not files. - .filter((node: ts.PropertyAssignment) => { - return node.initializer.kind === ts.SyntaxKind.StringLiteral; - }) - // Get the full text of the initializer. - .map((node: ts.PropertyAssignment) => { - const literal = node.initializer as ts.StringLiteral; - return literal.text; - }) - // Map to the module name itself. - .map((moduleName: string) => moduleName.split('#')[0]) - // Only get unique values (there might be multiple modules from a single URL, or a module used - // multiple times). - .filter((value: string) => { - if (unique[value]) { - return false; - } else { - unique[value] = true; - return true; - } - })); -} - -export function findLazyModules(projectRoot: any): {[key: string]: string} { - const result: {[key: string]: string} = {}; - glob.sync(path.join(projectRoot, '/**/*.ts')) - .forEach(tsPath => { - findLoadChildren(tsPath).forEach(moduleName => { - let fileName = moduleName.startsWith('.') - ? path.resolve(path.dirname(tsPath), moduleName) + '.ts' - : path.resolve(projectRoot, moduleName) + '.ts'; - - if (fs.existsSync(fileName)) { - // Put the moduleName as relative to the main.ts. - result[moduleName] = fileName; - } else { - try { - let res = resolve.sync(moduleName, { basedir: projectRoot }); - if (res) { - result[moduleName] = res; - } - } catch (e) { - } - } - }); - }); - return result; -} diff --git a/packages/angular-cli/tsconfig.json b/packages/angular-cli/tsconfig.json index 9623f183e150..f771c2461b8d 100644 --- a/packages/angular-cli/tsconfig.json +++ b/packages/angular-cli/tsconfig.json @@ -20,9 +20,9 @@ ], "baseUrl": "", "paths": { - "@angular-cli/ast-tools": [ "../../dist/ast-tools/src" ], - "@angular-cli/base-href-webpack": [ "../../dist/base-href-webpack/src" ], - "@ngtools/webpack": [ "../../dist/webpack/src" ] + "@angular-cli/ast-tools": [ "../../dist/@angular-cli/ast-tools/src" ], + "@angular-cli/base-href-webpack": [ "../../dist/@angular-cli/base-href-webpack/src" ], + "@ngtools/webpack": [ "../../dist/@ngtools/webpack/src" ] } }, "include": [ diff --git a/tests/acceptance/find-lazy-module.spec.ts b/tests/acceptance/find-lazy-module.spec.ts deleted file mode 100644 index 647e3f5bf9c3..000000000000 --- a/tests/acceptance/find-lazy-module.spec.ts +++ /dev/null @@ -1,50 +0,0 @@ -import * as mockFs from 'mock-fs'; -import {stripIndents} from 'common-tags'; -import {expect} from 'chai'; -import {join} from 'path'; - -import {findLazyModules} from 'angular-cli/models/find-lazy-modules'; - - -describe('find-lazy-module', () => { - beforeEach(() => { - mockFs({ - 'project-root': { - 'fileA.ts': stripIndents` - const r1 = { - "loadChildren": "moduleA" - }; - const r2 = { - loadChildren: "moduleB" - }; - const r3 = { - 'loadChildren': 'moduleC' - }; - const r4 = { - "loadChildren": 'app/+workspace/+settings/settings.module#SettingsModule' - }; - const r5 = { - loadChildren: 'unexistentModule' - }; - `, - // Create those files too as they have to exist. - 'moduleA.ts': '', - 'moduleB.ts': '', - 'moduleC.ts': '', - 'moduleD.ts': '', - 'app': { '+workspace': { '+settings': { 'settings.module.ts': '' } } } - } - }); - }); - afterEach(() => mockFs.restore()); - - it('works', () => { - expect(findLazyModules('project-root')).to.eql({ - 'moduleA': join(process.cwd(), 'project-root', 'moduleA.ts'), - 'moduleB': join(process.cwd(), 'project-root', 'moduleB.ts'), - 'moduleC': join(process.cwd(), 'project-root', 'moduleC.ts'), - 'app/+workspace/+settings/settings.module': - join(process.cwd(), 'project-root', 'app/+workspace/+settings/settings.module.ts'), - }); - }); -}); diff --git a/tests/e2e/assets/webpack/test-app-weird/package.json b/tests/e2e/assets/webpack/test-app-weird/package.json index 3820b8170840..58992bd35b1c 100644 --- a/tests/e2e/assets/webpack/test-app-weird/package.json +++ b/tests/e2e/assets/webpack/test-app-weird/package.json @@ -11,6 +11,7 @@ "@angular/platform-browser-dynamic": "2.2.1", "@angular/platform-server": "2.2.1", "@angular/router": "3.2.1", + "@ngtools/webpack": "0.0.0", "core-js": "^2.4.1", "rxjs": "^5.0.0-beta.12", "zone.js": "^0.6.21" diff --git a/tests/e2e/assets/webpack/test-app/package.json b/tests/e2e/assets/webpack/test-app/package.json index 1aee2cceee06..1cd7c2e4be78 100644 --- a/tests/e2e/assets/webpack/test-app/package.json +++ b/tests/e2e/assets/webpack/test-app/package.json @@ -11,6 +11,7 @@ "@angular/platform-browser-dynamic": "2.2.3", "@angular/platform-server": "2.2.3", "@angular/router": "3.2.3", + "@ngtools/webpack": "0.0.0", "core-js": "^2.4.1", "rxjs": "^5.0.0-beta.12", "zone.js": "^0.6.21" diff --git a/tests/e2e/tests/packages/webpack/test.ts b/tests/e2e/tests/packages/webpack/test.ts index be4a2b1e708e..ae46afcdf084 100644 --- a/tests/e2e/tests/packages/webpack/test.ts +++ b/tests/e2e/tests/packages/webpack/test.ts @@ -1,7 +1,5 @@ -import {copyAssets} from '../../../utils/assets'; -import {exec, silentNpm} from '../../../utils/process'; -import {updateJsonFile} from '../../../utils/project'; -import {join} from 'path'; +import {createProjectFromAsset} from '../../../utils/assets'; +import {exec} from '../../../utils/process'; import {expectFileSizeToBeUnder} from '../../../utils/fs'; @@ -12,13 +10,7 @@ export default function(skipCleaning: () => void) { } return Promise.resolve() - .then(() => copyAssets('webpack/test-app')) - .then(dir => process.chdir(dir)) - .then(() => updateJsonFile('package.json', json => { - const dist = '../../../../../dist/'; - json['dependencies']['@ngtools/webpack'] = join(__dirname, dist, 'webpack'); - })) - .then(() => silentNpm('install')) + .then(() => createProjectFromAsset('webpack/test-app')) .then(() => exec('node_modules/.bin/webpack', '-p')) .then(() => expectFileSizeToBeUnder('dist/app.main.js', 400000)) .then(() => expectFileSizeToBeUnder('dist/0.app.main.js', 40000)) diff --git a/tests/e2e/tests/packages/webpack/weird.ts b/tests/e2e/tests/packages/webpack/weird.ts index 26f2eca981be..74bb15c257f3 100644 --- a/tests/e2e/tests/packages/webpack/weird.ts +++ b/tests/e2e/tests/packages/webpack/weird.ts @@ -1,7 +1,6 @@ -import {copyAssets} from '../../../utils/assets'; -import {exec, silentNpm} from '../../../utils/process'; +import {createProjectFromAsset} from '../../../utils/assets'; +import {exec} from '../../../utils/process'; import {updateJsonFile} from '../../../utils/project'; -import {join} from 'path'; import {expectFileSizeToBeUnder, expectFileToExist} from '../../../utils/fs'; import {expectToFail} from '../../../utils/utils'; @@ -13,13 +12,7 @@ export default function(skipCleaning: () => void) { } return Promise.resolve() - .then(() => copyAssets('webpack/test-app-weird')) - .then(dir => process.chdir(dir)) - .then(() => updateJsonFile('package.json', json => { - const dist = '../../../../../dist/'; - json['dependencies']['@ngtools/webpack'] = join(__dirname, dist, 'webpack'); - })) - .then(() => silentNpm('install')) + .then(() => createProjectFromAsset('webpack/test-app-weird')) .then(() => exec('node_modules/.bin/webpack', '-p')) .then(() => expectFileToExist('dist/app.main.js')) .then(() => expectFileToExist('dist/0.app.main.js')) diff --git a/tests/e2e/utils/assets.ts b/tests/e2e/utils/assets.ts index 309d25702638..ce5ace7741f9 100644 --- a/tests/e2e/utils/assets.ts +++ b/tests/e2e/utils/assets.ts @@ -3,6 +3,8 @@ import * as glob from 'glob'; import {getGlobalVariable} from './env'; import {relative} from 'path'; import {copyFile} from './fs'; +import {updateJsonFile} from './project'; +import {silentNpm} from './process'; export function assetDir(assetName: string) { @@ -27,3 +29,22 @@ export function copyAssets(assetName: string) { }) .then(() => tempRoot); } + + +export function createProjectFromAsset(assetName: string) { + const packages = require('../../../lib/packages'); + + return Promise.resolve() + .then(() => copyAssets(assetName)) + .then(dir => process.chdir(dir)) + .then(() => updateJsonFile('package.json', json => { + for (const packageName of Object.keys(packages)) { + if (json['dependencies'].hasOwnProperty(packageName)) { + json['dependencies'][packageName] = packages[packageName].dist; + } else if (json['devDependencies'].hasOwnProperty(packageName)) { + json['devDependencies'][packageName] = packages[packageName].dist; + } + } + })) + .then(() => silentNpm('install')); +} diff --git a/tests/models/find-lazy-modules.spec.ts b/tests/models/find-lazy-modules.spec.ts deleted file mode 100644 index 75f80cffd32f..000000000000 --- a/tests/models/find-lazy-modules.spec.ts +++ /dev/null @@ -1,60 +0,0 @@ -const mockFs = require('mock-fs'); -const expect = require('chai').expect; -import * as path from 'path'; -import {findLoadChildren, findLazyModules} from 'angular-cli/models/find-lazy-modules'; - -describe('find-lazy-modules', () => { - beforeEach(() => { - let mockDrive = { - 'node_modules/feature-module': { - 'package.json': '{ "main": "index.js" }', - 'index.js': '' - }, - 'src/app': { - 'app.module.ts': `RouterModule.forRoot([ - { path: 'relative', loadChildren: './feature-a/feature-a.module' }, - { path: 'absolute', loadChildren: 'src/app/feature-b/feature-b.module' }, - { path: 'module', loadChildren: 'feature-module' }, - { path: 'module2', loadChildren: 'feature-module/index.js' }, - { path: 'invalid', loadChildren: 'invalid' } - ]);`, - 'feature-a': { - 'feature-a.module.ts': '' - }, - 'feature-b': { - 'feature-b.module.ts': '' - } - } - }; - mockFs(mockDrive); - }); - afterEach(() => { - mockFs.restore(); - }); - - it('should find children', () => { - let children = findLoadChildren('src/app/app.module.ts'); - expect(children.length).to.equal(5); - expect(children.sort()).to.deep.equal([ - './feature-a/feature-a.module', - 'feature-module', - 'feature-module/index.js', - 'invalid', - 'src/app/feature-b/feature-b.module' - ]); - }); - - it('should find lazy modules', () => { - let modules = findLazyModules('.'); - expect(modules).to.deep.equal({ - './feature-a/feature-a.module': - path.join(__dirname, '../../src/app/feature-a/feature-a.module.ts'), - 'src/app/feature-b/feature-b.module': - path.join(__dirname, '../../src/app/feature-b/feature-b.module.ts'), - 'feature-module': - path.join(__dirname, '../../node_modules/feature-module/index.js'), - 'feature-module/index.js': - path.join(__dirname, '../../node_modules/feature-module/index.js') - }); - }); -});