mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-21 22:09:16 +08:00
feat(cli): support custom webpack config
This commit is contained in:
parent
578448d040
commit
287251fa90
@ -7,14 +7,18 @@ export const SRC_DIR = join(CWD, 'src');
|
||||
export const DOCS_DIR = join(CWD, 'docs');
|
||||
export const CONFIG_FILE = join(CWD, 'components.config.js');
|
||||
export const PACKAGE_JSON_FILE = join(CWD, 'package.json');
|
||||
export const WEBPACK_CONFIG_FILE = join(CWD, 'webpack.config.js');
|
||||
|
||||
export const DIST_DIR = join(__dirname, '../../dist');
|
||||
export const CONFIG_DIR = join(__dirname, '../config');
|
||||
|
||||
export const PACKAGE_ENTRY_FILE = join(DIST_DIR, 'index.js');
|
||||
export const MOBILE_CONFIG_FILE = join(DIST_DIR, 'mobile-config.js');
|
||||
export const DESKTOP_CONFIG_FILE = join(DIST_DIR, 'desktop-config.js');
|
||||
export const BABEL_CONFIG_FILE = join(CONFIG_DIR, 'babel.config.js');
|
||||
|
||||
export const JEST_CONFIG_FILE = join(CONFIG_DIR, 'jest.config.js');
|
||||
export const BABEL_CONFIG_FILE = join(CONFIG_DIR, 'babel.config.js');
|
||||
export const POSTCSS_CONFIG_FILE = join(CONFIG_DIR, 'postcss.config.js');
|
||||
export const JEST_TRANSFORM_FILE = join(CONFIG_DIR, 'jest.transform.js');
|
||||
export const JEST_FILE_MOCK_FILE = join(CONFIG_DIR, 'jest.file-mock.js');
|
||||
export const JEST_STYLE_MOCK_FILE = join(CONFIG_DIR, 'jest.style-mock.js');
|
||||
export const JEST_TRANSFORM_FILE = join(CONFIG_DIR, 'jest.transform.js');
|
||||
export const POSTCSS_CONFIG_FILE = join(CONFIG_DIR, 'postcss.config.js');
|
||||
|
@ -1,7 +1,7 @@
|
||||
import fs from 'fs-extra';
|
||||
import decamelize from 'decamelize';
|
||||
import { readdirSync, existsSync, lstatSync } from 'fs-extra';
|
||||
import { join } from 'path';
|
||||
import { SRC_DIR } from './constant';
|
||||
import { SRC_DIR, WEBPACK_CONFIG_FILE } from './constant';
|
||||
|
||||
export const EXT_REGEXP = /\.\w+$/;
|
||||
export const SFC_REGEXP = /\.(vue)$/;
|
||||
@ -21,16 +21,16 @@ export function replaceExt(path: string, ext: string) {
|
||||
|
||||
export function getComponents() {
|
||||
const EXCLUDES = ['.DS_Store'];
|
||||
const dirs = fs.readdirSync(SRC_DIR);
|
||||
const dirs = readdirSync(SRC_DIR);
|
||||
return dirs
|
||||
.filter(dir => !EXCLUDES.includes(dir))
|
||||
.filter(dir =>
|
||||
ENTRY_EXTS.some(ext => fs.existsSync(join(SRC_DIR, dir, `index.${ext}`)))
|
||||
ENTRY_EXTS.some(ext => existsSync(join(SRC_DIR, dir, `index.${ext}`)))
|
||||
);
|
||||
}
|
||||
|
||||
export function isDir(dir: string) {
|
||||
return fs.lstatSync(dir).isDirectory();
|
||||
return lstatSync(dir).isDirectory();
|
||||
}
|
||||
|
||||
export function isDemoDir(dir: string) {
|
||||
@ -67,4 +67,19 @@ export function pascalize(str: string): string {
|
||||
);
|
||||
}
|
||||
|
||||
export function getWebpackConfig(): object {
|
||||
if (existsSync(WEBPACK_CONFIG_FILE)) {
|
||||
// eslint-disable-next-line
|
||||
const config = require(WEBPACK_CONFIG_FILE);
|
||||
|
||||
if (typeof config === 'function') {
|
||||
return config();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
export { decamelize };
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { join } from 'path';
|
||||
import merge from 'webpack-merge';
|
||||
import { join } from 'path';
|
||||
import { baseConfig } from './webpack.base';
|
||||
import { getWebpackConfig } from '../common';
|
||||
import { LIB_DIR, DIST_DIR, CONFIG_FILE } from '../common/constant';
|
||||
|
||||
// eslint-disable-next-line
|
||||
@ -8,32 +9,36 @@ const config = require(CONFIG_FILE);
|
||||
const { name } = config;
|
||||
|
||||
export function packageConfig(isMinify: boolean) {
|
||||
return merge(baseConfig as any, {
|
||||
mode: 'production',
|
||||
entry: {
|
||||
[name]: join(DIST_DIR, 'index.js')
|
||||
},
|
||||
stats: 'none',
|
||||
output: {
|
||||
path: LIB_DIR,
|
||||
library: name,
|
||||
libraryTarget: 'umd',
|
||||
filename: isMinify ? '[name].min.js' : '[name].js',
|
||||
umdNamedDefine: true,
|
||||
// https://github.com/webpack/webpack/issues/6522
|
||||
globalObject: "typeof self !== 'undefined' ? self : this"
|
||||
},
|
||||
externals: {
|
||||
vue: {
|
||||
root: 'Vue',
|
||||
commonjs: 'vue',
|
||||
commonjs2: 'vue',
|
||||
amd: 'vue'
|
||||
return merge(
|
||||
baseConfig as any,
|
||||
{
|
||||
mode: 'production',
|
||||
entry: {
|
||||
[name]: join(DIST_DIR, 'index.js')
|
||||
},
|
||||
stats: 'none',
|
||||
output: {
|
||||
path: LIB_DIR,
|
||||
library: name,
|
||||
libraryTarget: 'umd',
|
||||
filename: isMinify ? '[name].min.js' : '[name].js',
|
||||
umdNamedDefine: true,
|
||||
// https://github.com/webpack/webpack/issues/6522
|
||||
globalObject: "typeof self !== 'undefined' ? self : this"
|
||||
},
|
||||
externals: {
|
||||
vue: {
|
||||
root: 'Vue',
|
||||
commonjs: 'vue',
|
||||
commonjs2: 'vue',
|
||||
amd: 'vue'
|
||||
}
|
||||
},
|
||||
performance: false,
|
||||
optimization: {
|
||||
minimize: isMinify
|
||||
}
|
||||
},
|
||||
performance: false,
|
||||
optimization: {
|
||||
minimize: isMinify
|
||||
}
|
||||
});
|
||||
getWebpackConfig()
|
||||
);
|
||||
}
|
||||
|
@ -3,53 +3,58 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
|
||||
import { join } from 'path';
|
||||
import { baseConfig } from './webpack.base';
|
||||
import { CONFIG_FILE } from '../common/constant';
|
||||
import { getWebpackConfig } from '../common';
|
||||
|
||||
// eslint-disable-next-line
|
||||
const config = require(CONFIG_FILE);
|
||||
const title = `${config.title} - ${config.description}`;
|
||||
|
||||
export const siteDevConfig = merge(baseConfig as any, {
|
||||
entry: {
|
||||
'site-desktop': join(__dirname, '../../site/desktop/main.js'),
|
||||
'site-mobile': join(__dirname, '../../site/mobile/main.js')
|
||||
},
|
||||
devServer: {
|
||||
open: true,
|
||||
host: '0.0.0.0',
|
||||
stats: 'errors-only',
|
||||
disableHostCheck: true,
|
||||
},
|
||||
output: {
|
||||
path: join(__dirname, '../../site/dist'),
|
||||
publicPath: '/',
|
||||
chunkFilename: 'async_[name].js'
|
||||
},
|
||||
optimization: {
|
||||
splitChunks: {
|
||||
cacheGroups: {
|
||||
chunks: {
|
||||
chunks: 'all',
|
||||
minChunks: 2,
|
||||
minSize: 0,
|
||||
name: 'chunks'
|
||||
export const siteDevConfig = merge(
|
||||
baseConfig as any,
|
||||
{
|
||||
entry: {
|
||||
'site-desktop': join(__dirname, '../../site/desktop/main.js'),
|
||||
'site-mobile': join(__dirname, '../../site/mobile/main.js')
|
||||
},
|
||||
devServer: {
|
||||
open: true,
|
||||
host: '0.0.0.0',
|
||||
stats: 'errors-only',
|
||||
disableHostCheck: true
|
||||
},
|
||||
output: {
|
||||
path: join(__dirname, '../../site/dist'),
|
||||
publicPath: '/',
|
||||
chunkFilename: 'async_[name].js'
|
||||
},
|
||||
optimization: {
|
||||
splitChunks: {
|
||||
cacheGroups: {
|
||||
chunks: {
|
||||
chunks: 'all',
|
||||
minChunks: 2,
|
||||
minSize: 0,
|
||||
name: 'chunks'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
title,
|
||||
logo: config.logo,
|
||||
chunks: ['chunks', 'site-desktop'],
|
||||
template: join(__dirname, '../../site/desktop/index.html'),
|
||||
filename: 'index.html'
|
||||
}),
|
||||
new HtmlWebpackPlugin({
|
||||
title,
|
||||
logo: config.logo,
|
||||
chunks: ['chunks', 'site-mobile'],
|
||||
template: join(__dirname, '../../site/mobile/index.html'),
|
||||
filename: 'mobile.html'
|
||||
})
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
title,
|
||||
logo: config.logo,
|
||||
chunks: ['chunks', 'site-desktop'],
|
||||
template: join(__dirname, '../../site/desktop/index.html'),
|
||||
filename: 'index.html'
|
||||
}),
|
||||
new HtmlWebpackPlugin({
|
||||
title,
|
||||
logo: config.logo,
|
||||
chunks: ['chunks', 'site-mobile'],
|
||||
template: join(__dirname, '../../site/mobile/index.html'),
|
||||
filename: 'mobile.html'
|
||||
})
|
||||
]
|
||||
});
|
||||
getWebpackConfig()
|
||||
);
|
||||
|
@ -1,13 +1,18 @@
|
||||
import { join } from 'path';
|
||||
import merge from 'webpack-merge';
|
||||
import { join } from 'path';
|
||||
import { siteDevConfig } from './webpack.site.dev';
|
||||
import { getWebpackConfig } from '../common';
|
||||
|
||||
export const sitePrdConfig = merge(siteDevConfig, {
|
||||
mode: 'production',
|
||||
output: {
|
||||
path: join(__dirname, '../../site/dist'),
|
||||
publicPath: 'https://b.yzcdn.cn/vant/',
|
||||
filename: '[name].[hash:8].js',
|
||||
chunkFilename: 'async_[name].[chunkhash:8].js'
|
||||
}
|
||||
});
|
||||
export const sitePrdConfig = merge(
|
||||
siteDevConfig,
|
||||
{
|
||||
mode: 'production',
|
||||
output: {
|
||||
path: join(__dirname, '../../site/dist'),
|
||||
publicPath: 'https://b.yzcdn.cn/vant/',
|
||||
filename: '[name].[hash:8].js',
|
||||
chunkFilename: 'async_[name].[chunkhash:8].js'
|
||||
}
|
||||
},
|
||||
getWebpackConfig()
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user