Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
fix: require is not a function (#65)
Browse files Browse the repository at this point in the history
* add possibility to specify a function to alter webpack config

* update editConfig description and readme
  • Loading branch information
puhoy authored and Christopher Anderson committed Jan 23, 2018
1 parent 504c994 commit a70e99a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,22 @@ Usage: pack [options] <path>
Options:
-h, --help output usage information
-u, --uglify Uglify the project when webpacking
-o, --output <path> Path for output directory
-h, --help output usage information
-u, --uglify Uglify the project when webpacking
-o, --output <path> Path for output directory
-c, --copyToOutput Copy files to output directory
-e, --editConfig <path> Customize webpack config by applying function in this file
```

The `editConfig` option will let you specify a js file containing a function to alter the webpack configuration.

```
// $root/webpack.config.js
module.exports = function(config, webpack) {
config.plugins.push(new webpack.DefinePlugin({ "global.GENTLY": false }));
return config;
}
```

### funcpack.config.json
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function runCli() {
.option("-u, --uglify", "Uglify the project when webpacking")
.option("-o, --output <path>", "Path for output directory")
.option("-c, --copyToOutput", "Copy files to output directory")
.option("-e, --editConfig <path>", "Customize webpack config by applying function in this file")
.action(pack);

p.command("*", null, { noHelp: true, isDefault: true })
Expand Down Expand Up @@ -139,6 +140,7 @@ async function pack(name: string, options: any) {
try {
winston.info("Webpacking project");
await WebpackRunner.run({
editConfig: options.editConfig,
ignoredModules: config.ignoredModules,
outputPath,
projectRootPath,
Expand Down
15 changes: 14 additions & 1 deletion src/webpack-runner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as debugLib from "debug";
import * as path from "path";
import * as webpack from "webpack";
import * as winston from "winston";
import { IPackhostGeneratorOptions } from "./";
import { FileHelper } from "./utils";

Expand All @@ -12,6 +13,7 @@ export interface IWebpackRunner {
outputPath?: string;
uglify?: boolean;
ignoredModules?: string[];
editConfig?: string;
}

export class WebpackRunner {
Expand All @@ -36,7 +38,7 @@ export class WebpackRunner {
}

debug("Creating Webpack Configuration");
const config: webpack.Configuration = {
let config: webpack.Configuration = {
entry: oldPath,
externals: ignoredModules,
node: {
Expand All @@ -62,6 +64,17 @@ export class WebpackRunner {
}
}

try {
if (options.editConfig) {
const customizeFunctionPath = path.join(options.projectRootPath, options.editConfig);
const customizeFunction = require(customizeFunctionPath);
config = customizeFunction(config, webpack);
}
} catch (e) {
winston.error(e);
throw new Error("Could not apply customize function");
}

debug("Creating Webpack instance");
const compiler = webpack(config);
debug("Started webpack");
Expand Down

0 comments on commit a70e99a

Please sign in to comment.