yimi-builder-webpack 1.0.0
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/.eslintrc.js +8 -0
- package/README.md +0 -0
- package/babel.config.js +8 -0
- package/lib/webpack.base.js +138 -0
- package/lib/webpack.dev.js +18 -0
- package/lib/webpack.prod.js +45 -0
- package/lib/webpack.ssr.js +57 -0
- package/package.json +41 -0
- package/test/index.js +8 -0
- package/test/smoke/css-js-test.js +18 -0
- package/test/smoke/html-test.js +16 -0
- package/test/smoke/index.js +32 -0
- package/test/smoke/template/.eslintrc.js +11 -0
- package/test/smoke/template/babel.config.js +8 -0
- package/test/smoke/template/package-lock.json +10926 -0
- package/test/smoke/template/package.json +66 -0
- package/test/smoke/template/server/data.json +235 -0
- package/test/smoke/template/server/index.js +34 -0
- package/test/smoke/template/src/index/helloworld.js +3 -0
- package/test/smoke/template/src/index/index.html +12 -0
- package/test/smoke/template/src/index/index.js +10 -0
- package/test/smoke/template/src/search/images/logo.png +0 -0
- package/test/smoke/template/src/search/index-server.js +43 -0
- package/test/smoke/template/src/search/index.html +14 -0
- package/test/smoke/template/src/search/index.js +40 -0
- package/test/smoke/template/src/search/meta.html +10 -0
- package/test/smoke/template/src/search/text.js +3 -0
- package/test/smoke/template/src/search/tree-shaking.js +8 -0
- package/test/unit/webpack-base-test.js +12 -0
package/.eslintrc.js
ADDED
package/README.md
ADDED
|
File without changes
|
package/babel.config.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
const glob = require('glob');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
|
|
4
|
+
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
|
|
5
|
+
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
6
|
+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
|
7
|
+
const autoprefixer = require('autoprefixer');
|
|
8
|
+
|
|
9
|
+
const projectRoot = process.cwd();
|
|
10
|
+
|
|
11
|
+
const setMPA = () => {
|
|
12
|
+
const entry = {};
|
|
13
|
+
const htmlWebpackPlugins = [];
|
|
14
|
+
const entryFiles = glob.sync(path.join(projectRoot, './src/*/index.js'));
|
|
15
|
+
Object.keys(entryFiles)
|
|
16
|
+
.map((index) => {
|
|
17
|
+
const entryFile = entryFiles[index];
|
|
18
|
+
// '/Users/cpselvis/my-project/src/index/index.js'
|
|
19
|
+
|
|
20
|
+
const match = entryFile.match(/src\/(.*)\/index\.js/);
|
|
21
|
+
const pageName = match && match[1];
|
|
22
|
+
|
|
23
|
+
entry[pageName] = entryFile;
|
|
24
|
+
return htmlWebpackPlugins.push(
|
|
25
|
+
new HtmlWebpackPlugin({
|
|
26
|
+
inlineSource: '.css$',
|
|
27
|
+
template: path.join(projectRoot, `./src/${pageName}/index.html`),
|
|
28
|
+
filename: `${pageName}.html`,
|
|
29
|
+
chunks: ['vendors', pageName],
|
|
30
|
+
inject: true,
|
|
31
|
+
minify: {
|
|
32
|
+
html5: true,
|
|
33
|
+
collapseWhitespace: true,
|
|
34
|
+
preserveLineBreaks: false,
|
|
35
|
+
minifyCSS: true,
|
|
36
|
+
minifyJS: true,
|
|
37
|
+
removeComments: false,
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
entry,
|
|
45
|
+
htmlWebpackPlugins,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const { entry, htmlWebpackPlugins } = setMPA();
|
|
50
|
+
|
|
51
|
+
module.exports = {
|
|
52
|
+
entry,
|
|
53
|
+
output: {
|
|
54
|
+
path: path.join(projectRoot, 'dist'),
|
|
55
|
+
filename: '[name]_[chunkhash:8].js',
|
|
56
|
+
},
|
|
57
|
+
module: {
|
|
58
|
+
rules: [
|
|
59
|
+
{
|
|
60
|
+
test: /.js$/,
|
|
61
|
+
use: [
|
|
62
|
+
{
|
|
63
|
+
loader: 'babel-loader',
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
test: /.css$/,
|
|
69
|
+
use: [
|
|
70
|
+
MiniCssExtractPlugin.loader,
|
|
71
|
+
'css-loader',
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
test: /.less$/,
|
|
76
|
+
use: [
|
|
77
|
+
MiniCssExtractPlugin.loader,
|
|
78
|
+
'css-loader',
|
|
79
|
+
'less-loader',
|
|
80
|
+
{
|
|
81
|
+
loader: 'postcss-loader',
|
|
82
|
+
options: {
|
|
83
|
+
plugins: () => [
|
|
84
|
+
autoprefixer({
|
|
85
|
+
browsers: ['last 2 version', '> 1%', 'ios 7'],
|
|
86
|
+
}),
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
loader: 'px2rem-loader',
|
|
92
|
+
options: {
|
|
93
|
+
remUnit: 75,
|
|
94
|
+
remPrecision: 8,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
test: /.(png|jpg|gif|jpeg)$/,
|
|
101
|
+
use: [
|
|
102
|
+
{
|
|
103
|
+
loader: 'file-loader',
|
|
104
|
+
options: {
|
|
105
|
+
name: '[name]_[hash:8].[ext]',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
test: /.(woff|woff2|eot|ttf|otf)$/,
|
|
112
|
+
use: [
|
|
113
|
+
{
|
|
114
|
+
loader: 'file-loader',
|
|
115
|
+
options: {
|
|
116
|
+
name: '[name]_[hash:8][ext]',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
plugins: [
|
|
124
|
+
new MiniCssExtractPlugin({
|
|
125
|
+
filename: '[name]_[contenthash:8].css',
|
|
126
|
+
}),
|
|
127
|
+
new CleanWebpackPlugin(),
|
|
128
|
+
new FriendlyErrorsWebpackPlugin(),
|
|
129
|
+
function errorPlugin() {
|
|
130
|
+
this.hooks.done.tap('done', (stats) => {
|
|
131
|
+
if (stats.compilation.errors && stats.compilation.errors.length && process.argv.indexOf('--watch') === -1) {
|
|
132
|
+
process.exit(1);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
].concat(htmlWebpackPlugins),
|
|
137
|
+
stats: 'errors-only',
|
|
138
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const merge = require('webpack-merge');
|
|
2
|
+
const webpack = require('webpack');
|
|
3
|
+
const baseConfig = require('./webpack.base');
|
|
4
|
+
|
|
5
|
+
const devConfig = {
|
|
6
|
+
mode: 'production',
|
|
7
|
+
plugins: [
|
|
8
|
+
new webpack.HotModuleReplacementPlugin(),
|
|
9
|
+
],
|
|
10
|
+
devServer: {
|
|
11
|
+
contentBase: './dist',
|
|
12
|
+
hot: true,
|
|
13
|
+
stats: 'errors-only',
|
|
14
|
+
},
|
|
15
|
+
devtool: 'cheap-source-map',
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports = merge(baseConfig, devConfig);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const { merge } = require('webpack-merge');
|
|
2
|
+
// const cssnano = require('cssnano');
|
|
3
|
+
// const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
|
|
4
|
+
// const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
|
5
|
+
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
|
|
6
|
+
const baseConfig = require('./webpack.base');
|
|
7
|
+
|
|
8
|
+
const prodConfig = {
|
|
9
|
+
mode: 'production',
|
|
10
|
+
plugins: [
|
|
11
|
+
new CssMinimizerWebpackPlugin(),
|
|
12
|
+
// new OptimizeCSSAssetsPlugin({
|
|
13
|
+
// assetNameRegExp: /\.css$/g,
|
|
14
|
+
// cssProcessor: cssnano,
|
|
15
|
+
// }),
|
|
16
|
+
// new HtmlWebpackExternalsPlugin({
|
|
17
|
+
// externals: [
|
|
18
|
+
// {
|
|
19
|
+
// module: 'react',
|
|
20
|
+
// entry: 'https://11.url.cn/now/lib/16.2.0/react.min.js',
|
|
21
|
+
// global: 'React',
|
|
22
|
+
// },
|
|
23
|
+
// {
|
|
24
|
+
// module: 'react-dom',
|
|
25
|
+
// entry: 'https://11.url.cn/now/lib/16.2.0/react-dom.min.js',
|
|
26
|
+
// global: 'ReactDOM',
|
|
27
|
+
// },
|
|
28
|
+
// ],
|
|
29
|
+
// }),
|
|
30
|
+
],
|
|
31
|
+
optimization: {
|
|
32
|
+
splitChunks: {
|
|
33
|
+
minSize: 0,
|
|
34
|
+
cacheGroups: {
|
|
35
|
+
commons: {
|
|
36
|
+
name: 'vendors',
|
|
37
|
+
chunks: 'all',
|
|
38
|
+
minChunks: 2,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
module.exports = merge(baseConfig, prodConfig);
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { merge } = require('webpack-merge');
|
|
2
|
+
// const cssnano = require('cssnano');
|
|
3
|
+
// const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
|
|
4
|
+
// const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
|
5
|
+
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
|
|
6
|
+
const baseConfig = require('./webpack.base');
|
|
7
|
+
|
|
8
|
+
const prodConfig = {
|
|
9
|
+
mode: 'production',
|
|
10
|
+
module: {
|
|
11
|
+
rules: [
|
|
12
|
+
{
|
|
13
|
+
test: /\.css$/,
|
|
14
|
+
use: 'ignore-loader',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
test: /\.less$/,
|
|
18
|
+
use: 'ignore-loader',
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
plugins: [
|
|
23
|
+
new CssMinimizerWebpackPlugin(),
|
|
24
|
+
// new OptimizeCSSAssetsPlugin({
|
|
25
|
+
// assetNameRegExp: /\.css$/g,
|
|
26
|
+
// cssProcessor: cssnano,
|
|
27
|
+
// }),
|
|
28
|
+
// new HtmlWebpackExternalsPlugin({
|
|
29
|
+
// externals: [
|
|
30
|
+
// {
|
|
31
|
+
// module: 'react',
|
|
32
|
+
// entry: 'https://11.url.cn/now/lib/16.2.0/react.min.js',
|
|
33
|
+
// global: 'React',
|
|
34
|
+
// },
|
|
35
|
+
// {
|
|
36
|
+
// module: 'react-dom',
|
|
37
|
+
// entry: 'https://11.url.cn/now/lib/16.2.0/react-dom.min.js',
|
|
38
|
+
// global: 'ReactDOM',
|
|
39
|
+
// },
|
|
40
|
+
// ],
|
|
41
|
+
// })
|
|
42
|
+
],
|
|
43
|
+
optimization: {
|
|
44
|
+
splitChunks: {
|
|
45
|
+
minSize: 0,
|
|
46
|
+
cacheGroups: {
|
|
47
|
+
commons: {
|
|
48
|
+
name: 'commons',
|
|
49
|
+
chunks: 'all',
|
|
50
|
+
minChunks: 2,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
module.exports = merge(baseConfig, prodConfig);
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "yimi-builder-webpack",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "webpack项目配置集成",
|
|
5
|
+
"private": false,
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha",
|
|
9
|
+
"test2": "nyc npm run test",
|
|
10
|
+
"eslint": "eslint ./lib --fix"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "wangguoliang123",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"babel-eslint": "^10.1.0",
|
|
17
|
+
"eslint": "^7.32.0",
|
|
18
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
19
|
+
"eslint-plugin-import": "^2.32.0",
|
|
20
|
+
"glob-all": "^3.3.1",
|
|
21
|
+
"mocha": "^11.7.5",
|
|
22
|
+
"rimraf": "^3.0.2"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@babel/core": "^7.28.5",
|
|
26
|
+
"@babel/plugin-transform-react-jsx": "^7.28.6",
|
|
27
|
+
"@babel/preset-env": "^7.28.5",
|
|
28
|
+
"autoprefixer": "^9.8.8",
|
|
29
|
+
"babel-loader": "^8.4.1",
|
|
30
|
+
"clean-webpack-plugin": "^4.0.0",
|
|
31
|
+
"css-minimizer-webpack-plugin": "^1.3.0",
|
|
32
|
+
"friendly-errors-webpack-plugin": "^1.7.0",
|
|
33
|
+
"glob": "^7.2.3",
|
|
34
|
+
"html-webpack-plugin": "^4.5.2",
|
|
35
|
+
"mini-css-extract-plugin": "^1.6.2",
|
|
36
|
+
"nyc": "^17.1.0",
|
|
37
|
+
"webpack": "^4.47.0",
|
|
38
|
+
"webpack-cli": "^3.3.12",
|
|
39
|
+
"webpack-merge": "^6.0.1"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/test/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const glob = require('glob-all');
|
|
2
|
+
|
|
3
|
+
describe('Checking generated css js files', () => {
|
|
4
|
+
it('should generate css js files', (done) => {
|
|
5
|
+
const files = glob.sync([
|
|
6
|
+
'./dist/index_*.js',
|
|
7
|
+
'./dist/index_*.css',
|
|
8
|
+
'./dist/search_*.js',
|
|
9
|
+
'./dist/search_*.css',
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
if (files.length > 0) {
|
|
13
|
+
done();
|
|
14
|
+
} else {
|
|
15
|
+
throw new Error('no css js files generated');
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const glob = require('glob-all');
|
|
2
|
+
|
|
3
|
+
describe('Checking generated html files', () => {
|
|
4
|
+
it('should generate html files', (done) => {
|
|
5
|
+
const files = glob.sync([
|
|
6
|
+
'./dist/index.html',
|
|
7
|
+
'./dist/search.html'
|
|
8
|
+
]);
|
|
9
|
+
|
|
10
|
+
if (files.length > 0) {
|
|
11
|
+
done();
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error('no html files generated');
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const webpack = require('webpack');
|
|
3
|
+
const rimraf = require('rimraf');
|
|
4
|
+
const Mocha = require('mocha');
|
|
5
|
+
|
|
6
|
+
const mocha = new Mocha({
|
|
7
|
+
timeout: '10000ms'
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
process.chdir(path.join(__dirname, 'template'));
|
|
11
|
+
|
|
12
|
+
rimraf('./dist', () => {
|
|
13
|
+
const prodConfig = require('../../lib/webpack.prod.js');
|
|
14
|
+
|
|
15
|
+
webpack(prodConfig, (err, stats) => {
|
|
16
|
+
if (err) {
|
|
17
|
+
console.error(err);
|
|
18
|
+
process.exit(2);
|
|
19
|
+
}
|
|
20
|
+
console.log(stats.toString({
|
|
21
|
+
colors: true,
|
|
22
|
+
modules: false,
|
|
23
|
+
children: false
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
console.log('Webpack build success, begin run test.');
|
|
27
|
+
|
|
28
|
+
mocha.addFile(path.join(__dirname, 'html-test.js'));
|
|
29
|
+
mocha.addFile(path.join(__dirname, 'css-js-test.js'));
|
|
30
|
+
mocha.run();
|
|
31
|
+
});
|
|
32
|
+
});
|