Skip to content

Commit

Permalink
Merge pull request #4010 from alibaba/release-next
Browse files Browse the repository at this point in the history
Release/1.4.1
  • Loading branch information
ClarkXia authored Jan 14, 2021
2 parents d744f81 + 50fde35 commit e12ba3e
Show file tree
Hide file tree
Showing 166 changed files with 4,139 additions and 1,440 deletions.
4 changes: 4 additions & 0 deletions examples/basic-mpa/src/pages/Detail/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(<h2>Detail Page</h2>, document.body);
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const projects = require('./projects');
import projects from './projects';

const status = 'SUCCESS';
// mock/index.js
module.exports = {
export default {
'GET /api/repo': {
status: 'SUCCESS',
status,
data: {
group: 'ice.js',
url: 'http://github.com/ice-lab/ice.js'
}
},

'GET /api/projects': {
status: 'SUCCESS',
data: projects
status,
data: projects
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
export default [
{
id: 1,
name: 'facebook/react',
Expand Down
5 changes: 3 additions & 2 deletions examples/basic-spa/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/camelcase */
// config runtime APP_MODE
// eslint-disable-next-line @typescript-eslint/camelcase
// @ts-ignore
window.__app_mode__ = 'build';

const config = {
Expand All @@ -16,4 +17,4 @@ const config = {
}
};

export default config;
export default config;
4 changes: 4 additions & 0 deletions examples/basic-spa/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '*.module.scss' {
const classes: { [key: string]: string };
export default classes;
}
2 changes: 1 addition & 1 deletion examples/basic-spa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"types": ["node"],
"types": ["node", "jest"],
"paths": {
"@/*": [
"./src/*"
Expand Down
2 changes: 1 addition & 1 deletion examples/basic-store/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"types": ["node"],
"types": ["node", "jest"],
"paths": {
"@/*": ["./src/*"],
"ice": [".ice/index.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-ice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @builder/babel-preset-ice
41 changes: 41 additions & 0 deletions packages/babel-preset-ice/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@builder/babel-preset-ice",
"version": "1.0.0",
"description": "Basic babel preset",
"main": "lib/index.js",
"types": "types/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"plugin",
"babel"
],
"author": "",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.2",
"@babel/plugin-proposal-do-expressions": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-function-bind": "^7.0.0",
"@babel/plugin-proposal-function-sent": "^7.1.0",
"@babel/plugin-proposal-json-strings": "^7.0.0",
"@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-proposal-numeric-separator": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-proposal-pipeline-operator": "^7.0.0",
"@babel/plugin-proposal-throw-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.4.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3"
}
}
53 changes: 53 additions & 0 deletions packages/babel-preset-ice/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
interface IOpts {
typescript?: boolean;
env?: object;
react?: boolean;
}
/**
* babel config
*/
function resolvePlugin(plugins) {
return plugins.filter(Boolean).map((plugin) => {
if (Array.isArray(plugin)) {
const [pluginName, ...args] = plugin;
return [require.resolve(pluginName), ...args];
}
return require.resolve(plugin);
});
}

export default (opts: IOpts = {}) => {
const plugins = [
// Stage 0
'@babel/plugin-proposal-function-bind',
// Stage 1
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-logical-assignment-operators',
['@babel/plugin-proposal-optional-chaining', { loose: false }],
['@babel/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],
['@babel/plugin-proposal-nullish-coalescing-operator', { loose: false }],
'@babel/plugin-proposal-do-expressions',
// Stage 2
['@babel/plugin-proposal-decorators', { legacy: true }],
'@babel/plugin-proposal-function-sent',
'@babel/plugin-proposal-export-namespace-from',
'@babel/plugin-proposal-numeric-separator',
'@babel/plugin-proposal-throw-expressions',
// Stage 3
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-syntax-import-meta',
['@babel/plugin-proposal-class-properties', { loose: true }],
'@babel/plugin-proposal-json-strings',
];

return {
presets: resolvePlugin([
opts.env && [
'@babel/preset-env', opts.env,
],
opts.typescript && '@babel/preset-typescript',
opts.react && '@babel/preset-react',
]),
plugins: resolvePlugin(plugins),
};
};
8 changes: 8 additions & 0 deletions packages/babel-preset-ice/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.settings.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"outDir": "lib"
}
}
6 changes: 4 additions & 2 deletions packages/build-app-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@builder/app-helpers",
"version": "2.0.3",
"version": "2.1.0",
"description": "build helpers for app framework",
"author": "[email protected]",
"homepage": "",
Expand All @@ -20,7 +20,9 @@
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"@babel/parser": "^7.12.11",
"@babel/traverse": "^7.12.12",
"fs-extra": "^8.1.0",
"lodash": "^4.17.20"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fse from 'fs-extra';
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';

function checkExportDefaultDeclarationExists(sourcePath: string) {
if (!fse.existsSync(sourcePath)) {
const ext = ['.ts', '.js', '.tsx', '.jsx'].find((extension) => fse.existsSync(`${sourcePath}${extension}`));
if (!ext) {
return;
}
sourcePath = `${sourcePath}${ext}`;
}
const code = fse.readFileSync(sourcePath, 'utf-8');
const ast = parseCode(code);

let exportDefaultDeclarationExists = false;
traverse(ast, {
ExportDefaultDeclaration() {
exportDefaultDeclarationExists = true;
},
});

return exportDefaultDeclarationExists;
}

function parseCode(code) {
return parse(code, {
sourceType: 'module',
plugins: ['jsx', 'typescript', 'decorators-legacy', 'dynamicImport', 'classProperties'],
});
}

export default checkExportDefaultDeclarationExists;
13 changes: 9 additions & 4 deletions packages/build-app-helpers/src/getMpaEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function getEntriesByJson(api, target, appJsonPath, appJsonContent): IEntry[] {
entryName = pageName.toLocaleLowerCase();
}
return {
entryPath: getPageEntryByAppJson(rootDir, route.source),
// routes.pageSource is absolute path, passed from the plugin-store
entryPath: route.pageSource || getPageEntryByAppJson(rootDir, route.source),
entryName,
pageName,
source: route.source,
Expand All @@ -57,14 +58,15 @@ function getPageEntryByAppJson(rootDir, source) {
if (!targetExt) {
throw new Error(`Cannot find target file ${absolutePath}.`);
}
return `${source}.${targetExt}`;
return `${absolutePath}.${targetExt}`;
}

function getEntriesByDir(api: any): IEntry[] {
const {
context: { rootDir },
} = api;
const pagesPath = path.join(rootDir, 'src/pages');
const srcPath = path.join(rootDir, 'src');
const pagesPath = path.join(srcPath, 'pages');
const pages = fs.existsSync(pagesPath)
? fs
.readdirSync(pagesPath)
Expand All @@ -75,11 +77,14 @@ function getEntriesByDir(api: any): IEntry[] {
const entries = pages.map((pageName) => {
const entryName = pageName.toLocaleLowerCase();
const pageEntry = getPageEntryByDir(pagesPath, pageName);
const source = `pages/${pageName}/${pageEntry}`;
const entryPath = path.join(srcPath, source);
if (!pageEntry) return null;
return {
entryName,
pageName,
entryPath: `pages/${pageName}/${pageEntry}`,
entryPath,
source,
};
}).filter(Boolean);
return entries;
Expand Down
1 change: 1 addition & 0 deletions packages/build-app-helpers/src/getRoutesByAppJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface IRoute {
source: string;
path: string;
name?: string;
pageSource?: string;
}
interface IStaticConfig {
routes: IRoute[];
Expand Down
1 change: 1 addition & 0 deletions packages/build-app-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as formatPath } from './formatPath';
export { default as validation } from './validation';
export { default as unionBy } from './unionBy';
export { default as injectTransformRuntime } from './injectTransformRuntime';
export { default as checkExportDefaultDeclarationExists } from './checkExportDefaultDeclarationExists';
8 changes: 4 additions & 4 deletions packages/build-mpa-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@builder/mpa-config",
"version": "2.0.1",
"version": "3.0.0",
"description": "enable mpa project for framework",
"author": "[email protected]",
"homepage": "",
Expand All @@ -20,8 +20,8 @@
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"@builder/app-helpers": "^2.0.0",
"fs-extra": "^8.1.0",
"loader-utils": "^2.0.0",
"@builder/app-helpers": "^2.0.0"
"loader-utils": "^2.0.0"
}
}
}
20 changes: 12 additions & 8 deletions packages/build-mpa-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as path from 'path';
import { formatPath } from '@builder/app-helpers';
import { formatPath, checkExportDefaultDeclarationExists } from '@builder/app-helpers';
import generateEntry from './generateEntry';

interface IEntries {
entryName: string;
entryPath: string;
source?: string;
path?: string;
}

interface IConfigOptions {
Expand All @@ -27,16 +29,18 @@ export const generateMPAEntries = (api, options: IConfigOptions) => {

const parsedEntries = {};
entries.forEach((entry) => {
const { entryName, entryPath } = entry;
const pageEntry = path.join(rootDir, 'src', entryPath);
const useOriginEntry = /app\.(t|j)sx?$/.test(entryPath) || type === 'node';
const { entryName, entryPath, source } = entry;
const pageEntry = entryPath;
const useOriginEntry = /app(\.(t|j)sx?)?$/.test(entryPath) || type === 'node';
const exportDefaultDeclarationExists = checkExportDefaultDeclarationExists(path.join(rootDir, 'src', source));
// icejs will config entry by api modifyUserConfig

let finalEntry = pageEntry;
if (!useOriginEntry) {
// when the source is not the custom render page or runApp, do not generate entry
if (exportDefaultDeclarationExists && !useOriginEntry) {
// generate mpa entries
finalEntry = generateEntry(api, { framework, targetDir, pageEntry, entryName });
}

parsedEntries[entryName] = {
...entry,
finalEntry,
Expand All @@ -60,11 +64,11 @@ const setMPAConfig = (api, config, options: IConfigOptions) => {
const matchStrs = [];

Object.keys(parsedEntries).forEach((entryKey) => {
const { entryName, entryPath, finalEntry } = parsedEntries[entryKey];
const { entryName, source, finalEntry } = parsedEntries[entryKey];
config.entry(entryName).add(finalEntry);

// get page paths for rule match
const matchStr = `src/${entryPath}`;
const matchStr = `src/${source}`;
matchStrs.push(formatPath(matchStr));
});

Expand Down
Loading

0 comments on commit e12ba3e

Please sign in to comment.