Skip to content

Commit

Permalink
refactor: added support cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
kotarella1110 committed May 16, 2019
1 parent c581ad4 commit 7b7d12b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 124 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module.exports = {

Before preparing your application, it is bundled with webpack.

```
```shell
$ cordova build
```

Expand All @@ -87,15 +87,15 @@ Processing flow:

### Live Reload (Hot Module Replacement) the App

```
```shell
$ cordova prepare -- --livereload
$ cordova build -- --livereload
$ cordova run -- -l
```

Processing flow:

`webpack compile` > `cordova prepare` > `webpack serve` > `cordova compile`
`cordova prepare` > `webpack serve` > `cordova compile`

### Customize webpack configuration

Expand Down Expand Up @@ -135,7 +135,7 @@ By defaults:

If you want to customize `devServer` options, modify `webpack.config.js` file as follows:

```
```js
...
module.exports = {
...
Expand All @@ -144,13 +144,25 @@ module.exports = {
host: 'localhost',
port: '8000',
},
...
};
```

## CLI examples

```shell
$ cordova prepare # webpack compile
$ cordova prepare -- --livereload # webpack live reload
$ cordova build # webpack compile
$ cordova build -- --webpackConfig path/to/dir/webpack.config.babel.js # webpack compile
$ cordova build -- --w path/to/dir/webpack.config.js --livereload # webpack live reload
$ cordova run -- --w path/to/dir/webpack.config.j -l # webpack live reload
```

## TODO

- [x] Bundle with webpack before preparing.
- [x] Live Reload (Hot Module Replacement) with webpack-dev-server.
- [x] Live Reload (Hot Module Replacement) with webpack-dev-server after preparing.
- [x] Emulator
- [x] Device

Expand Down
25 changes: 22 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/rechoir": "^0.6.1",
"@types/webpack": "^4.4.31",
"@types/webpack-dev-server": "^3.1.5",
"@types/yargs-parser": "^13.0.0",
"cordova-common": "^3.1.0",
"csp-parse": "0.0.2",
"current-device": "^0.8.0",
Expand All @@ -59,7 +60,8 @@
"source-map-support": "^0.5.12",
"webpack": "^4.31.0",
"webpack-dev-server": "^3.3.1",
"webpack-inject-plugin": "^1.5.0"
"webpack-inject-plugin": "^1.5.0",
"yargs-parser": "^13.1.0"
},
"devDependencies": {
"@commitlint/cli": "^7.6.0",
Expand Down
14 changes: 11 additions & 3 deletions src/utils/webpackHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as rechoir from 'rechoir';
import findup from 'findup-sync';
import webpack from 'webpack';

export const webpackConfigPath = (cwd: string) => {
export const defaultWebpackConfigPath = (cwd: string) => {
const extensions = Object.keys(interpret.extensions);
const defaultConfigFileNames = ['webpack.config', 'webpackfile'];
const configFileRegExp = `(${defaultConfigFileNames.join(
Expand All @@ -17,8 +17,16 @@ export const webpackConfigPath = (cwd: string) => {
return configPath;
};

export const webpackConfig = (configPath: string, cwd?: string) => {
const reslvedConfigPath = cwd ? path.resolve(configPath, cwd) : configPath;
export const webpackConfig = (cwd: string, configPath?: string) => {
const reslvedConfigPath = (() => {
if (!configPath) {
return defaultWebpackConfigPath(cwd);
}
if (path.isAbsolute(configPath)) {
return path.resolve(configPath);
}
return path.resolve(cwd, configPath);
})();

// register module loaders
rechoir.prepare(interpret.extensions, reslvedConfigPath);
Expand Down
23 changes: 19 additions & 4 deletions src/webpackCompile.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import 'source-map-support/register';
import argvParse from 'yargs-parser';
import webpack from 'webpack';
import * as webpackHelper from './utils/webpackHelper';

module.exports = (ctx: any) =>
new Promise((resolve, reject) => {
const platforms = ['browser', 'android', 'ios'];
if (!platforms.some(platform => ctx.opts.platforms.includes(platform))) {
resolve();
return;
}

const argv = argvParse(ctx.opts.options.argv.join(' '));
if (argv.livereload || argv.l) {
resolve();
return;
}

const customWebpackConfig: webpack.Configuration = webpackHelper.webpackConfig(
webpackHelper.webpackConfigPath(ctx.opts.projectRoot),
ctx.opts.projectRoot,
argv.webpackConfig || argv.w,
);
const compiler = webpack(customWebpackConfig);

compiler.run((err, stats) => {
if (err) {
reject(err);
if (err && err.message) {
console.log(err.message);
reject();
return;
}

console.log(
stats.toString({
chunks: false,
Expand Down
Loading

0 comments on commit 7b7d12b

Please sign in to comment.