Merge branch 'dev' of codeup.aliyun.com:61c14a7efa282c88e103c23f/oak-cli into dev
This commit is contained in:
commit
066ff597e5
|
|
@ -19,11 +19,10 @@ module.exports = (babel) => {
|
|||
const { cwd, filename } = state;
|
||||
const rel = relative(cwd, filename).replace(/\\/g, '/');
|
||||
if (/(pages|components)[\w|\W]+(index\.tsx|index\.pc\.tsx)$/.test(rel)) {
|
||||
const lessFile = filename.replace(/\.ts$/, '.less');
|
||||
const lessFile = filename.replace(/\.(ts|tsx)$/, '.less');
|
||||
const lessFileExists = fs.existsSync(lessFile);
|
||||
const pcLessFile = filename.replace(/\.ts$/, '.pc.less');
|
||||
const pcLessFile = filename.replace(/\.(ts|tsx)$/, '.pc.less');
|
||||
const pcLessFileExists = fs.existsSync(pcLessFile);
|
||||
|
||||
const { body } = path.node;
|
||||
const lessFileImport = rel.endsWith('.pc.tsx') ?
|
||||
(pcLessFileExists ? './index.pc.less' : './index.less') :
|
||||
|
|
|
|||
|
|
@ -83,13 +83,16 @@ module.exports = (babel) => {
|
|||
if (tsxFileExists && pcTsxFileExists) {
|
||||
statements.push(
|
||||
t.ifStatement(
|
||||
t.booleanLiteral(true),
|
||||
t.blockStatement(
|
||||
renderStatements
|
||||
t.binaryExpression(
|
||||
'===',
|
||||
t.memberExpression(
|
||||
t.memberExpression(t.thisExpression(), t.identifier('props')),
|
||||
t.identifier('width')
|
||||
),
|
||||
t.stringLiteral('xs')
|
||||
),
|
||||
t.blockStatement(
|
||||
renderPcStatements
|
||||
)
|
||||
t.blockStatement(renderStatements),
|
||||
t.blockStatement(renderPcStatements)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
const fs = require('fs');
|
||||
const { relative } = require('path');
|
||||
const t = require('@babel/types');
|
||||
const pull = require('lodash/pull');
|
||||
const { assert } = require('console');
|
||||
const generate = require('@babel/generator').default;
|
||||
const parser = require("@babel/parser");
|
||||
|
||||
const defaultOptions = {
|
||||
baseDpr: 2, // base device pixel ratio (default: 2)
|
||||
rpxUnit: 750, // rpx unit value (default: 750)
|
||||
rpxPrecision: 6, // rpx value precision (default: 6)
|
||||
forceRpxComment: 'rpx', // force px comment (default: `rpx`)
|
||||
keepComment: 'no', // no transform value comment (default: `no`)
|
||||
};
|
||||
|
||||
const rpxRegExp = /\b(\d+(\.\d+)?)rpx\b/;
|
||||
|
||||
module.exports = (babel) => {
|
||||
return {
|
||||
visitor: {
|
||||
JSXAttribute(path, state) {
|
||||
const { cwd, filename } = state;
|
||||
if (
|
||||
path.node &&
|
||||
t.isJSXIdentifier(path.node.name) &&
|
||||
path.node.name.name === 'style'
|
||||
) {
|
||||
const properties =
|
||||
path.node.value &&
|
||||
path.node.value.expression &&
|
||||
path.node.value.expression.properties;
|
||||
|
||||
if (properties) {
|
||||
properties.forEach((node2, index) => {
|
||||
const { key, value } = node2;
|
||||
const code = generate(value);
|
||||
|
||||
function getValue(val) {
|
||||
return val == 0
|
||||
? val
|
||||
: `calc(100vw / ${defaultOptions.rpxUnit} * ${val})`;
|
||||
}
|
||||
|
||||
const rpxGlobalRegExp = new RegExp(
|
||||
rpxRegExp.source,
|
||||
'g'
|
||||
);
|
||||
let codeStr = code.code;
|
||||
if (rpxGlobalRegExp.test(codeStr)) {
|
||||
codeStr = codeStr.replace(
|
||||
rpxGlobalRegExp,
|
||||
function ($0, $1) {
|
||||
return getValue($1);
|
||||
}
|
||||
);
|
||||
console.log('code2', codeStr);
|
||||
|
||||
const ast2 = parser.parse(
|
||||
"30px",
|
||||
{
|
||||
sourceType: 'module',
|
||||
plugins: ['jsx'],
|
||||
}
|
||||
);
|
||||
console.log('ast2', JSON.stringify(ast2));
|
||||
|
||||
properties.splice(
|
||||
index,
|
||||
1,
|
||||
t.objectProperty(key, value)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
@ -1,7 +1,17 @@
|
|||
const { resolve, relative } = require('path');
|
||||
const rpxRegExp = /\b(\d+(\.\d+)?)rpx\b/;
|
||||
|
||||
module.exports = function (content) {
|
||||
/* const options = this.getOptions() || {}; //获取配置参数
|
||||
const defaultOptions = {
|
||||
baseDpr: 2, // base device pixel ratio (default: 2)
|
||||
rpxUnit: 750, // rpx unit value (default: 750)
|
||||
rpxPrecision: 6, // rpx value precision (default: 6)
|
||||
forceRpxComment: 'rpx', // force px comment (default: `rpx`)
|
||||
keepComment: 'no', // no transform value comment (default: `no`)
|
||||
};
|
||||
|
||||
module.exports = function (source) {
|
||||
const options = Object.assign(defaultOptions, this.getOptions()); //获取配置参数
|
||||
/*
|
||||
const { context: projectContext } = options; // context 本项目路径
|
||||
const {
|
||||
options: webpackLegacyOptions,
|
||||
|
|
@ -15,9 +25,22 @@ module.exports = function (content) {
|
|||
const issuerContext = (issuer && issuer.context) || context;
|
||||
const root = resolve(context, issuerContext);
|
||||
if (/.tsx|.jsx/.test(resourcePath)) {
|
||||
// console.log(content);
|
||||
// console.log(source);
|
||||
|
||||
} */
|
||||
// const { rpxUnit } = options;
|
||||
|
||||
return content;
|
||||
// function getValue(val) {
|
||||
// return val == 0 ? val : `calc(100vw / ${rpxUnit} * ${val})`;
|
||||
// }
|
||||
|
||||
// const rpxGlobalRegExp = new RegExp(rpxRegExp.source, 'g');
|
||||
// if (rpxGlobalRegExp.test(source)) {
|
||||
// return source.replace(rpxGlobalRegExp, function ($0, $1) {
|
||||
// return getValue($1);
|
||||
// });
|
||||
// }
|
||||
|
||||
return source;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,7 @@ function traverse(doc, callback) {
|
|||
const isSrc = (name) => name === 'src';
|
||||
|
||||
const isDynamicSrc = (src) => /\{\{/.test(src);
|
||||
const oakMessage = 'oak-message';
|
||||
const oakRegex = /(\/*[a-zA-Z0-9_-])*\/app\/|(\\*[a-zA-Z0-9_-])*\\app\\/;
|
||||
const localRegex = /(\/*[a-zA-Z0-9_-])*\/src+\/|(\\*[a-zA-Z0-9_-])*\\src+\\/;
|
||||
const TranslationFunction = 't';
|
||||
|
|
@ -117,6 +118,15 @@ function getWxsCode() {
|
|||
return [code, runner].join('\n');
|
||||
}
|
||||
|
||||
function getAppJson(context) {
|
||||
const JSON_PATH = require.resolve(`${context}/app.json`);
|
||||
if (!fs.existsSync(JSON_PATH)) {
|
||||
return;
|
||||
}
|
||||
const data = fs.readFileSync(JSON_PATH, 'utf8');
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
module.exports = async function (content) {
|
||||
// loader的缓存功能
|
||||
// this.cacheable && this.cacheable();
|
||||
|
|
@ -175,9 +185,16 @@ module.exports = async function (content) {
|
|||
}
|
||||
// 注入全局message组件
|
||||
if (/pages/.test(context)) {
|
||||
source =
|
||||
source +
|
||||
`<message show="{{!!oakError}}" type="{{oakError.type || ''}}" content="{{oakError.msg || ''}}" />`;
|
||||
const appJson = getAppJson(projectContext);
|
||||
if (
|
||||
appJson &&
|
||||
appJson.usingComponents &&
|
||||
appJson.usingComponents[oakMessage]
|
||||
) {
|
||||
source =
|
||||
source +
|
||||
`\n <${oakMessage} show="{{!!oakError}}" type="{{oakError.type || ''}}" content="{{oakError.msg || ''}}" ></${oakMessage}>`;
|
||||
}
|
||||
}
|
||||
|
||||
const doc = new DOMParser({
|
||||
|
|
|
|||
|
|
@ -11,14 +11,27 @@ const resolveApp = (relativePath) => path.resolve(resolveRoot(subDirName), relat
|
|||
|
||||
const buildPath = process.env.BUILD_PATH || 'dist';
|
||||
|
||||
const moduleFileExtensions = [
|
||||
let moduleFileExtensions = [
|
||||
'mp.js',
|
||||
'js',
|
||||
'mp.ts',
|
||||
'ts',
|
||||
'json',
|
||||
'wxs',
|
||||
];
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
moduleFileExtensions = [
|
||||
'dev.mp.js',
|
||||
'dev.mp.ts',
|
||||
'dev.js',
|
||||
'dev.ts',
|
||||
].concat(moduleFileExtensions);
|
||||
} else {
|
||||
moduleFileExtensions = [
|
||||
'prod.mp.js',
|
||||
'prod.mp.ts',
|
||||
'prod.js',
|
||||
'prod.ts',
|
||||
].concat(moduleFileExtensions);
|
||||
}
|
||||
|
||||
// Resolve file paths in the same order as webpack
|
||||
const resolveModule = (resolveFn, filePath) => {
|
||||
|
|
|
|||
|
|
@ -97,8 +97,8 @@ class Rpx2px {
|
|||
return val == 0 ? val : `calc(100vw / ${rpxUnit} * ${val})`;
|
||||
}
|
||||
|
||||
return value.replace(rpxGlobalRegExp, function (ele1, ele2) {
|
||||
return type === 'rpx' ? ele2 : getValue(ele2);
|
||||
return value.replace(rpxGlobalRegExp, function ($0, $1) {
|
||||
return type === 'rpx' ? $1 : getValue($1);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -25,19 +25,41 @@ const publicUrlOrPath = getPublicUrlOrPath(
|
|||
|
||||
const buildPath = process.env.BUILD_PATH || 'build';
|
||||
|
||||
const moduleFileExtensions = [
|
||||
'web.mjs',
|
||||
'mjs',
|
||||
'web.js',
|
||||
'js',
|
||||
'web.ts',
|
||||
'ts',
|
||||
'web.tsx',
|
||||
'tsx',
|
||||
'json',
|
||||
'web.jsx',
|
||||
'jsx',
|
||||
let moduleFileExtensions = [
|
||||
'web.mjs',
|
||||
'mjs',
|
||||
'web.js',
|
||||
'js',
|
||||
'web.ts',
|
||||
'pc.ts',
|
||||
'ts',
|
||||
'web.tsx',
|
||||
'pc.tsx',
|
||||
'tsx',
|
||||
'web.jsx',
|
||||
'pc.jsx',
|
||||
'jsx',
|
||||
];
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
moduleFileExtensions = [
|
||||
'dev.web.js',
|
||||
'dev.web.ts',
|
||||
'dev.web.tsx',
|
||||
'dev.js',
|
||||
'dev.ts',
|
||||
'dev.tsx',
|
||||
].concat(moduleFileExtensions);
|
||||
}
|
||||
else {
|
||||
moduleFileExtensions = [
|
||||
'prod.web.js',
|
||||
'prod.web.ts',
|
||||
'prod.web.tsx',
|
||||
'prod.js',
|
||||
'prod.ts',
|
||||
'prod.tsx',
|
||||
].concat(moduleFileExtensions);
|
||||
}
|
||||
|
||||
// Resolve file paths in the same order as webpack
|
||||
const resolveModule = (resolveFn, filePath) => {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,8 @@ const createEnvironmentHash = require('./webpack/persistentCache/createEnvironme
|
|||
const oakPathTsxPlugin = require('../babel-plugin/oakPath');
|
||||
const oakRenderTsxPlugin = require('../babel-plugin/oakRender');
|
||||
const oakRouterPlugin = require('../babel-plugin/router');
|
||||
const oakRpxToPxPlugin = require('../babel-plugin/oakRpxToPx');
|
||||
const oakStylePlugin = require('../babel-plugin/oakStyle');
|
||||
const oakRpxToPxPlugin = require('../postcss-plugin/oakRpxToPx');
|
||||
|
||||
// Source maps are resource heavy and can cause out of memory issue for large source files.
|
||||
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
|
||||
|
|
@ -492,6 +493,7 @@ module.exports = function (webpackEnv) {
|
|||
oakPathTsxPlugin,
|
||||
oakRenderTsxPlugin,
|
||||
oakRouterPlugin,
|
||||
// oakStylePlugin,
|
||||
],
|
||||
// This is a feature of `babel-loader` for webpack (not Babel itself).
|
||||
// It enables caching results in ./node_modules/.cache/babel-loader/
|
||||
|
|
|
|||
Loading…
Reference in New Issue