ko 5.2.1 → 5.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -128
- package/lib/actions/build.js +74 -0
- package/lib/actions/creator.js +42 -0
- package/lib/actions/dev.js +110 -0
- package/lib/cli.js +41 -0
- package/lib/interfaces.js +2 -0
- package/lib/utils/config.js +42 -0
- package/lib/utils/config.test.js +30 -0
- package/lib/webpack/index.js +55 -0
- package/lib/webpack/loaders/asset.js +19 -0
- package/lib/webpack/loaders/index.js +12 -0
- package/lib/webpack/loaders/script.js +59 -0
- package/lib/webpack/loaders/style.js +72 -0
- package/lib/webpack/plugins.js +61 -0
- package/package.json +43 -85
- package/{preinstall.js → scripts/preinstall.js} +6 -5
- package/.eslintignore +0 -3
- package/.eslintrc.js +0 -96
- package/.github/workflows/auto-publish.yml +0 -32
- package/.github/workflows/ci.yml +0 -67
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -5
- package/CHANGELOG.md +0 -115
- package/bin/ko-build.js +0 -30
- package/bin/ko-create.js +0 -14
- package/bin/ko-dev.js +0 -31
- package/bin/ko-init.js +0 -14
- package/bin/ko-install.js +0 -27
- package/bin/ko-lint.js +0 -18
- package/bin/ko-move.js +0 -14
- package/bin/ko-preview.js +0 -27
- package/bin/ko-swagger.js +0 -41
- package/bin/ko.js +0 -29
- package/commitlint.config.js +0 -7
- package/config/defaultPaths.js +0 -26
- package/config/lint/getEslintConf.js +0 -23
- package/config/lint/getPrettierConf.js +0 -14
- package/config/lint/index.js +0 -14
- package/config/webpack/base.js +0 -77
- package/config/webpack/devServer.js +0 -39
- package/config/webpack/index.js +0 -63
- package/config/webpack/loaders.js +0 -142
- package/config/webpack/plugins.js +0 -70
- package/constants/default.js +0 -7
- package/constants/env.js +0 -7
- package/jest.config.js +0 -44
- package/prettier.config.js +0 -8
- package/script/build.js +0 -23
- package/script/create.js +0 -31
- package/script/dev.js +0 -96
- package/script/init.js +0 -129
- package/script/install.js +0 -91
- package/script/lint.js +0 -24
- package/script/move.js +0 -38
- package/script/preview.js +0 -22
- package/script/swagger.js +0 -239
- package/template/index.html +0 -0
- package/template/pageTemplate.mustache +0 -21
- package/template/pageTemplateTS.mustache +0 -29
- package/template/restful.hbs +0 -14
- package/template/restfulTemplate.mustache +0 -12
- package/template/routerConfTemplate.mustache +0 -15
- package/template/styleTemplate.mustache +0 -3
- package/template/tsconfig.json +0 -22
- package/util/__tests__/file.test.js +0 -16
- package/util/__tests__/fileService.test.js +0 -101
- package/util/__tests__/index.test.js +0 -51
- package/util/__tests__/userConfig.test.js +0 -83
- package/util/__tests__/verifyHtml.test.js +0 -81
- package/util/createComp.js +0 -71
- package/util/createPage.js +0 -141
- package/util/defaultData.js +0 -64
- package/util/file.js +0 -9
- package/util/fileService.js +0 -55
- package/util/index.js +0 -25
- package/util/prepareUrl.js +0 -69
- package/util/program.js +0 -20
- package/util/request.js +0 -18
- package/util/stdout.js +0 -36
- package/util/userConfig.js +0 -40
- package/util/verifyHtml.js +0 -63
package/README.md
CHANGED
|
@@ -1,129 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
支持 react,vue, react-ts快速打包,让你不再纠结 webpack,vue,ts配置
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
global install with:
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
$ yarn add ko -g
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
install as devDependencies:
|
|
14
|
-
|
|
15
|
-
```
|
|
16
|
-
$ yarn add ko --dev
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Getting Started
|
|
20
|
-
|
|
21
|
-
### Use Basics
|
|
22
|
-
```text
|
|
23
|
-
1. ko init 初始项目模版文件 效果如:[create-react-app]
|
|
24
|
-
|
|
25
|
-
2. ko dll 生成动态连接库
|
|
26
|
-
|
|
27
|
-
3. ko dev 启动本地开发环境
|
|
28
|
-
|
|
29
|
-
4. ko build 编译项目到生产环境
|
|
30
|
-
|
|
31
|
-
5. ko preview 预览编译后项目静态文件
|
|
32
|
-
|
|
33
|
-
6. ko move 默认移动文件(可配置)
|
|
34
|
-
|
|
35
|
-
7. ko swagger 生成swagger接口文件(可选js/ts),用户自定义请放在restful.js
|
|
36
|
-
|
|
37
|
-
9. ko install 安装已发布npm包组件或者区块
|
|
38
|
-
|
|
39
|
-
10.ko create 创建页面或者组件目录及文件
|
|
40
|
-
|
|
41
|
-
11.ko lint 项目代码格式化
|
|
42
|
-
|
|
43
|
-
12.ko [xx] -h 查看相关命令参数使用
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### Use Advanced
|
|
47
|
-
> 自定义配置,遵循commjs语法,在项目根目录创建 ko.config.js 结构如下:
|
|
48
|
-
```js
|
|
49
|
-
module.exports = (context) => {
|
|
50
|
-
const { webpack } = context;
|
|
51
|
-
return {
|
|
52
|
-
dll:[], //自定义dll打包modules,默认dependencies中的模块包
|
|
53
|
-
server: { //本地服务配置
|
|
54
|
-
"host": "127.0.0.1",
|
|
55
|
-
"port": 3002
|
|
56
|
-
},
|
|
57
|
-
proxy: [{ //接口代理配置,解决跨域问题
|
|
58
|
-
"context": ["/auth", "/api"],
|
|
59
|
-
"target": "http://localhost:3000"
|
|
60
|
-
}],
|
|
61
|
-
move: {
|
|
62
|
-
"from"://移动目录或文件
|
|
63
|
-
"to": //目标地址
|
|
64
|
-
},
|
|
65
|
-
webpack:{ //自定义webpack配置
|
|
66
|
-
entry:{},
|
|
67
|
-
output:{},
|
|
68
|
-
modules:{}
|
|
69
|
-
...
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
> [babel polyfill](https://babeljs.io/docs/en/babel-polyfill)
|
|
76
|
-
```text
|
|
77
|
-
import "core-js/stable"
|
|
78
|
-
import "regenerator-runtime/runtime"
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
> Global Configuration
|
|
82
|
-
|
|
83
|
-
```text
|
|
84
|
-
1.在项目public目录下新建config目录,并新建文件conf.dev.js/conf.prod.js
|
|
85
|
-
|
|
86
|
-
2.conf.dev.js/conf.prod.js 示例如下:
|
|
87
|
-
|
|
88
|
-
var FRONT_CONF = {
|
|
89
|
-
LOGO: '/img/logo.png', // 项目logo
|
|
90
|
-
COPY_RIGHT: (new Date()).getFullYear() + ' 杭州玳数科技有限公司 浙ICP备15044486号-1',
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
### ko Project Directory
|
|
97
|
-
```
|
|
98
|
-
project
|
|
99
|
-
├── public // 公共资源文件(第三方资源库,项目模板文件,全局配置文件config)
|
|
100
|
-
├── src
|
|
101
|
-
│ ├── components // 公共组件
|
|
102
|
-
│ ├── layouts // 通用布局
|
|
103
|
-
│ ├── pages // 页面
|
|
104
|
-
│ └── index.js/index.tsx // 默认入口文件,可手动配置
|
|
105
|
-
├── dll // 构建后的动态链接库文件
|
|
106
|
-
├── dist // 构建后的前端静态资源
|
|
107
|
-
│ ├── index.html
|
|
108
|
-
│ ├── css
|
|
109
|
-
│ └── js
|
|
110
|
-
├── ko.config.js // 自定义配置文件
|
|
111
|
-
├── package.json // package.json
|
|
112
|
-
└── README.md // 项目说明
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### Links
|
|
116
|
-
|
|
117
|
-
- [Change Log](CHANGELOG.md)
|
|
118
|
-
- [Roadmap](https://github.com/dtux-kangaroo/ko-script/wiki/Roadmap)
|
|
119
|
-
|
|
120
|
-
### Tips
|
|
121
|
-
* v3版本以上仅支持babel-loader8
|
|
122
|
-
|
|
123
|
-
* react支持ts,并且ts,tsx和js,jsx可以共存,但是如果使用es6新语法,诸如 await,箭头函数,const等,需要将文件改为ts或者tsx
|
|
124
|
-
|
|
125
|
-
* 文件路径别名问题,如果使用tsx,webpack中别名配置将会报错,需要在tsconfig中配置path别名
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1
|
+
# ko
|
|
129
2
|
|
|
3
|
+
build & lint library
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const webpack_1 = __importDefault(require("webpack"));
|
|
7
|
+
const esbuild_loader_1 = require("esbuild-loader");
|
|
8
|
+
const creator_1 = require("./creator");
|
|
9
|
+
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
|
|
10
|
+
class Build extends creator_1.WebpackCreator {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
super(opts);
|
|
13
|
+
}
|
|
14
|
+
config() {
|
|
15
|
+
const { esbuild } = this.opts;
|
|
16
|
+
const conf = {
|
|
17
|
+
optimization: {
|
|
18
|
+
minimizer: [
|
|
19
|
+
!esbuild && new CssMinimizerPlugin(),
|
|
20
|
+
esbuild && new esbuild_loader_1.ESBuildMinifyPlugin({
|
|
21
|
+
target: 'es2015',
|
|
22
|
+
css: true
|
|
23
|
+
})
|
|
24
|
+
].filter(Boolean)
|
|
25
|
+
},
|
|
26
|
+
plugins: [
|
|
27
|
+
new webpack_1.default.optimize.SplitChunksPlugin({
|
|
28
|
+
chunks: 'async',
|
|
29
|
+
minSize: 30000,
|
|
30
|
+
maxSize: 600000,
|
|
31
|
+
minChunks: 1,
|
|
32
|
+
maxAsyncRequests: 5,
|
|
33
|
+
maxInitialRequests: 3,
|
|
34
|
+
automaticNameDelimiter: '_',
|
|
35
|
+
cacheGroups: {
|
|
36
|
+
antd: {
|
|
37
|
+
name: 'antd',
|
|
38
|
+
test: /[\\/]node_modules[\\/]antd[\\/]/,
|
|
39
|
+
chunks: 'initial',
|
|
40
|
+
},
|
|
41
|
+
lodash: {
|
|
42
|
+
name: 'lodash',
|
|
43
|
+
test: /[\\/]node_modules[\\/]lodash[\\/]/,
|
|
44
|
+
chunks: 'initial',
|
|
45
|
+
priority: -10,
|
|
46
|
+
},
|
|
47
|
+
default: {
|
|
48
|
+
minChunks: 2,
|
|
49
|
+
priority: -20,
|
|
50
|
+
reuseExistingChunk: true,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
]
|
|
55
|
+
};
|
|
56
|
+
return this.mergeConfig([this.baseConfig, conf]);
|
|
57
|
+
}
|
|
58
|
+
action() {
|
|
59
|
+
//TODO: redefine stats
|
|
60
|
+
(0, webpack_1.default)(this.config(), (error, stats) => {
|
|
61
|
+
if (stats && stats.hasErrors()) {
|
|
62
|
+
throw stats.toString({
|
|
63
|
+
logging: 'warn',
|
|
64
|
+
colors: true
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (error) {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
this.successStdout('ko build completed!');
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.default = Build;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WebpackCreator = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const webpack_merge_1 = require("webpack-merge");
|
|
9
|
+
const webpack_1 = __importDefault(require("../webpack"));
|
|
10
|
+
const config_1 = __importDefault(require("../utils/config"));
|
|
11
|
+
class Creator {
|
|
12
|
+
}
|
|
13
|
+
class WebpackCreator extends Creator {
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
super();
|
|
16
|
+
this.opts = opts;
|
|
17
|
+
this.baseConfig = this.initConfig(opts);
|
|
18
|
+
}
|
|
19
|
+
pluginsUnique(pluginNames) {
|
|
20
|
+
return (0, webpack_merge_1.unique)('plugins', pluginNames, (plugin) => plugin.constructor && plugin.constructor.name);
|
|
21
|
+
}
|
|
22
|
+
initConfig(opts) {
|
|
23
|
+
this.baseConfig = (0, webpack_1.default)(opts);
|
|
24
|
+
return (0, webpack_merge_1.mergeWithCustomize)({
|
|
25
|
+
customizeArray: this.pluginsUnique(['HtmlWebpackPlugin']),
|
|
26
|
+
})(this.baseConfig, config_1.default.userConf);
|
|
27
|
+
}
|
|
28
|
+
mergeConfig(conf) {
|
|
29
|
+
return (0, webpack_merge_1.merge)(conf);
|
|
30
|
+
}
|
|
31
|
+
successStdout(log) {
|
|
32
|
+
console.log(chalk_1.default.green(log));
|
|
33
|
+
}
|
|
34
|
+
linkStdout(link) {
|
|
35
|
+
console.log(chalk_1.default.underline(link));
|
|
36
|
+
}
|
|
37
|
+
errorStdout(log) {
|
|
38
|
+
console.log(chalk_1.default.red(log));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.WebpackCreator = WebpackCreator;
|
|
42
|
+
exports.default = Creator;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const webpack_1 = __importDefault(require("webpack"));
|
|
7
|
+
const webpack_dev_server_1 = __importDefault(require("webpack-dev-server"));
|
|
8
|
+
const detect_port_1 = __importDefault(require("detect-port"));
|
|
9
|
+
const inquirer_1 = require("inquirer");
|
|
10
|
+
const config_1 = __importDefault(require("../utils/config"));
|
|
11
|
+
const creator_1 = require("./creator");
|
|
12
|
+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
|
|
13
|
+
class Dev extends creator_1.WebpackCreator {
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
super(opts);
|
|
16
|
+
}
|
|
17
|
+
devSerConf() {
|
|
18
|
+
const userDefinedDevServerConfig = config_1.default.userConf.devServer || {};
|
|
19
|
+
const { port, host } = this.opts;
|
|
20
|
+
const defaultDevServerConfig = {
|
|
21
|
+
port,
|
|
22
|
+
host,
|
|
23
|
+
contentBase: config_1.default.defaultPaths.dist,
|
|
24
|
+
historyApiFallback: true,
|
|
25
|
+
disableHostCheck: true,
|
|
26
|
+
compress: true,
|
|
27
|
+
clientLogLevel: 'none',
|
|
28
|
+
hot: true,
|
|
29
|
+
inline: true,
|
|
30
|
+
publicPath: '/',
|
|
31
|
+
watchOptions: {
|
|
32
|
+
ignored: /node_modules/,
|
|
33
|
+
aggregateTimeout: 600,
|
|
34
|
+
},
|
|
35
|
+
open: true
|
|
36
|
+
};
|
|
37
|
+
return { ...defaultDevServerConfig, ...userDefinedDevServerConfig };
|
|
38
|
+
}
|
|
39
|
+
config() {
|
|
40
|
+
const conf = {
|
|
41
|
+
devtool: 'cheap-module-source-map',
|
|
42
|
+
plugins: [
|
|
43
|
+
new webpack_1.default.HotModuleReplacementPlugin(),
|
|
44
|
+
this.opts.analyzer && new BundleAnalyzerPlugin()
|
|
45
|
+
].filter(Boolean),
|
|
46
|
+
};
|
|
47
|
+
return this.mergeConfig([this.baseConfig, conf]);
|
|
48
|
+
}
|
|
49
|
+
async changePort(newPort, port) {
|
|
50
|
+
const question = {
|
|
51
|
+
type: 'confirm',
|
|
52
|
+
name: 'changePort',
|
|
53
|
+
message: `port: ${port} has been used,use new port ${newPort} instead?`,
|
|
54
|
+
default: true,
|
|
55
|
+
};
|
|
56
|
+
const answer = await (0, inquirer_1.prompt)([question]);
|
|
57
|
+
if (answer.changePort) {
|
|
58
|
+
return newPort;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async checkPort(port) {
|
|
65
|
+
const newPort = await (0, detect_port_1.default)(port);
|
|
66
|
+
if (newPort === port) {
|
|
67
|
+
return newPort;
|
|
68
|
+
}
|
|
69
|
+
const isInteractive = process.stdout.isTTY;
|
|
70
|
+
if (isInteractive) {
|
|
71
|
+
return this.changePort(newPort, port);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getUrlHost(host) {
|
|
75
|
+
const regex = /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*$/i;
|
|
76
|
+
return regex.test(host) ? host : `http://${host}`;
|
|
77
|
+
}
|
|
78
|
+
async action() {
|
|
79
|
+
const { port, host } = this.devSerConf();
|
|
80
|
+
const newPort = await this.checkPort(parseInt(port));
|
|
81
|
+
if (!newPort)
|
|
82
|
+
return;
|
|
83
|
+
webpack_dev_server_1.default.addDevServerEntrypoints(this.config(), this.devSerConf());
|
|
84
|
+
const compiler = (0, webpack_1.default)(this.config());
|
|
85
|
+
const devServer = new webpack_dev_server_1.default(compiler, this.devSerConf());
|
|
86
|
+
let isFirstCompile = true;
|
|
87
|
+
compiler.hooks.done.tap('done', (stats) => {
|
|
88
|
+
if (isFirstCompile) {
|
|
89
|
+
isFirstCompile = false;
|
|
90
|
+
this.successStdout('development server has been started');
|
|
91
|
+
console.log(`server starts at: ${this.linkStdout(this.getUrlHost(host) + ':' + port)}`);
|
|
92
|
+
}
|
|
93
|
+
if (stats.hasErrors()) {
|
|
94
|
+
console.log(stats.toString({
|
|
95
|
+
colors: true
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
compiler.hooks.invalid.tap('invalid', () => {
|
|
100
|
+
console.log('Compiling...');
|
|
101
|
+
});
|
|
102
|
+
devServer.listen(port, host, (err) => {
|
|
103
|
+
if (err) {
|
|
104
|
+
console.error(err);
|
|
105
|
+
process.exit(500);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.default = Dev;
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const ko_lints_1 = __importDefault(require("ko-lints"));
|
|
9
|
+
const build_1 = __importDefault(require("./actions/build"));
|
|
10
|
+
const dev_1 = __importDefault(require("./actions/dev"));
|
|
11
|
+
const program = new commander_1.Command();
|
|
12
|
+
const pkg = require('../package.json');
|
|
13
|
+
program
|
|
14
|
+
.description('Project toolkit base on webpack,babel,eslint and prettier.')
|
|
15
|
+
.version(pkg.version, '-v, --version')
|
|
16
|
+
.usage('<command> [options]');
|
|
17
|
+
program
|
|
18
|
+
.command('build')
|
|
19
|
+
.description('build project')
|
|
20
|
+
.option('--hash', 'output file name with hash')
|
|
21
|
+
.option('-t,--ts,--typescript', 'support typescript')
|
|
22
|
+
.option('-e,--esbuild', 'enable esbuild')
|
|
23
|
+
.action((opts) => {
|
|
24
|
+
const buildInstance = new build_1.default(opts);
|
|
25
|
+
buildInstance.action();
|
|
26
|
+
});
|
|
27
|
+
program
|
|
28
|
+
.command('dev')
|
|
29
|
+
.description('start devServer')
|
|
30
|
+
.option('-p, --port <port>', 'server start on which port', parseInt)
|
|
31
|
+
.option('--host <host>', 'specify a host to use')
|
|
32
|
+
.option('-t, --ts', 'support typescript')
|
|
33
|
+
.option('-a,--analyzer', 'support building analyzer')
|
|
34
|
+
.action((opts) => {
|
|
35
|
+
process.env.NODE_ENV = 'development';
|
|
36
|
+
const devInstance = new dev_1.default(opts);
|
|
37
|
+
devInstance.action();
|
|
38
|
+
});
|
|
39
|
+
//attach lint features to program
|
|
40
|
+
(0, ko_lints_1.default)(program);
|
|
41
|
+
program.parse();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path_1 = require("path");
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
class Config {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.cwd = process.cwd();
|
|
8
|
+
}
|
|
9
|
+
static getInstance() {
|
|
10
|
+
if (!Config.instance) {
|
|
11
|
+
Config.instance = new Config();
|
|
12
|
+
}
|
|
13
|
+
return Config.instance;
|
|
14
|
+
}
|
|
15
|
+
getFileRealPath(path) {
|
|
16
|
+
return (0, path_1.isAbsolute)(path) ? path : (0, path_1.resolve)(this.cwd, path);
|
|
17
|
+
}
|
|
18
|
+
//TODO: define userConf
|
|
19
|
+
get userConf() {
|
|
20
|
+
const userConfPath = this.getFileRealPath('ko.config.js');
|
|
21
|
+
if ((0, fs_1.existsSync)(userConfPath)) {
|
|
22
|
+
return userConfPath ? require(userConfPath) : {};
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
throw new Error('user config file not exist, please check it!');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
get defaultPaths() {
|
|
29
|
+
return {
|
|
30
|
+
src: this.getFileRealPath('src'),
|
|
31
|
+
dist: this.getFileRealPath('dist'),
|
|
32
|
+
public: this.getFileRealPath('public'),
|
|
33
|
+
html: this.getFileRealPath('public/index.html'),
|
|
34
|
+
tsconfig: this.getFileRealPath('tsconfig.json')
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
get isProductionEnv() {
|
|
38
|
+
const PROD = 'production';
|
|
39
|
+
return process.env.NODE_ENV === PROD;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.default = Config.getInstance();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const config_1 = __importDefault(require("./config"));
|
|
7
|
+
console.log(typeof config_1.default.getFileRealPath);
|
|
8
|
+
describe('config instance', () => {
|
|
9
|
+
it('getFileRealPath should return absolute path', () => {
|
|
10
|
+
const absolutePath = '/foo/bar';
|
|
11
|
+
expect(config_1.default.getFileRealPath(absolutePath)).toBe(absolutePath);
|
|
12
|
+
const relativePath = 'ko.config.js';
|
|
13
|
+
expect(config_1.default.getFileRealPath(relativePath)).toBe(process.cwd() + '/' + relativePath);
|
|
14
|
+
});
|
|
15
|
+
it('userConf should throw', () => {
|
|
16
|
+
expect(() => {
|
|
17
|
+
config_1.default.userConf;
|
|
18
|
+
}).toThrow();
|
|
19
|
+
});
|
|
20
|
+
it('defaultPaths should ', () => {
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
expect(config_1.default.defaultPaths).toEqual({
|
|
23
|
+
src: cwd + '/src',
|
|
24
|
+
dist: cwd + '/dist',
|
|
25
|
+
public: cwd + '/public',
|
|
26
|
+
html: cwd + '/public/index.html',
|
|
27
|
+
tsconfig: cwd + '/tsconfig.json'
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const tsconfig_paths_webpack_plugin_1 = __importDefault(require("tsconfig-paths-webpack-plugin"));
|
|
7
|
+
const config_1 = __importDefault(require("../utils/config"));
|
|
8
|
+
const loaders_1 = __importDefault(require("./loaders"));
|
|
9
|
+
const plugins_1 = __importDefault(require("./plugins"));
|
|
10
|
+
const extensions = [
|
|
11
|
+
'.js',
|
|
12
|
+
'.jsx',
|
|
13
|
+
'.ts',
|
|
14
|
+
'.tsx',
|
|
15
|
+
'.css',
|
|
16
|
+
'.scss',
|
|
17
|
+
'.sass',
|
|
18
|
+
'.less',
|
|
19
|
+
'.json',
|
|
20
|
+
'.html',
|
|
21
|
+
];
|
|
22
|
+
function getWebpackBaseConf(opts) {
|
|
23
|
+
const { ts, hash } = opts;
|
|
24
|
+
const webpackBaseConf = {
|
|
25
|
+
mode: config_1.default.isProductionEnv ? 'production' : 'development',
|
|
26
|
+
target: 'web',
|
|
27
|
+
context: config_1.default.cwd,
|
|
28
|
+
entry: `src/index.${ts ? 'tsx' : 'js'}`,
|
|
29
|
+
output: {
|
|
30
|
+
path: config_1.default.defaultPaths.dist,
|
|
31
|
+
filename: hash ? 'js/[name].[contenthash].js' : 'js/[name].js',
|
|
32
|
+
publicPath: '/',
|
|
33
|
+
},
|
|
34
|
+
module: {
|
|
35
|
+
rules: (0, loaders_1.default)(opts),
|
|
36
|
+
},
|
|
37
|
+
plugins: (0, plugins_1.default)(opts),
|
|
38
|
+
resolve: {
|
|
39
|
+
extensions,
|
|
40
|
+
plugins: [
|
|
41
|
+
ts && new tsconfig_paths_webpack_plugin_1.default({
|
|
42
|
+
configFile: config_1.default.defaultPaths.tsconfig
|
|
43
|
+
})
|
|
44
|
+
].filter(Boolean)
|
|
45
|
+
},
|
|
46
|
+
performance: {
|
|
47
|
+
hints: false,
|
|
48
|
+
},
|
|
49
|
+
cache: {
|
|
50
|
+
type: 'filesystem',
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
return webpackBaseConf;
|
|
54
|
+
}
|
|
55
|
+
exports.default = getWebpackBaseConf;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const assetModules = [
|
|
4
|
+
{
|
|
5
|
+
test: /\.(woff|woff2|ttf|eot)$/,
|
|
6
|
+
type: 'asset/resource',
|
|
7
|
+
generator: {
|
|
8
|
+
filename: 'fonts/[hash][ext][query]',
|
|
9
|
+
},
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
test: /\.(png|jpg|jpeg|gif|webp|svg)$/i,
|
|
13
|
+
type: 'asset/resource',
|
|
14
|
+
generator: {
|
|
15
|
+
filename: 'images/[hash][ext][query]',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
exports.default = assetModules;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const style_1 = __importDefault(require("./style"));
|
|
7
|
+
const asset_1 = __importDefault(require("./asset"));
|
|
8
|
+
const script_1 = __importDefault(require("./script"));
|
|
9
|
+
function getLoaders(opts) {
|
|
10
|
+
return [...style_1.default, ...asset_1.default, ...(0, script_1.default)(opts.ts)];
|
|
11
|
+
}
|
|
12
|
+
exports.default = getLoaders;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const getCacheIdentifier_1 = __importDefault(require("react-dev-utils/getCacheIdentifier"));
|
|
7
|
+
const config_1 = __importDefault(require("../../utils/config"));
|
|
8
|
+
const THREAD_LOADER = require.resolve('thread-loader');
|
|
9
|
+
const BABEL_LOADER = require.resolve('babel-loader');
|
|
10
|
+
function getScriptLoaders(supportTypescript) {
|
|
11
|
+
let scriptLoader = [
|
|
12
|
+
{
|
|
13
|
+
test: /\.jsx?$/,
|
|
14
|
+
include: config_1.default.defaultPaths.src,
|
|
15
|
+
use: [
|
|
16
|
+
THREAD_LOADER,
|
|
17
|
+
{
|
|
18
|
+
loader: BABEL_LOADER,
|
|
19
|
+
options: {
|
|
20
|
+
presets: [
|
|
21
|
+
[
|
|
22
|
+
require.resolve('babel-preset-ko-app'),
|
|
23
|
+
{
|
|
24
|
+
useAbsoluteRuntime: true,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
],
|
|
28
|
+
babelrc: false,
|
|
29
|
+
configFile: false,
|
|
30
|
+
cacheIdentifier: (0, getCacheIdentifier_1.default)(config_1.default.isProductionEnv ? 'production' : '', ['babel-preset-ko-app', 'react-dev-utils', 'ko']),
|
|
31
|
+
cacheDirectory: true,
|
|
32
|
+
cacheCompression: false,
|
|
33
|
+
compact: config_1.default.isProductionEnv,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
if (supportTypescript) {
|
|
40
|
+
const TS_LOADER = require.resolve('ts-loader');
|
|
41
|
+
const typescriptLoader = {
|
|
42
|
+
test: /\.tsx?$/,
|
|
43
|
+
use: [
|
|
44
|
+
THREAD_LOADER,
|
|
45
|
+
{
|
|
46
|
+
loader: TS_LOADER,
|
|
47
|
+
options: {
|
|
48
|
+
transpileOnly: true,
|
|
49
|
+
happyPackMode: true,
|
|
50
|
+
allowTsInNodeModules: true
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
scriptLoader.push(typescriptLoader);
|
|
56
|
+
}
|
|
57
|
+
return scriptLoader;
|
|
58
|
+
}
|
|
59
|
+
exports.default = getScriptLoaders;
|