Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/initial state break #70

Merged
merged 12 commits into from
Mar 6, 2020
6 changes: 0 additions & 6 deletions packages/plugin-initial-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@
"license": "MIT",
"bugs": "http://github.com/umijs/plugins/issues",
"homepage": "https://github.com/umijs/plugins/tree/master/packages/plugin-initial-state#readme",
"dependencies": {
"es-module-lexer": "^0.3.17"
},
"peerDependencies": {
"umi": "3.x"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@types/es-module-lexer": "^0.3.0"
}
}
2 changes: 1 addition & 1 deletion packages/plugin-initial-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default (api: IApi) => {

const relEntryFile = relative(api.paths.cwd!, entryFile || '');

const enable = await shouldPluginEnable(entryFile);
const enable = shouldPluginEnable(entryFile);

api.writeTmpFile({
path: RELATIVE_MODEL_PATH,
Expand Down
34 changes: 28 additions & 6 deletions packages/plugin-initial-state/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import { readFileSync } from 'fs';
// @ts-ignore
import { init, parse } from 'es-module-lexer/dist/lexer.cjs';

export const shouldPluginEnable = async (entryFile?: string) => {
const { parser } = utils;

export const shouldPluginEnable = (entryFile?: string) => {
let hasExport = false;

if (entryFile) {
await init;
const fileContent = readFileSync(entryFile, 'utf-8');
const [_, exportsList] = parse(fileContent);
hasExport = exportsList.includes('getInitialState');
const ast = parser.parse(fileContent, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});

ast.program.body.forEach(ele => {
if (ele.type === 'ExportNamedDeclaration') {
try {
// export const xxx = () => {};
// export function xxx(){};
if ((ele.declaration[0].id.name = 'getInitialState')) {
hasExport = true;
}
} catch (e) {}

try {
// export { getInitialState };
if (
ele.specifiers.some(exp => exp.exported.name === 'getInitialState')
) {
hasExport = true;
}
} catch (e) {}
}
});
}

return hasExport;
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2683,11 +2683,6 @@
resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.0.1.tgz#ef3d3d8cf1710a69c3b25dde9bbc7b21ae7c5969"
integrity sha512-e8zAwnKg8lGy8MpAZx7gblNXY4agrAXH9YaAWYsMIfvusHdoS50WvwlcBb75JP8U2rg99PqxbEIstbiZSPAnEg==

"@types/es-module-lexer@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@types/es-module-lexer/-/es-module-lexer-0.3.0.tgz#9fee3f19f64e6b3f999eeb3a70bd177a4d57a6cb"
integrity sha512-XI3MGSejUQIJ3wzY0i5IHy5J3eb36M/ytgG8jIOssP08ovtRPcjpjXQqrx51AHBNBOisTS/NQNWJitI17+EwzQ==

"@types/estree@*":
version "0.0.42"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.42.tgz#8d0c1f480339efedb3e46070e22dd63e0430dd11"
Expand Down Expand Up @@ -6488,11 +6483,6 @@ es-abstract@^1.17.0-next.1, es-abstract@^1.17.2:
string.prototype.trimleft "^2.1.1"
string.prototype.trimright "^2.1.1"

es-module-lexer@^0.3.17:
version "0.3.17"
resolved "https://registry.yarnpkg.com/es-module-lexer/-/es-module-lexer-0.3.17.tgz#a248dec2870934d9054420fead19db095ea21537"
integrity sha512-nwvMtzyEB6FhlyXBlV+BW2By3Vn2sUvlQBYP4LvdK8YpdbFQUOiBoeuB7/ip1+EbjmgNydkJ8+dIlyO09VP9BA==

es-to-primitive@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/es-to-primitive/-/es-to-primitive-1.2.1.tgz#e55cd4c9cdc188bcefb03b366c736323fc5c898a"
Expand Down