From 40c3e0a291306cb42507841f111294082c4494fa Mon Sep 17 00:00:00 2001 From: Swashata Ghosh Date: Thu, 4 Oct 2018 20:58:43 +0530 Subject: [PATCH] feat: remove config and scripts dependency on process.cwd It should be passed as a parameter and act as context for webpack. It will also be considered the file system path, to be mapped through URL and also to put files through webpack compiler. --- packages/scripts/package.json | 1 + .../scripts/src/config/CreateWebpackConfig.ts | 5 ++++- .../scripts/src/config/WebpackConfigHelper.ts | 16 ++++++++++++++-- .../scripts/src/config/project.config.default.ts | 3 ++- packages/scripts/src/scripts/Build.ts | 9 ++++++++- packages/scripts/src/scripts/Server.ts | 9 ++++++++- yarn.lock | 2 +- 7 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/scripts/package.json b/packages/scripts/package.json index 950e5ae43..d10e592a0 100644 --- a/packages/scripts/package.json +++ b/packages/scripts/package.json @@ -29,6 +29,7 @@ "babel-loader": "^8.0.2", "browser-sync": "^2.24.7", "clean-webpack-plugin": "^0.1.19", + "commander": "^2.18.0", "cross-env": "^5.2.0", "css-loader": "^1.0.0", "mini-css-extract-plugin": "^0.4.3", diff --git a/packages/scripts/src/config/CreateWebpackConfig.ts b/packages/scripts/src/config/CreateWebpackConfig.ts index 40f01358d..24c317350 100644 --- a/packages/scripts/src/config/CreateWebpackConfig.ts +++ b/packages/scripts/src/config/CreateWebpackConfig.ts @@ -15,6 +15,7 @@ import { WebpackConfigHelper } from './WebpackConfigHelper'; export class CreateWebpackConfig { private projectConfig: ProjectConfig; private serverConfig: ServerConfig; + private cwd: string; private isDev: boolean; /** @@ -27,6 +28,7 @@ export class CreateWebpackConfig { constructor( projectConfig: ProjectConfig, serverConfig: ServerConfig, + cwd: string, isDev: boolean = true ) { // Create final configuration @@ -39,7 +41,7 @@ export class CreateWebpackConfig { ...serverConfigDefault, ...serverConfig, }; - + this.cwd = cwd; this.isDev = isDev; } @@ -104,6 +106,7 @@ export class CreateWebpackConfig { optimizeSplitChunks, outputPath, }, + this.cwd, this.isDev ); diff --git a/packages/scripts/src/config/WebpackConfigHelper.ts b/packages/scripts/src/config/WebpackConfigHelper.ts index 3b78d03c1..1ed0f7afd 100644 --- a/packages/scripts/src/config/WebpackConfigHelper.ts +++ b/packages/scripts/src/config/WebpackConfigHelper.ts @@ -28,6 +28,7 @@ interface Config { } interface CommonWebpackConfig { + context: webpack.Configuration['context']; devtool: webpack.Configuration['devtool']; target: webpack.Configuration['target']; watch: webpack.Configuration['watch']; @@ -41,6 +42,10 @@ export class WebpackConfigHelper { private file: FileConfig; private isDev: boolean; private config: Config; + /** + * Context directory, from where we read the stuff and put stuff. + */ + private cwd: string; /** * Simulated NODE_ENV string, used internally and defined * in webpack with webpack.DefinePlugin. @@ -50,9 +55,15 @@ export class WebpackConfigHelper { /** * Create an instance of GetEntryAndOutput class. */ - constructor(file: FileConfig, config: Config, isDev: boolean = true) { + constructor( + file: FileConfig, + config: Config, + cwd: string, + isDev: boolean = true + ) { this.file = file; this.config = config; + this.cwd = cwd; this.isDev = isDev; if (isDev) { this.env = 'development'; @@ -134,7 +145,7 @@ export class WebpackConfigHelper { // of this configuration object. // Also here we assume, user has passed in the correct `relative` // path for `outputPath`. Otherwise this will break. - path: path.join(process.cwd(), outputPath, outputInnerDir), + path: path.join(this.cwd, outputPath, outputInnerDir), filename, // leave blank because we would handle with free variable // __webpack_public_path__ in runtime. @@ -337,6 +348,7 @@ ${bannerConfig.credit ? creditNote : ''} */ public getCommon(): CommonWebpackConfig { return { + context: this.cwd, devtool: this.isDev ? 'inline-source-map' : 'source-map', target: 'web', watch: this.isDev, diff --git a/packages/scripts/src/config/project.config.default.ts b/packages/scripts/src/config/project.config.default.ts index 45a76d6c0..b365389e3 100644 --- a/packages/scripts/src/config/project.config.default.ts +++ b/packages/scripts/src/config/project.config.default.ts @@ -1,4 +1,3 @@ -import path from 'path'; import webpack from 'webpack'; // Export common interfaces @@ -95,6 +94,8 @@ export const projectConfigDefault: ProjectConfig = { // }, // If has more length, then multi-compiler ], + // Output path relative to the context directory + // We need relative path here, else, we can not map to publicPath outputPath: 'dist', // Project specific config // Needs react? diff --git a/packages/scripts/src/scripts/Build.ts b/packages/scripts/src/scripts/Build.ts index 0fb94b5d3..719f9e16c 100644 --- a/packages/scripts/src/scripts/Build.ts +++ b/packages/scripts/src/scripts/Build.ts @@ -6,10 +6,16 @@ import { ServerConfig } from '../config/server.config.default'; export class Build { private projectConfig: ProjectConfig; private serverConfig: ServerConfig; + private cwd: string; - constructor(projectConfig: ProjectConfig, serverConfig: ServerConfig) { + constructor( + projectConfig: ProjectConfig, + serverConfig: ServerConfig, + cwd: string + ) { this.projectConfig = projectConfig; this.serverConfig = serverConfig; + this.cwd = cwd; } /** @@ -20,6 +26,7 @@ export class Build { const config = new CreateWebpackConfig( this.projectConfig, this.serverConfig, + this.cwd, false ); const compiler = webpack( diff --git a/packages/scripts/src/scripts/Server.ts b/packages/scripts/src/scripts/Server.ts index 3edbcd9c4..c4d020670 100644 --- a/packages/scripts/src/scripts/Server.ts +++ b/packages/scripts/src/scripts/Server.ts @@ -11,6 +11,7 @@ import { ServerConfig } from '../config/server.config.default'; export class Server { private projectConfig: ProjectConfig; private serverConfig: ServerConfig; + private cwd: string; private isServing: boolean = false; @@ -23,9 +24,14 @@ export class Server { * @param projectConfig Project configuration as recovered from user directory. * @param serverConfig Server configuration as recovered from user directory. */ - constructor(projectConfig: ProjectConfig, serverConfig: ServerConfig) { + constructor( + projectConfig: ProjectConfig, + serverConfig: ServerConfig, + cwd: string + ) { this.projectConfig = projectConfig; this.serverConfig = serverConfig; + this.cwd = cwd; } /** @@ -44,6 +50,7 @@ export class Server { const webpackConfig = new CreateWebpackConfig( this.projectConfig, this.serverConfig, + this.cwd, true ).getConfig(); // Init middleware and stuff diff --git a/yarn.lock b/yarn.lock index 95ed6882d..acc9d466a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2635,7 +2635,7 @@ combined-stream@~1.0.5, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -commander@^2.11.0, commander@^2.12.1, commander@^2.15.1, commander@^2.2.0, commander@^2.8.1: +commander@^2.11.0, commander@^2.12.1, commander@^2.15.1, commander@^2.18.0, commander@^2.2.0, commander@^2.8.1: version "2.18.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.18.0.tgz#2bf063ddee7c7891176981a2cc798e5754bc6970"