Skip to content

Commit

Permalink
fix: fix some esm build issue
Browse files Browse the repository at this point in the history
- add commonjs plugin to all output format
- install needed babel runtime deps to project
  • Loading branch information
longgui.wjb committed Jan 7, 2022
1 parent b80a414 commit ca618bb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
8 changes: 4 additions & 4 deletions packages/middleware-rollup/examples/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/middleware-rollup/examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@alifd/next": "1.20.28",
"@babel/runtime": "^7.16.7",
"@formily/core": "^2.0.0-rc.20",
"@formily/next": "^2.0.0-rc.20",
"@formily/react": "^2.0.0-rc.20",
Expand Down
5 changes: 4 additions & 1 deletion packages/middleware-rollup/examples/basic/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import "./sass-style.scss";

const Component: React.FC<{ foo?: string; bar?: number }> = () => {
useEffect(() => {
axios.get("https://alibaba.github.io/dawn/middleware.yml");
(async () => {
const ret = await axios.get("https://alibaba.github.io/dawn/middleware.yml");
console.log(ret);
})();
}, []);

return (
Expand Down
4 changes: 3 additions & 1 deletion packages/middleware-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"@dawnjs/types": "^2.0.1",
"@types/async": "^3.2.10",
"@types/lodash": "^4.14.177",
"@types/postcss-import": "^14.0.0"
"@types/postcss-import": "^14.0.0",
"@types/semver": "^7.3.9"
},
"dependencies": {
"@babel/core": "^7.16.0",
Expand Down Expand Up @@ -73,6 +74,7 @@
"rollup-plugin-typescript2": "^0.31.1",
"rollup-plugin-visualizer": "^5.5.2",
"sass": "^1.44.0",
"semver": "^7.3.5",
"tslib": "^2.3.1",
"typescript": "^4.5.2"
}
Expand Down
13 changes: 7 additions & 6 deletions packages/middleware-rollup/src/getRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,15 @@ export const getRollupConfig = async (
nodeResolve({
mainFields: ["module", "main"],
extensions,
...(target === "browser" ? { browser: true } : {}),
...(target === "browser" ? { browser: true, preferBuiltins: false } : { preferBuiltins: true }),
...nodeResolveOpts,
}),
type !== "dts" &&
commonjs({
transformMixedEsModules: true,
requireReturnsDefault: "auto",
...commonjsOpts,
}),
type !== "dts" &&
!disableTypescript &&
typescript2({
Expand Down Expand Up @@ -280,7 +286,6 @@ export const getRollupConfig = async (
!parallel && progress(),
];
};
const extraUmdPlugins = [commonjs(commonjsOpts)];

switch (type) {
case "esm":
Expand Down Expand Up @@ -375,7 +380,6 @@ export const getRollupConfig = async (
name: umd && umd.name,
},
plugins: [
...extraUmdPlugins,
...getPlugins(),
replace({
preventAssignment: true,
Expand Down Expand Up @@ -407,7 +411,6 @@ export const getRollupConfig = async (
name: umd && umd.name,
},
plugins: [
...extraUmdPlugins,
...getPlugins({ minCSS: true }),
replace({
preventAssignment: true,
Expand All @@ -432,7 +435,6 @@ export const getRollupConfig = async (
},
plugins: [
...getPlugins({ minCSS: (system && system.minify) || false }),
...extraUmdPlugins,
replace({
preventAssignment: true,
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -456,7 +458,6 @@ export const getRollupConfig = async (
},
plugins: [
...getPlugins({ minCSS: (iife && iife.minify) || false }),
...extraUmdPlugins,
replace({
preventAssignment: true,
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down
19 changes: 19 additions & 0 deletions packages/middleware-rollup/src/opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as assert from "assert";
import { basename, join } from "path";
import { existsSync } from "fs";
import { camelCase, merge } from "lodash";
import semver from "semver";
import { getExistFile } from "./utils";
import { IDawnContext, IOpts } from "./types";

Expand Down Expand Up @@ -91,4 +92,22 @@ export const validateOpts = async (opts: IOpts, ctx: IDawnContext): Promise<void
// );
}
}

if (opts.runtimeHelpers) {
let depName = "@babel/runtime";
if (opts.corejs) {
const corejsVersion = typeof opts.corejs === "number" ? opts.corejs : opts.corejs.version;
depName = `@babel/runtime-corejs${corejsVersion}`;
}
const depVersion = typeof opts.runtimeHelpers === "string" ? opts.runtimeHelpers : "*";

const installedDepVersion = ctx.project.dependencies?.[depName];
if (!installedDepVersion || !semver.satisfies(semver.minVersion(installedDepVersion), depVersion)) {
if (depVersion === "*") {
await ctx.mod.install(depName);
} else {
await ctx.mod.install(`${depName}@${depVersion}`);
}
}
}
};

0 comments on commit ca618bb

Please sign in to comment.