Skip to content

Commit

Permalink
fix: issues with cleanWebPackPlugin
Browse files Browse the repository at this point in the history
By default, the root is set to __dirname, not process.cwd(). So
we pass in this.cwd from the helper class to make it always right.
  • Loading branch information
swashata committed Oct 7, 2018
1 parent 1cc53c3 commit bc38690
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/scripts/@types/cleanWebpackPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'clean-webpack-plugin' {
import * as webpack from 'webpack';
export default class cleanWebpackPlugin extends webpack.Plugin {
constructor(config:string[])
constructor(config:string[], options: {[x:string]: boolean|string|string[]})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ Array [
"options": Object {
"allowExternal": false,
"dry": false,
"root": "/Volumes/Development/PersonalProjects/wp-webpack-script/packages/scripts/src/config",
"root": "/foo/bar",
"verbose": false,
},
"paths": Array [
"dist",
"/foo/bar/dist/config1",
],
},
MiniCssExtractPlugin {
Expand Down Expand Up @@ -176,11 +176,11 @@ Array [
"options": Object {
"allowExternal": false,
"dry": false,
"root": "/Volumes/Development/PersonalProjects/wp-webpack-script/packages/scripts/src/config",
"root": "/foo/bar",
"verbose": false,
},
"paths": Array [
"dist",
"/foo/bar/dist/config1",
],
},
MiniCssExtractPlugin {
Expand Down
20 changes: 15 additions & 5 deletions packages/scripts/src/config/WebpackConfigHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface CommonWebpackConfig {
* A helper class to get different configuration of webpack.
*/
export class WebpackConfigHelper {
public readonly outputInnerDir: string;
public readonly outputPath: string;
private file: FileConfig;
private isDev: boolean;
private config: WebpackConfigHelperConfig;
Expand Down Expand Up @@ -71,6 +73,13 @@ export class WebpackConfigHelper {
} else {
this.env = 'production';
}

// Create the outputPath, because we would be needing that
const { outputPath } = this.config;
// and file
const { name } = this.file;
this.outputInnerDir = slugify(name, { lower: true });
this.outputPath = path.join(this.cwd, outputPath, this.outputInnerDir);
}

/**
Expand Down Expand Up @@ -137,8 +146,7 @@ export class WebpackConfigHelper {
// Destucture stuff we need from config
const { type, slug, host, port, outputPath } = this.config;
// and file
const { name, filename } = this.file;
const outputInnerDir: string = slugify(name, { lower: true });
const { filename } = this.file;
// Assuming it is production
const output: webpack.Output = {
// Here we create a directory inside the user provided outputPath
Expand All @@ -148,7 +156,7 @@ export class WebpackConfigHelper {
// path for `outputPath`. Otherwise this will break.
// We do not use path.resolve, because we expect outputPath to be
// relative. @todo: create a test here
path: path.join(this.cwd, outputPath, outputInnerDir),
path: this.outputPath,
filename,
// leave blank because we would handle with free variable
// __webpack_public_path__ in runtime.
Expand All @@ -162,7 +170,9 @@ export class WebpackConfigHelper {
// Maybe we can have a `prefix` in Config, but let's not do that
// right now.
output.publicPath = `//${host ||
'localhost'}:${port}/wp-content/${contentDir}/${slug}/${outputPath}/${outputInnerDir}`;
'localhost'}:${port}/wp-content/${contentDir}/${slug}/${outputPath}/${
this.outputInnerDir
}`;
}

return output;
Expand All @@ -180,7 +190,7 @@ export class WebpackConfigHelper {
'process.env.BABEL_ENV': this.env,
}),
// Clean dist directory
new cleanWebpackPlugin(['dist']),
new cleanWebpackPlugin([this.outputPath], { root: this.cwd }),
// Initiate mini css extract
new miniCssExtractPlugin({
filename: '[name].css',
Expand Down

0 comments on commit bc38690

Please sign in to comment.