npm i --save-dev html-webpack-cdn
yarn add --dev html-webpack-cdn
// 无需配置 webpack externals
// modules 为空则不会生效
const WebpackHtmlCdnPlugin = require("html-webpack-cdn");
const cdnEnv = process.env.NODE_ENV === 'development' ? 'development' : 'production.min';
new HtmlWebpackCdnPlugin({
modules: [
{ name: 'react', _var: 'React', path: `umd/react.${cdnEnv}.js` },
{ name: 'react-dom', _var: 'ReactDOM', path: `umd/react-dom.${cdnEnv}.js` },
],
})
<!-- html 中效果 -->
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script>