webpack.config 配置调整

This commit is contained in:
Wang Kejun 2022-12-07 12:27:01 +08:00
parent e1fd1a28b9
commit be3cd00709
1 changed files with 6 additions and 119 deletions

View File

@ -42,24 +42,12 @@ const copyPatterns = [].concat(pkg.copyWebpack || []).map((pattern) =>
}
: pattern
);
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/lib\/|(\\*[a-zA-Z0-9_-])*\\lib\\/;
const localRegex = /(\/*[a-zA-Z0-9_-])*\/src+\/|(\\*[a-zA-Z0-9_-])*\\src+\\/;
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/(lib|src)\/|(\\*[a-zA-Z0-9_-])*\\(lib|src)\\/;
module.exports = function (webpackEnv) {
const isEnvDevelopment = webpackEnv === 'development';
const isEnvProduction = webpackEnv === 'production';
const relativeFileLoader = (ext = '[ext]') => {
return {
loader: 'file-loader',
options: {
useRelativePath: true,
name: `[path][name].${ext}`,
context: paths.appSrc,
},
};
};
const oakFileLoader = (ext = '[ext]') => {
return {
loader: 'file-loader',
@ -75,21 +63,6 @@ module.exports = function (webpackEnv) {
};
};
const localFileLoader = (ext = '[ext]') => {
return {
loader: 'file-loader',
options: {
useRelativePath: true,
name: `[path][name].${ext}`,
outputPath: (url, resourcePath, context) => {
const outputPath = url.replace(localRegex, '');
return outputPath;
},
context: paths.appSrc,
},
};
};
const getOakInclude = () => {
return isEnvProduction
? [/oak-general-business/, /oak-frontend-base/]
@ -181,59 +154,24 @@ module.exports = function (webpackEnv) {
},
module: {
rules: [
{
test: /\.wxs$/,
include: paths.appSrc,
type: 'javascript/auto',
use: [relativeFileLoader()],
},
{
test: /\.wxs$/,
include: oakRegex,
exclude: paths.appSrc,
type: 'javascript/auto',
use: [oakFileLoader('wxs')],
},
{
test: /\.wxs$/,
include: paths.appRootSrc,
exclude: /node_modules/,
type: 'javascript/auto',
use: [oakFileLoader('wxs')],
},
{
test: /\.(png|jpg|gif|svg)$/,
include: paths.appSrc,
type: 'javascript/auto',
use: relativeFileLoader(),
},
{
test: /\.(png|jpg|gif|svg)$/,
include: oakRegex,
exclude: paths.appSrc,
exclude: /node_modules/,
type: 'javascript/auto',
use: oakFileLoader(),
},
{
test: /\.(png|jpg|gif|svg)$/,
include: paths.appRootSrc,
type: 'javascript/auto',
use: localFileLoader(),
},
{
test: /\.less$/,
include: paths.appSrc,
exclude: /node_modules/,
use: [
relativeFileLoader('wxss'),
{
loader: 'less-loader',
},
],
},
{
test: /\.less$/,
include: oakRegex,
exclude: paths.appSrc,
exclude: /node_modules/,
type: 'javascript/auto',
use: [
oakFileLoader('wxss'),
@ -251,26 +189,6 @@ module.exports = function (webpackEnv) {
},
],
},
{
test: /\.less$/,
include: paths.appRootSrc,
type: 'javascript/auto',
use: [
localFileLoader('wxss'),
{
loader: 'less-loader',
options: {
lessOptions: () => {
const oakConfigJson = require(paths.oakConfigJson);
return {
javascriptEnabled: true,
modifyVars: oakConfigJson.theme,
};
},
},
},
],
},
{
test: /\.js$/,
include: [paths.appSrc, paths.appRootSrc].concat(
@ -285,7 +203,7 @@ module.exports = function (webpackEnv) {
},
},
{
test: /\.((?!tsx)ts)$/,
test: /\.ts$/,
include: [paths.appSrc, paths.appRootSrc].concat(
getOakInclude()
),
@ -322,25 +240,9 @@ module.exports = function (webpackEnv) {
// },
{
test: /\.(xml|wxml)$/,
include: paths.appSrc,
include: oakRegex,
exclude: /node_modules/,
type: 'javascript/auto',
use: [
relativeFileLoader('wxml'),
{
loader: 'wxml-loader',
options: {
context: paths.appSrc,
cacheDirectory: false,
},
},
],
},
{
test: /\.(xml|wxml)$/,
include: oakRegex,
exclude: paths.appSrc,
type: 'javascript/auto',
use: [
oakFileLoader('wxml'),
{
@ -352,21 +254,6 @@ module.exports = function (webpackEnv) {
},
],
},
{
test: /\.(xml|wxml)$/,
include: paths.appRootSrc,
type: 'javascript/auto',
use: [
localFileLoader('wxml'),
{
loader: 'wxml-loader',
options: {
context: paths.appSrc,
cacheDirectory: false,
},
},
],
},
],
},
plugins: [