Skip to content

Commit

Permalink
feat: remove config and scripts dependency on process.cwd
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
swashata committed Oct 4, 2018
1 parent 8245033 commit 40c3e0a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion packages/scripts/src/config/CreateWebpackConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { WebpackConfigHelper } from './WebpackConfigHelper';
export class CreateWebpackConfig {
private projectConfig: ProjectConfig;
private serverConfig: ServerConfig;
private cwd: string;
private isDev: boolean;

/**
Expand All @@ -27,6 +28,7 @@ export class CreateWebpackConfig {
constructor(
projectConfig: ProjectConfig,
serverConfig: ServerConfig,
cwd: string,
isDev: boolean = true
) {
// Create final configuration
Expand All @@ -39,7 +41,7 @@ export class CreateWebpackConfig {
...serverConfigDefault,
...serverConfig,
};

this.cwd = cwd;
this.isDev = isDev;
}

Expand Down Expand Up @@ -104,6 +106,7 @@ export class CreateWebpackConfig {
optimizeSplitChunks,
outputPath,
},
this.cwd,
this.isDev
);

Expand Down
16 changes: 14 additions & 2 deletions packages/scripts/src/config/WebpackConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface Config {
}

interface CommonWebpackConfig {
context: webpack.Configuration['context'];
devtool: webpack.Configuration['devtool'];
target: webpack.Configuration['target'];
watch: webpack.Configuration['watch'];
Expand All @@ -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.
Expand All @@ -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';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/scripts/src/config/project.config.default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path';
import webpack from 'webpack';

// Export common interfaces
Expand Down Expand Up @@ -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?
Expand Down
9 changes: 8 additions & 1 deletion packages/scripts/src/scripts/Build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -20,6 +26,7 @@ export class Build {
const config = new CreateWebpackConfig(
this.projectConfig,
this.serverConfig,
this.cwd,
false
);
const compiler = webpack(
Expand Down
9 changes: 8 additions & 1 deletion packages/scripts/src/scripts/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -44,6 +50,7 @@ export class Server {
const webpackConfig = new CreateWebpackConfig(
this.projectConfig,
this.serverConfig,
this.cwd,
true
).getConfig();
// Init middleware and stuff
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 40c3e0a

Please sign in to comment.