From 30fd94273c99ac84e09af86840bf8a590aa229db Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Wed, 20 Jul 2016 01:39:43 +0100 Subject: [PATCH] fix(build): re-add support for sourceDir (#1378) --- .../ng2/blueprints/ng2/files/__path__/test.ts | 2 +- .../ng2/files/__path__/tsconfig.json | 2 +- addon/ng2/models/webpack-build-common.ts | 13 ++++---- addon/ng2/models/webpack-build-development.ts | 5 +-- addon/ng2/models/webpack-build-mobile.ts | 11 ++++--- addon/ng2/models/webpack-build-production.ts | 5 +-- addon/ng2/models/webpack-build-test.ts | 13 ++++---- addon/ng2/models/webpack-config.ts | 12 ++++--- addon/ng2/tasks/serve-webpack.ts | 3 +- addon/ng2/tasks/{test.js => test.ts} | 31 +++++++++---------- 10 files changed, 51 insertions(+), 46 deletions(-) rename addon/ng2/tasks/{test.js => test.ts} (63%) diff --git a/addon/ng2/blueprints/ng2/files/__path__/test.ts b/addon/ng2/blueprints/ng2/files/__path__/test.ts index e61005825b11..6ba450cbd3dd 100644 --- a/addon/ng2/blueprints/ng2/files/__path__/test.ts +++ b/addon/ng2/blueprints/ng2/files/__path__/test.ts @@ -29,7 +29,7 @@ Promise.all([ ); }); -let testContext: any = require.context('../src', true, /\.spec\.ts/); +let testContext: any = require.context('./', true, /\.spec\.ts/); function requireAll(requireContext: any) { return requireContext.keys().map(requireContext); } diff --git a/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json b/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json index e0831213e64c..5acc3c417061 100644 --- a/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json +++ b/addon/ng2/blueprints/ng2/files/__path__/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "baseUrl":"./src", + "baseUrl":"./", "declaration": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, diff --git a/addon/ng2/models/webpack-build-common.ts b/addon/ng2/models/webpack-build-common.ts index feb71aba1f3d..4d3e5ca27513 100644 --- a/addon/ng2/models/webpack-build-common.ts +++ b/addon/ng2/models/webpack-build-common.ts @@ -1,23 +1,24 @@ import * as webpack from 'webpack'; import {LoaderConfig, PathsPlugin} from '../utilities/ts-path-mappings-webpack-plugin'; +import { CliConfig } from './config'; const path = require('path'); const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin; const CopyWebpackPlugin = require('copy-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin'); -export function getWebpackCommonConfig(projectRoot: string) { +export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) { const awesomeTypescriptLoaderConfig: LoaderConfig | any = { useWebpackText: true, useForkChecker: true, - tsconfig: path.resolve(projectRoot, './src/tsconfig.json') + tsconfig: path.resolve(projectRoot, `./${sourceDir}/tsconfig.json`) } return { devtool: 'inline-source-map', resolve: { extensions: ['', '.ts', '.js'], - root: path.resolve(projectRoot, './src'), + root: path.resolve(projectRoot, `./${sourceDir}`), moduleDirectories: ['node_modules'], plugins: [ new PathsPlugin(awesomeTypescriptLoaderConfig); @@ -25,8 +26,8 @@ export function getWebpackCommonConfig(projectRoot: string) { }, context: path.resolve(__dirname, './'), entry: { - main: [path.resolve(projectRoot, './src/main.ts')], - polyfills: path.resolve(projectRoot, './src/polyfills.ts') + main: [path.resolve(projectRoot, `./${sourceDir}/main.ts`)], + polyfills: path.resolve(projectRoot, `./${sourceDir}/polyfills.ts`) }, output: { path: path.resolve(projectRoot, './dist'), @@ -69,7 +70,7 @@ export function getWebpackCommonConfig(projectRoot: string) { plugins: [ new ForkCheckerPlugin(), new HtmlWebpackPlugin({ - template: path.resolve(projectRoot, 'src/index.html'), + template: path.resolve(projectRoot, `./${sourceDir}/index.html`), chunksSortMode: 'dependency' }), new webpack.optimize.CommonsChunkPlugin({ diff --git a/addon/ng2/models/webpack-build-development.ts b/addon/ng2/models/webpack-build-development.ts index fc37a5d92b45..61932014d254 100644 --- a/addon/ng2/models/webpack-build-development.ts +++ b/addon/ng2/models/webpack-build-development.ts @@ -1,6 +1,7 @@ +import { CliConfig } from './config'; const path = require('path') -export const getWebpackDevConfigPartial = function(projectRoot: string) { +export const getWebpackDevConfigPartial = function(projectRoot: string, sourceDir: string) { return { debug: true, devtool: 'cheap-module-source-map', @@ -13,7 +14,7 @@ export const getWebpackDevConfigPartial = function(projectRoot: string) { tslint: { emitErrors: false, failOnHint: false, - resourcePath: path.resolve(projectRoot, './src') + resourcePath: path.resolve(projectRoot, `./${sourceDir}`) }, node: { global: 'window', diff --git a/addon/ng2/models/webpack-build-mobile.ts b/addon/ng2/models/webpack-build-mobile.ts index 397fa412f68c..1480a4eebf37 100644 --- a/addon/ng2/models/webpack-build-mobile.ts +++ b/addon/ng2/models/webpack-build-mobile.ts @@ -3,18 +3,19 @@ import * as path from 'path'; import * as OfflinePlugin from 'offline-plugin'; import * as CopyWebpackPlugin from 'copy-webpack-plugin'; import { PrerenderWebpackPlugin } from '../utilities/prerender-webpack-plugin.ts'; +import { CliConfig } from './config'; -export const getWebpackMobileConfigPartial = function (projectRoot: string) { +export const getWebpackMobileConfigPartial = function (projectRoot: string, sourceDir: string) { return { plugins: [ new CopyWebpackPlugin([ - {from: path.resolve(projectRoot, './src/icons'), to: path.resolve(projectRoot, './dist/icons')}, - {from: path.resolve(projectRoot, './src/manifest.webapp'), to: path.resolve(projectRoot, './dist')} + {from: path.resolve(projectRoot, `./${sourceDir}/icons`), to: path.resolve(projectRoot, './dist/icons')}, + {from: path.resolve(projectRoot, `./${sourceDir}/manifest.webapp`), to: path.resolve(projectRoot, './dist')} ]), new PrerenderWebpackPlugin({ templatePath: 'index.html', - configPath: path.resolve(projectRoot, './src/main-app-shell.ts'), - appPath: path.resolve(projectRoot, './src') + configPath: path.resolve(projectRoot, `./${sourceDir}/main-app-shell.ts`), + appPath: path.resolve(projectRoot, `./${sourceDir}`) }) ] } diff --git a/addon/ng2/models/webpack-build-production.ts b/addon/ng2/models/webpack-build-production.ts index 60028dca1917..e5c53d5648e6 100644 --- a/addon/ng2/models/webpack-build-production.ts +++ b/addon/ng2/models/webpack-build-production.ts @@ -1,11 +1,12 @@ import * as webpack from 'webpack'; +import { CliConfig } from './config'; const path = require('path'); const webpackMerge = require('webpack-merge'); // used to merge webpack configs const WebpackMd5Hash = require('webpack-md5-hash'); const CompressionPlugin = require("compression-webpack-plugin"); -export const getWebpackProdConfigPartial = function(projectRoot: string) { +export const getWebpackProdConfigPartial = function(projectRoot: string, sourceDir: string) { return { debug: false, devtool: 'source-map', @@ -36,7 +37,7 @@ export const getWebpackProdConfigPartial = function(projectRoot: string) { tslint: { emitErrors: true, failOnHint: true, - resourcePath: path.resolve(projectRoot, './src') + resourcePath: path.resolve(projectRoot, `./${sourceDir}`) }, htmlLoader: { minimize: true, diff --git a/addon/ng2/models/webpack-build-test.ts b/addon/ng2/models/webpack-build-test.ts index 455b5960c6c0..42ee2bf45dbc 100644 --- a/addon/ng2/models/webpack-build-test.ts +++ b/addon/ng2/models/webpack-build-test.ts @@ -1,17 +1,18 @@ import * as webpack from 'webpack'; +import { CliConfig } from './config'; const path = require('path'); -export const getWebpackTestConfig = function(projectRoot: string) { +export const getWebpackTestConfig = function(projectRoot: string, sourceDir: string) { return { devtool: 'inline-source-map', context: path.resolve(__dirname, './'), resolve: { extensions: ['', '.ts', '.js'], - root: path.resolve(projectRoot, './src') + root: path.resolve(projectRoot, `./${sourceDir}`) }, entry: { - test: path.resolve(projectRoot, './src/test.ts') + test: path.resolve(projectRoot, `./${sourceDir}/test.ts`) }, output: { path: './dist.test', @@ -43,7 +44,7 @@ export const getWebpackTestConfig = function(projectRoot: string) { loader: 'awesome-typescript-loader', query: { useWebpackText: true, - tsconfig: path.resolve(projectRoot, './src/tsconfig.json'), + tsconfig: path.resolve(projectRoot, `./${sourceDir}/tsconfig.json`), module: "commonjs", target: "es5", useForkChecker: true, @@ -62,7 +63,7 @@ export const getWebpackTestConfig = function(projectRoot: string) { { test: /\.less$/, loaders: ['raw-loader', 'postcss-loader', 'less-loader'] }, { test: /\.scss$/, loaders: ['raw-loader', 'postcss-loader', 'sass-loader'] }, { test: /\.(jpg|png)$/, loader: 'url-loader?limit=128000'}, - { test: /\.html$/, loader: 'raw-loader', exclude: [path.resolve(projectRoot, 'src/index.html')] } + { test: /\.html$/, loader: 'raw-loader', exclude: [path.resolve(projectRoot, `./${sourceDir}/index.html`)] } ], postLoaders: [ { @@ -77,7 +78,7 @@ export const getWebpackTestConfig = function(projectRoot: string) { tslint: { emitErrors: false, failOnHint: false, - resourcePath: 'src' + resourcePath: `./${sourceDir}` }, node: { global: 'window', diff --git a/addon/ng2/models/webpack-config.ts b/addon/ng2/models/webpack-config.ts index 95e3ddb3d0d6..5dc9541bb333 100644 --- a/addon/ng2/models/webpack-config.ts +++ b/addon/ng2/models/webpack-config.ts @@ -24,13 +24,15 @@ export class NgCliWebpackConfig { private webpackMobileProdConfigPartial: any; constructor(public ngCliProject: any, public environment: string) { - this.webpackBaseConfig = getWebpackCommonConfig(this.ngCliProject.root); - this.webpackDevConfigPartial = getWebpackDevConfigPartial(this.ngCliProject.root); - this.webpackProdConfigPartial = getWebpackProdConfigPartial(this.ngCliProject.root); + const sourceDir = CliConfig.fromProject().defaults.sourceDir; + + this.webpackBaseConfig = getWebpackCommonConfig(this.ngCliProject.root, sourceDir); + this.webpackDevConfigPartial = getWebpackDevConfigPartial(this.ngCliProject.root, sourceDir); + this.webpackProdConfigPartial = getWebpackProdConfigPartial(this.ngCliProject.root, sourceDir); if (CliConfig.fromProject().apps[0].mobile){ - this.webpackMobileConfigPartial = getWebpackMobileConfigPartial(this.ngCliProject.root); - this.webpackMobileProdConfigPartial = getWebpackMobileProdConfigPartial(this.ngCliProject.root); + this.webpackMobileConfigPartial = getWebpackMobileConfigPartial(this.ngCliProject.root, sourceDir); + this.webpackMobileProdConfigPartial = getWebpackMobileProdConfigPartial(this.ngCliProject.root, sourceDir); this.webpackBaseConfig = webpackMerge(this.webpackBaseConfig, this.webpackMobileConfigPartial); this.webpackProdConfigPartial = webpackMerge(this.webpackProdConfigPartial, this.webpackMobileProdConfigPartial); } diff --git a/addon/ng2/tasks/serve-webpack.ts b/addon/ng2/tasks/serve-webpack.ts index 315cc3d163ad..176af5388902 100644 --- a/addon/ng2/tasks/serve-webpack.ts +++ b/addon/ng2/tasks/serve-webpack.ts @@ -1,6 +1,7 @@ import {webpackDevServerOutputOptions} from '../models/'; import {NgCliWebpackConfig} from '../models/webpack-config'; import {ServeTaskOptions} from '../commands/serve'; +import { CliConfig } from '../models/config'; const path = require('path'); const chalk = require('chalk'); @@ -32,7 +33,7 @@ module.exports = Task.extend({ })); const webpackDevServerConfiguration: IWebpackDevServerConfigurationOptions = { - contentBase: path.resolve(this.project.root, './src'), + contentBase: path.resolve(this.project.root, `./${CliConfig.fromProject().defaults.sourceDir}`), historyApiFallback: true, stats: webpackDevServerOutputOptions, inline: true, diff --git a/addon/ng2/tasks/test.js b/addon/ng2/tasks/test.ts similarity index 63% rename from addon/ng2/tasks/test.js rename to addon/ng2/tasks/test.ts index dcf28c2f7e55..fd8885e28b62 100644 --- a/addon/ng2/tasks/test.js +++ b/addon/ng2/tasks/test.ts @@ -1,25 +1,22 @@ -/* jshint node: true */ -'use strict'; - -var Promise = require('ember-cli/lib/ext/promise'); -var Task = require('ember-cli/lib/models/task'); -var path = require('path'); -var webpackTestConfig = require('../models/webpack-build-test').getWebpackTestConfig; +const Promise = require('ember-cli/lib/ext/promise'); +const Task = require('ember-cli/lib/models/task'); +const path = require('path'); +const webpackTestConfig = require('../models/webpack-build-test').getWebpackTestConfig; // require dependencies within the target project function requireDependency(root, moduleName) { - var packageJson = require(path.join(root, 'node_modules', moduleName, 'package.json')); - var main = path.normalize(packageJson.main); + const packageJson = require(path.join(root, 'node_modules', moduleName, 'package.json')); + const main = path.normalize(packageJson.main); return require(path.join(root, 'node_modules', moduleName, main)); } -module.exports = Task.extend({ +module.exports = Task.extend({ run: function (options) { - var projectRoot = this.project.root; + const projectRoot = this.project.root; return new Promise((resolve) => { - - var karma = requireDependency(projectRoot, 'karma'); - var karmaConfig = path.join(projectRoot, this.project.ngConfig.test.karma.config); + const karma = requireDependency(projectRoot, 'karma'); + const karmaConfig = path.join(projectRoot, this.project.ngConfig.test.karma.config); + const testFile = `./${this.project.ngConfig.defaults.sourceDir}/test.ts`; options.plugins = [ require('karma-webpack'), @@ -35,8 +32,8 @@ module.exports = Task.extend({ // Add those details here. // Single test entry file. Will run the test.ts bundle and track it. - options.files = [{ pattern: './src/test.ts', watched: false }]; - options.preprocessors = { './src/test.ts': ['webpack','sourcemap'] }; + options.files = [{ pattern: testFile, watched: false }]; + options.preprocessors = { [testFile]: ['webpack','sourcemap'] }; options.webpack = webpackTestConfig(projectRoot); options.webpackMiddleware = { noInfo: true, // Hide webpack output because its noisy. @@ -60,7 +57,7 @@ module.exports = Task.extend({ options.configFile = karmaConfig; // :shipit: - var karmaServer = new karma.Server(options, resolve); + const karmaServer = new karma.Server(options, resolve); karmaServer.start(); }); }