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

fix: require is not a function #65

Merged
merged 3 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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