framework-for-react 1.0.1

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 ADDED
@@ -0,0 +1,79 @@
1
+ # framework-for-react
2
+
3
+ #### 介绍
4
+ framework-for-react
5
+
6
+
7
+ #### 安装教程
8
+
9
+ 1. npm install -D framework-for-react
10
+
11
+ #### 使用说明
12
+
13
+ 1. 这是一个针对react18,使用webpack5进行打包的简洁工具包,包含babel预置v7,各种预置loader,react-router等工具,开箱即用
14
+ 2. 使用示例
15
+ ```
16
+ *** dev.js ***
17
+ const { webpackConfig, devServerConfig, startServer } = require('framework-for-react');
18
+ // modify loader, eg: jsx-loader
19
+ webpackConfig.module.rules.forEach(i => {
20
+ if (i.test.toString() == /\.jsx?$/) {
21
+ // todo vue
22
+ }
23
+ });
24
+ // copywebpack plugin
25
+ webpackConfig.plugins.push(
26
+ new CopyWebpackPlugin({
27
+ patterns: [
28
+ { from: 'xxx', to: 'xxx' },
29
+ ]
30
+ })
31
+ );
32
+ devServerConfig.port = '8080';
33
+ devServerConfig.host = '127.0.0.1';
34
+ devServerConfig.proxy = {
35
+ '/': {
36
+ target: 'http://x.xx.xxx/api', // 后端接口
37
+ secure: false, // 是否验证SSl证书
38
+ changeOrigin: true,
39
+ bypass: function(req, res, proxyOptions) {
40
+ console.log(proxyOptions.target, req.originalUrl)
41
+ }
42
+ }
43
+ };
44
+
45
+ // start dev server on host:port
46
+ startServer(webpackConfig, devServerConfig);
47
+ ```
48
+
49
+ ```
50
+ *** build.js ***
51
+ const { webpackConfig } = require('framework-for-react');
52
+ // modify loader, eg: vue-loader
53
+ webpackConfig.module.rules.forEach(i => {
54
+ if (i.test.toString() == /\.vue$/) {
55
+ // todo vue
56
+ }
57
+ });
58
+ // copywebpack plugin
59
+ webpackConfig.plugins.push(
60
+ new CopyWebpackPlugin({
61
+ patterns: [
62
+ { from: 'xxx', to: 'xxx' },
63
+ ]
64
+ })
65
+ );
66
+ module.exports = webpackConfig;
67
+ ```
68
+
69
+ ```
70
+ *** package.json ***
71
+ "script": {
72
+ "dev": "cross-env NODE_ENV=development node ./dev.js",
73
+ "build": "cross-env NODE_ENV=production webpack --config ./build.js",
74
+ }
75
+ ```
76
+
77
+
78
+
79
+
@@ -0,0 +1,22 @@
1
+ module.exports = {
2
+ assumptions: {
3
+ privateFieldsAsProperties: true,
4
+ setPublicClassFields: true,
5
+ },
6
+ presets: ['@babel/preset-env', '@babel/preset-react'],
7
+ plugins: [
8
+ '@babel/plugin-transform-arrow-functions',
9
+ ['@babel/plugin-proposal-decorators', { legacy: true }],
10
+ ['@babel/plugin-proposal-private-methods' /*, { loose: true }*/],
11
+ ['@babel/plugin-proposal-class-properties' /*, { loose: true }*/],
12
+ [
13
+ 'babel-plugin-import',
14
+ {
15
+ libraryName: 'antd',
16
+ libraryDirectory: 'lib',
17
+ style: 'css',
18
+ },
19
+ ],
20
+ ],
21
+ compact: false,
22
+ };
package/index.js ADDED
@@ -0,0 +1,178 @@
1
+ const path = require('path');
2
+ const webpack = require('webpack');
3
+ const WebpackDevServer = require('webpack-dev-server');
4
+ const autoprefixer = require('autoprefixer');
5
+ const htmlWebpackPlugin = require('html-webpack-plugin');
6
+ const CopyWebpackPlugin = require('copy-webpack-plugin');
7
+ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
8
+ const { GitRevisionPlugin } = require('git-revision-webpack-plugin');
9
+ const gitRevision = new GitRevisionPlugin({ branch: true });
10
+
11
+ // 定义当前工作目录
12
+ const APP_PATH = path.resolve(process.cwd());
13
+ const APP_SRC = path.join(APP_PATH, '/src');
14
+ const APP_DIST = path.join(APP_PATH, '/dist');
15
+ // framework 目录
16
+ const dir_path = path.resolve(__dirname);
17
+
18
+ const devMode = process.env.NODE_ENV !== 'production';
19
+
20
+ const webpackConfig = {
21
+ devtool: devMode,
22
+ entry: {
23
+ app: [path.join(APP_SRC, '/main.js')],
24
+ vendors: [
25
+ '@babel/polyfill',
26
+ 'react',
27
+ 'react-dom',
28
+ 'react-redux',
29
+ 'react-router-dom',
30
+ 'axios',
31
+ 'react-intl',
32
+ 'classnames'
33
+ ]
34
+ },
35
+ output: {
36
+ path: APP_DIST,
37
+ filename: '[name].[contenthash:8].js',
38
+ chunkFilename: '[name].[contenthash:8].js',
39
+ },
40
+ resolve: {
41
+ alias: {
42
+ React: 'react/index.js',
43
+ app_src: APP_SRC,
44
+ app_component: 'app_src/component',
45
+ app_view: 'app_src/view'
46
+ },
47
+ extensions: ['.js', '.json', '.jsx', '.scss', '.less', '.css']
48
+ },
49
+ module: {
50
+ rules: [
51
+ {
52
+ test: /\.jsx?$/,
53
+ exclude: /^node_modules$/,
54
+ use: [
55
+ {
56
+ loader: path.resolve(process.cwd(), 'node_modules/babel-loader'),
57
+ options: {
58
+ configFile: path.resolve(dir_path, './babel.config.js'),
59
+ cacheDirectory: true
60
+ }
61
+ }
62
+ ]
63
+ },
64
+ {
65
+ test: /\.(png|jpe?g|gif|svg)$/,
66
+ type: 'asset',
67
+ generator: {
68
+ filename: "images/[name]-[contenthash:8].[ext]",
69
+ }
70
+ },
71
+ {
72
+ test: /\.(eot|svg|ttf|woff|woff2)$/,
73
+ type: 'asset',
74
+ generator: {
75
+ filename: 'fonts/[name].[contenthash:8][ext]'
76
+ }
77
+ },
78
+ {
79
+ test: /\.css$/,
80
+ use: [devMode ? 'style-loader' : MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']
81
+ },
82
+ {
83
+ test: /\.less$/,
84
+ use: [
85
+ devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
86
+ 'css-loader',
87
+ 'postcss-loader',
88
+ {
89
+ loader: 'less-loader',
90
+ options: {
91
+ lessOptions: {
92
+ strictMath: true,
93
+ noIeCompat: true
94
+ }
95
+ }
96
+ }
97
+ ]
98
+ }
99
+ ]
100
+ },
101
+ optimization: {
102
+ splitChunks: {
103
+ chunks: 'all',
104
+ minSize: 256 * 1024,
105
+ maxSize: 1280 * 1024
106
+ }
107
+ },
108
+ plugins: [
109
+ new MiniCssExtractPlugin({
110
+ filename: devMode ? 'css/[name].css' : 'css/[name].[contenthash:8].css',
111
+ chunkFilename: devMode ? 'css/[name].css' : 'css/[name].[contenthash:8].css'
112
+ }),
113
+ new htmlWebpackPlugin({
114
+ hash: true,
115
+ minify: {
116
+ removeComments: true,
117
+ collapseWhitespace: true
118
+ },
119
+ favicon: path.join(APP_SRC, '/asset/images/ico.ico'),
120
+ template: path.join(APP_SRC, '/template/index.html')
121
+ })
122
+ ]
123
+ };
124
+
125
+ try {
126
+ const ver = gitRevision.version();
127
+ console.log(ver);
128
+ webpackConfig.plugins.push(gitRevision);
129
+ webpackConfig.plugins.push(
130
+ new webpack.DefinePlugin({
131
+ __ENV__: JSON.stringify(process.env.NODE_ENV),
132
+ __TEST__: JSON.stringify(process.env.PROJ_TEST),
133
+ VERSION: JSON.stringify(gitRevision.version()),
134
+ COMMITHASH: JSON.stringify(gitRevision.commithash()),
135
+ BRANCH: JSON.stringify(gitRevision.branch())
136
+ })
137
+ );
138
+ } catch(e) {
139
+ console.log('no git informations');
140
+ webpackConfig.plugins.push(
141
+ new webpack.DefinePlugin({
142
+ __ENV__: JSON.stringify(process.env.NODE_ENV),
143
+ __TEST__: JSON.stringify(process.env.PROJ_TEST),
144
+ })
145
+ );
146
+ }
147
+
148
+ const devServerConfig = {
149
+ hot: true,
150
+ host: '0.0.0.0',
151
+ port: '8080',
152
+ historyApiFallback: true,
153
+ compress: true,
154
+ allowedHosts: 'all',
155
+ proxy: { },
156
+ };
157
+
158
+ function startServer(config, devConfig) {
159
+ const compiler = webpack({
160
+ ...config,
161
+ devtool: 'eval-cheap-module-source-map',
162
+ mode: devMode ? 'development' : 'production',
163
+ });
164
+ const server = new WebpackDevServer(devConfig, compiler);
165
+ server.start().then(error => {
166
+ if (error) {
167
+ console.error(error);
168
+ } else {
169
+ console.info('%c Listening on http://%s:%s in your browser.', 'font-size: 18px; color: #e22;', devConfig.host, devConfig.port);
170
+ }
171
+ });
172
+ }
173
+
174
+ module.exports = {
175
+ webpackConfig,
176
+ devServerConfig,
177
+ startServer
178
+ };
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "framework-for-react",
3
+ "version": "1.0.1",
4
+ "description": "framework for react18",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "files": [
10
+ "index.js",
11
+ "babel.config.js"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://gitee.com/sky_yz/framework-for-react.git"
16
+ },
17
+ "keywords": [
18
+ "framework",
19
+ "react18",
20
+ "webpack5"
21
+ ],
22
+ "author": "yzh",
23
+ "license": "ISC",
24
+ "dependencies": {
25
+ "@babel/core": "^7.18.5",
26
+ "@babel/plugin-syntax-dynamic-import": "^7.8.3",
27
+ "@babel/plugin-syntax-jsx": "^7.17.12",
28
+ "@babel/plugin-transform-runtime": "^7.18.5",
29
+ "@babel/polyfill": "^7.12.1",
30
+ "@babel/preset-env": "^7.18.2",
31
+ "@babel/preset-react": "^7.18.6",
32
+ "@babel/runtime": "^7.18.3",
33
+ "autoprefixer": "^10.4.7",
34
+ "babel-loader": "^8.2.5",
35
+ "classnames": "^2.3.1",
36
+ "copy-webpack-plugin": "^8.1.1",
37
+ "cross-env": "^7.0.3",
38
+ "css-loader": "^6.7.1",
39
+ "git-revision-webpack-plugin": "^5.0.0",
40
+ "html-webpack-plugin": "^4.5.2",
41
+ "less": "^4.1.3",
42
+ "less-loader": "^7.3.0",
43
+ "mini-css-extract-plugin": "^2.6.1",
44
+ "postcss": "^8.4.14",
45
+ "postcss-loader": "^7.0.0",
46
+ "react": "^18.2.0",
47
+ "react-dom": "^18.2.0",
48
+ "react-intl": "^6.4.0",
49
+ "react-redux": "^8.0.5",
50
+ "react-router-dom": "^6.10.0",
51
+ "style-loader": "^3.3.1",
52
+ "webpack": "^5.73.0",
53
+ "webpack-cli": "^4.10.0",
54
+ "webpack-dev-server": "^4.9.2",
55
+ "webpack-merge": "^5.8.0"
56
+ }
57
+ }