From 26068a35714f77a3f785fcb80ea3f32aa0eae671 Mon Sep 17 00:00:00 2001 From: xiaohuoni Date: Sun, 7 Apr 2024 10:19:32 +0800 Subject: [PATCH] feat: umd support mako bundler --- examples/mako/.fatherrc.ts | 14 ++++++++++++++ examples/mako/package.json | 15 +++++++++++++++ examples/mako/src/a.tsx | 15 +++++++++++++++ examples/mako/src/content.tsx | 7 +++++++ examples/mako/src/foo.ts | 1 + examples/mako/src/index.tsx | 17 +++++++++++++++++ examples/mako/tsconfig.json | 19 +++++++++++++++++++ pnpm-lock.yaml | 19 +++++++++++++++---- src/builder/bundle/mako.ts | 16 ++++++++++------ 9 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 examples/mako/.fatherrc.ts create mode 100644 examples/mako/package.json create mode 100644 examples/mako/src/a.tsx create mode 100644 examples/mako/src/content.tsx create mode 100644 examples/mako/src/foo.ts create mode 100644 examples/mako/src/index.tsx create mode 100644 examples/mako/tsconfig.json diff --git a/examples/mako/.fatherrc.ts b/examples/mako/.fatherrc.ts new file mode 100644 index 00000000..6dc08a0a --- /dev/null +++ b/examples/mako/.fatherrc.ts @@ -0,0 +1,14 @@ +import { defineConfig } from '../../dist'; +const path = require('path'); + +export default defineConfig({ + umd: { + bundler: 'mako', + }, + alias: { + '@': path.resolve(__dirname, './src'), + 'hello-a': path.resolve(__dirname, './src/a.tsx'), + 'hello-foo': path.resolve(__dirname, './src/foo.ts'), + }, + platform: 'browser', +}); diff --git a/examples/mako/package.json b/examples/mako/package.json new file mode 100644 index 00000000..9ed166a8 --- /dev/null +++ b/examples/mako/package.json @@ -0,0 +1,15 @@ +{ + "scripts": { + "build": "OKAM=@alipay/umi-bundler-okam father build", + "build:no-clean": "father build --no-clean", + "dev": "father dev", + "dev:no-clean": "father dev --no-clean", + "doctor": "father doctor", + "version": "father version" + }, + "dependencies": { + "father": "workspace:*", + "react": "^18.2.0", + "react-dom": "^18.2.0" + } +} diff --git a/examples/mako/src/a.tsx b/examples/mako/src/a.tsx new file mode 100644 index 00000000..ec27cce9 --- /dev/null +++ b/examples/mako/src/a.tsx @@ -0,0 +1,15 @@ +console.log('hello here'); + +// @ts-ignore +import React from 'react'; +// @ts-ignore +import ReactDOM from 'react-dom'; + +function App({content}:{content:string}) { + // @ts-ignore + return
{content}
; +} + +// @ts-ignore +const root = ReactDOM.createRoot(document.getElementById('root')); +root.render(); \ No newline at end of file diff --git a/examples/mako/src/content.tsx b/examples/mako/src/content.tsx new file mode 100644 index 00000000..1910985f --- /dev/null +++ b/examples/mako/src/content.tsx @@ -0,0 +1,7 @@ +class Content { + say() { + return 'Hello father 3'; + } +} + +export default new Content().say(); diff --git a/examples/mako/src/foo.ts b/examples/mako/src/foo.ts new file mode 100644 index 00000000..f70741a6 --- /dev/null +++ b/examples/mako/src/foo.ts @@ -0,0 +1 @@ +console.log('foo here'); diff --git a/examples/mako/src/index.tsx b/examples/mako/src/index.tsx new file mode 100644 index 00000000..540ea2b4 --- /dev/null +++ b/examples/mako/src/index.tsx @@ -0,0 +1,17 @@ +import content from '@/content'; +import 'hello-a' +import 'hello-foo' +/* +import React from 'react'; +import ReactDOM from 'react-dom'; + +// const content = 'Hello' +function App() { + return
{content}
; +} + +// @ts-ignore +const root = ReactDOM.createRoot(document.getElementById('root')); +root.render(); +*/ +console.log(content); diff --git a/examples/mako/tsconfig.json b/examples/mako/tsconfig.json new file mode 100644 index 00000000..739a5918 --- /dev/null +++ b/examples/mako/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "esModuleInterop": true, + "module": "commonjs", + "moduleResolution": "node", + "noUnusedLocals": true, + "noUnusedParameters": true, + "strict": true, + "skipLibCheck": true, + "target": "es2015", + "jsx": "react", + "baseUrl": ".", + "paths": { + "@/*": ["./src/*"] + } + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cbc81986..6522dfc4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -139,6 +139,18 @@ importers: specifier: ^4.0.0 version: 4.0.0 + examples/mako: + dependencies: + father: + specifier: workspace:* + version: link:../.. + react: + specifier: ^18.2.0 + version: 18.2.0 + react-dom: + specifier: ^18.2.0 + version: 18.2.0(react@18.2.0) + examples/normal: dependencies: father: @@ -4969,8 +4981,8 @@ packages: dependencies: once: 1.4.0 - /enhanced-resolve@5.13.0: - resolution: {integrity: sha512-eyV8f0y1+bzyfh8xAwW/WTSZpLbjhqc4ne9eGSH4Zo2ejdyiNG9pU6mf9DG8a7+Auk6MFTlNOT4Y2y/9k8GKVg==} + /enhanced-resolve@5.16.0: + resolution: {integrity: sha512-O+QWCviPNSSLAD9Ucn8Awv+poAkqn3T1XY5/N7kR7rQO9yfSGWkYZDwpJ+iKF7B8rxaQKWngSqACpgzeapSyoA==} engines: {node: '>=10.13.0'} dependencies: graceful-fs: 4.2.10 @@ -8446,7 +8458,6 @@ packages: loose-envify: 1.4.0 object-assign: 4.1.1 prop-types: 15.8.1 - bundledDependencies: false /react@18.2.0: resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==} @@ -9683,7 +9694,7 @@ packages: acorn-import-assertions: 1.8.0(acorn@8.7.1) browserslist: 4.21.5 chrome-trace-event: 1.0.3 - enhanced-resolve: 5.13.0 + enhanced-resolve: 5.16.0 es-module-lexer: 1.2.1 eslint-scope: 5.1.1 events: 3.3.0 diff --git a/src/builder/bundle/mako.ts b/src/builder/bundle/mako.ts index 074910d8..5bdc6346 100644 --- a/src/builder/bundle/mako.ts +++ b/src/builder/bundle/mako.ts @@ -1,8 +1,12 @@ -import path from 'path'; +import { tryPaths } from '@umijs/utils'; +import { join, parse } from 'path'; import { getCachePath } from '../../utils'; import type { BundleConfigProvider, IBundleConfig } from '../config'; import { getBabelPresetReactOpts, getBundleTargets } from '../utils'; import { logStatus } from './utils'; + +const extensions = ['.js', '.jsx', '.ts', '.tsx', '.cjs', '.mjs']; + interface IBundleOpts { cwd: string; configProvider: BundleConfigProvider; @@ -19,6 +23,10 @@ async function makoBundle( require('@umijs/bundler-webpack/dist/requireHook'); // @ts-ignore const { build, dev } = require(process.env.OKAM); + // mako need extension + const entry = tryPaths( + extensions.map((ext) => join(opts.cwd, `${config.entry}${ext}`)), + ); const options = { cwd: opts.cwd, config: { @@ -43,10 +51,7 @@ async function makoBundle( cacheDirectoryPath: getCachePath(), }, entry: { - [path.parse(config.output.filename).name]: path.join( - opts.cwd, - config.entry, - ), + [parse(config.output.filename).name]: entry, }, babelPreset: [ require.resolve('@umijs/babel-preset-umi'), @@ -81,7 +86,6 @@ async function makoBundle( if (!isFirstCompile) logStatus(config); }, }; - console.log(options); if (opts.watch) { return await dev(options);