Skip to content

Commit 1e54ca5

Browse files
authored
feat: bundler-webpack output support esm (umijs#172)
* add esm config * change script type when Enable esm * feat:support output esm-file * update pnpm-lock.yaml
1 parent c671396 commit 1e54ca5

File tree

10 files changed

+64
-1
lines changed

10 files changed

+64
-1
lines changed

examples/boilerplate/.umirc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export default {
1414
headScripts: [`console.log('head script')`],
1515
scripts: [`console.log('script')`],
1616
npmClient: 'pnpm',
17+
esm: {},
1718
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
// externals: {
3+
// react: 'React',
4+
// 'react-dom': 'ReactDOM',
5+
// },
6+
esm: {},
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
8+
/>
9+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
10+
<title>Document</title>
11+
</head>
12+
<body>
13+
<div id="root"></div>
14+
<script src="https://gw.alipayobjects.com/os/lib/react/18.0.0-alpha-fd5e01c2e-20210913/umd/react.production.min.js"></script>
15+
<script src="https://gw.alipayobjects.com/os/lib/react-dom/18.0.0-alpha-fd5e01c2e-20210913/umd/react-dom.production.min.js"></script>
16+
<script type="module" src="./dist/index.js"></script>
17+
</body>
18+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
function App() {
5+
return <div>ESM App</div>;
6+
}
7+
8+
// @ts-ignore
9+
const root = ReactDOM.createRoot(document.getElementById('root'));
10+
root.render(<App />);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"scripts": {
3+
"dev": "bundler-webpack dev",
4+
"build": "bundler-webpack build"
5+
},
6+
"devDependencies": {
7+
"@types/react": "^17.0.33",
8+
"@types/react-dom": "^17.0.10",
9+
"react": "^18.0.0-alpha-fd5e01c2e-20210913",
10+
"react-dom": "^18.0.0-alpha-fd5e01c2e-20210913"
11+
}
12+
}

packages/bundler-webpack/src/config/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export async function getConfig(opts: IOpts): Promise<Configuration> {
127127
// experiments
128128
config.experiments({
129129
topLevelAwait: true,
130+
outputModule: !!userConfig.esm,
130131
});
131132

132133
// node polyfill

packages/bundler-webpack/src/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,6 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
117117
svgo: (Joi) => Joi.alternatives().try(Joi.object(), Joi.boolean()),
118118
targets: (Joi) => Joi.object(),
119119
writeToDisk: (Joi) => Joi.boolean(),
120+
esm: (Joi) => Joi.object(),
120121
};
121122
}

packages/bundler-webpack/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface IConfig {
4848
depTranspiler?: Transpiler;
4949
devtool?: Config.DevTool;
5050
externals?: WebpackConfig['externals'];
51+
esm?: { [key: string]: any };
5152
extraBabelPlugins?: IBabelPlugin[];
5253
extraBabelPresets?: IBabelPlugin[];
5354
extraPostCSSPlugins?: any[];

packages/preset-umi/src/commands/build.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ umi build --clean
110110
const markup = await getMarkup({
111111
...markupArgs,
112112
scripts: ['/umi.js'].concat(markupArgs.scripts),
113-
esmScript: vite,
113+
esmScript: !!opts.config.esm || vite,
114114
path: '/',
115115
});
116116
writeFileSync(

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)