diff --git a/.changeset/silent-bags-burn.md b/.changeset/silent-bags-burn.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/silent-bags-burn.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/examples/css-url/farm.config.ts b/examples/css-url/farm.config.ts index 07777baae..80252d2c7 100644 --- a/examples/css-url/farm.config.ts +++ b/examples/css-url/farm.config.ts @@ -1,20 +1,20 @@ import { resolve } from 'node:path'; -import type { UserConfig } from '@farmfe/core'; +import { defineConfig } from '@farmfe/core'; import postcss from '@farmfe/js-plugin-postcss'; -function defineConfig(config: UserConfig) { - return config; -} +console.log(__dirname); +console.log(__filename); +console.log(import.meta.url); export default defineConfig({ compilation: { input: { - index: './index.html', + index: './index.html' }, persistentCache: false, output: { path: './build', - publicPath: '/public-dir/', + publicPath: '/public-dir/' }, assets: { include: ['aaa'] @@ -23,15 +23,13 @@ export default defineConfig({ sourcemap: true, // treeShaking: true, // minify: true, - resolve:{ + resolve: { alias: { '/@': resolve(__dirname, 'src'), '@': resolve(__dirname, 'src') - }, + } } }, - server: { - open: true, - }, - plugins: ['@farmfe/plugin-react', '@farmfe/plugin-sass', postcss()], + server: {}, + plugins: ['@farmfe/plugin-react', '@farmfe/plugin-sass', postcss()] }); diff --git a/examples/react-ssr/farm.config.mjs b/examples/react-ssr/farm.config.mjs index 6b4b08239..184509026 100644 --- a/examples/react-ssr/farm.config.mjs +++ b/examples/react-ssr/farm.config.mjs @@ -1,5 +1,5 @@ import path from 'path'; - +import { fileURLToPath } from 'url'; /** * @type {import('@farmfe/core').UserConfig} */ @@ -30,20 +30,18 @@ export default { (server) => { server.app().use(async (ctx, next) => { await next(); - if (ctx.path === '/' || ctx.status === 404) { // console.log('ctx.path', ctx.path); const template = server .getCompiler() .resource('index_client.html') .toString(); - // console.log('html template', template); - console.log( - path.join(path.dirname(import.meta.url), 'dist', 'index.js') + const moudlePath = path.join( + path.dirname(import.meta.url), + 'dist', + 'index.js' ); - const render = await import( - path.join(path.dirname(import.meta.url), 'dist', 'index.js') - ).then((m) => m.default); + const render = await import(moudlePath).then((m) => m.default); const renderedHtml = render(ctx.path); // console.log(renderedHtml); const html = template.replace( diff --git a/examples/react/farm.config.js b/examples/react/farm.config.js index 5d275dcee..cd0c68071 100644 --- a/examples/react/farm.config.js +++ b/examples/react/farm.config.js @@ -6,7 +6,7 @@ export default defineConfig(() => { sourcemap: true, persistentCache: false, presetEnv: false, - progress: false, + progress: false // output: { // publicPath: '/dist/' // }, @@ -17,9 +17,6 @@ export default defineConfig(() => { path: '/__farm_hmr' } }, - plugins: [ - '@farmfe/plugin-react', - '@farmfe/plugin-sass' - ] + plugins: ['@farmfe/plugin-react', '@farmfe/plugin-sass'] }; }); diff --git a/packages/core/package.json b/packages/core/package.json index 932aca33d..79a07b8ad 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -93,14 +93,15 @@ "dotenv-expand": "^11.0.6", "execa": "^7.1.1", "farm-browserslist-generator": "^1.0.0", + "farm-plugin-replace-dirname": "0.2.1", "fast-glob": "^3.3.2", "fs-extra": "^11.1.1", + "http-proxy-middleware": "^3.0.0", "is-plain-object": "^5.0.0", "koa": "^2.13.4", "koa-compress": "^5.1.1", - "koa-static": "^5.0.0", - "http-proxy-middleware": "^3.0.0", "koa-connect": "^2.1.0", + "koa-static": "^5.0.0", "lodash.debounce": "^4.0.8", "loglevel": "^1.8.1", "mime-types": "^2.1.35", diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts index a5ae0ddc3..23bdaf997 100644 --- a/packages/core/src/config/index.ts +++ b/packages/core/src/config/index.ts @@ -5,10 +5,7 @@ import path, { isAbsolute, join } from 'node:path'; import { pathToFileURL } from 'node:url'; import { bindingPath } from '../../binding/index.js'; -import { - OutputConfig, - type PluginTransformHookParam -} from '../types/binding.js'; +import { OutputConfig } from '../types/binding.js'; import { JsPlugin } from '../index.js'; import { @@ -17,7 +14,8 @@ import { resolveAsyncPlugins, resolveConfigHook, resolveConfigResolvedHook, - resolveFarmPlugins + resolveFarmPlugins, + rustPluginResolver } from '../plugin/index.js'; import { Server } from '../server/index.js'; import { @@ -751,11 +749,16 @@ async function readConfigFile( 'development' ); + const replaceDirnamePlugin = await rustPluginResolver( + 'farm-plugin-replace-dirname', + normalizedConfig.root + ); + const compiler = new Compiler( { config: normalizedConfig, - jsPlugins: [replaceDirnamePlugin()], - rustPlugins: [] + jsPlugins: [], + rustPlugins: [replaceDirnamePlugin] }, logger ); @@ -999,39 +1002,6 @@ export async function getConfigFilePath( return undefined; } -// transform __dirname and __filename with resolve config file path -export function replaceDirnamePlugin() { - const moduleTypes = ['ts', 'js', 'cjs', 'mjs', 'mts', 'cts']; - const resolvedPaths: string[] = []; - return { - name: 'replace-dirname', - transform: { - filters: { - moduleTypes, - resolvedPaths - }, - async executor(param: PluginTransformHookParam) { - const { content, resolvedPath, moduleType } = param; - let replaceContent = content; - const dirPath = path.dirname(resolvedPath); - - replaceContent = param.content - .replace(/__dirname/g, JSON.stringify(dirPath)) - .replace(/__filename/g, JSON.stringify(resolvedPath)) - .replace( - /import\.meta\.url/g, - JSON.stringify(pathToFileURL(resolvedPath)) - ); - - return { - content: replaceContent, - moduleType - }; - } - } - }; -} - export async function resolvePlugins( userConfig: UserConfig, logger: Logger, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 43ebc4170..3fac0c1c2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -322,6 +322,7 @@ export async function createCompiler( rustPlugins, compilation: compilationConfig } = resolvedUserConfig; + const compiler = new Compiler( { config: compilationConfig, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d2a6b6be4..e93207c33 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2229,6 +2229,9 @@ importers: farm-browserslist-generator: specifier: ^1.0.0 version: 1.0.0 + farm-plugin-replace-dirname: + specifier: 0.2.1 + version: 0.2.1 fast-glob: specifier: ^3.3.2 version: 3.3.2 @@ -6053,9 +6056,6 @@ packages: resolution: {integrity: sha512-v+p6ongsrp0yTGbJXjgxPow2+DL93DASP4kXCDKb8/bwRtt9OEF3whggkkDkGNzgcWy2XaF4a8nZglC7uElscg==} engines: {node: '>=8'} - call-bind@1.0.5: - resolution: {integrity: sha512-C3nQxfFZxFRVoJoGKKI8y3MOEo129NQ+FgQ08iye+Mk4zNZZGdjfs06bVTr+DBSlA66Q2VEcMki/cUCP4SercQ==} - call-bind@1.0.7: resolution: {integrity: sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==} engines: {node: '>= 0.4'} @@ -7777,6 +7777,63 @@ packages: farm-plugin-remove-console@0.1.8: resolution: {integrity: sha512-t/QH2N97w2/VM4tj/hmnceU/95yYeORR5BnlWP91PherULRFhTVctECcC0JI33Psvyk06uSW4BuKgBPtktM5+w==} + farm-plugin-replace-dirname-darwin-arm64@0.2.1: + resolution: {integrity: sha512-9FThv/qoFuj3cJjv9P6YnXbBwPQ5TwGjnr50ejXdZn13Ehz0+7w7EscbRsZHNvT7p24p6i0Y9NUSallcWc2syw==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [darwin] + + farm-plugin-replace-dirname-darwin-x64@0.2.1: + resolution: {integrity: sha512-Msqrh8mAPBbEpANpa0z9uQBr1/MO+PaHgBxym/aNs1vpxB4KAs6JQWYKtO+Ob7JzFyV6d9lIRhpnpxzxTqSIfA==} + engines: {node: '>= 10'} + cpu: [x64] + os: [darwin] + + farm-plugin-replace-dirname-linux-arm64-gnu@0.2.1: + resolution: {integrity: sha512-ZKuxGu9G01CW521uTQHh+IP8pcT/NolGQfcQuEmBpD8epJ8per8Ps52fS05u5TGniaOg+ySZpt7HxbX+++k1YQ==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [linux] + + farm-plugin-replace-dirname-linux-arm64-musl@0.2.1: + resolution: {integrity: sha512-m3gH8ggczbRYTHZSNp3LjIQIcqhvDO4O78bxXc8O1ozKD8M47/YfQLyQV06M7H4rZ8s6XV3Bb1kAcRAASp3M5A==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [linux] + + farm-plugin-replace-dirname-linux-x64-gnu@0.2.1: + resolution: {integrity: sha512-MehKkoM2RFw3sCnEu9nCbXKjxtC3hfTad0h/dC+Z8iEBcLEReVLoNzHWWUa6BxkxqDtB82/BWO/ObSUj/VUnwQ==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + farm-plugin-replace-dirname-linux-x64-musl@0.2.1: + resolution: {integrity: sha512-o1qPZi16N/sHOteZYJVv6UmZFK3QKpVQrywk/4spJI0mPH9A9Y+G6iBE2Tqjb3d+1Hb6phr++EBJHZ2x1ajtGQ==} + engines: {node: '>= 10'} + cpu: [x64] + os: [linux] + + farm-plugin-replace-dirname-win32-arm64-msvc@0.2.1: + resolution: {integrity: sha512-Xn/wYFkgb7SsTlSaefFtvxNbXEVdvZB854b/rBZu47+MRQpSnBIPwnTGcqo8eNTMjtnY4beGGtcd78iqMVAHFQ==} + engines: {node: '>= 10'} + cpu: [arm64] + os: [win32] + + farm-plugin-replace-dirname-win32-ia32-msvc@0.2.1: + resolution: {integrity: sha512-YtIu5CS/BSgbQZb1qjaBg0cEKvB4vCIbBxNn64H468zwliPbE93SAIyiwu6cL3la59cjBP4sEbz4ZAWyY9GoMQ==} + engines: {node: '>= 10'} + cpu: [ia32] + os: [win32] + + farm-plugin-replace-dirname-win32-x64-msvc@0.2.1: + resolution: {integrity: sha512-KUAf4rcv3Nz+CpGs4zr+ZRu4hWRp7SHQBgpX+mb0hhMjRvn+LoWm2qCL2q9Gp3jsTDVmzjPbyZxp/9UJKx13lQ==} + engines: {node: '>= 10'} + cpu: [x64] + os: [win32] + + farm-plugin-replace-dirname@0.2.1: + resolution: {integrity: sha512-aJ4euQzxoq0sVu4AwXrNQflHJrSZdrdApGEyVRtN6KiCop3CHXnTg9ydlyCNXN2unQB283aNjojvCd5E/32KgA==} + farmup@0.0.12: resolution: {integrity: sha512-B8RMixABXp/ILgadg2wQ1BAXwcdOqKdF1K4aq6rYur+7MsrRRlj6g/Wvifil0Xs1zvAUKBGEdCGIcu9TL29Brg==} hasBin: true @@ -8065,9 +8122,6 @@ packages: get-installed-path@2.1.1: resolution: {integrity: sha512-Qkn9eq6tW5/q9BDVdMpB8tOHljX9OSP0jRC5TRNVA4qRc839t4g8KQaR8t0Uv0EFVL0MlyG7m/ofjEgAROtYsA==} - get-intrinsic@1.2.2: - resolution: {integrity: sha512-0gSo4ml/0j98Y3lngkFEot/zhiCeWsbYIlZ+uZOVgzLyLaUw7wxUL+nCTP0XJvJg1AXulJRI3UJi8GsbDuxdGA==} - get-intrinsic@1.2.4: resolution: {integrity: sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==} engines: {node: '>= 0.4'} @@ -15169,7 +15223,7 @@ snapshots: '@ljharb/through@2.3.11': dependencies: - call-bind: 1.0.5 + call-bind: 1.0.7 '@ljharb/through@2.3.13': dependencies: @@ -15650,7 +15704,7 @@ snapshots: '@ts-morph/common@0.19.0': dependencies: - fast-glob: 3.3.1 + fast-glob: 3.3.2 minimatch: 7.4.6 mkdirp: 2.1.6 path-browserify: 1.0.1 @@ -18477,12 +18531,6 @@ snapshots: normalize-url: 6.1.0 responselike: 2.0.1 - call-bind@1.0.5: - dependencies: - function-bind: 1.1.2 - get-intrinsic: 1.2.4 - set-function-length: 1.2.2 - call-bind@1.0.7: dependencies: es-define-property: 1.0.0 @@ -19317,9 +19365,9 @@ snapshots: deep-equal@2.2.3: dependencies: array-buffer-byte-length: 1.0.0 - call-bind: 1.0.5 + call-bind: 1.0.7 es-get-iterator: 1.1.3 - get-intrinsic: 1.2.2 + get-intrinsic: 1.2.4 is-arguments: 1.1.1 is-array-buffer: 3.0.2 is-date-object: 1.0.5 @@ -20210,6 +20258,49 @@ snapshots: farm-plugin-remove-console-win32-ia32-msvc: 0.1.8 farm-plugin-remove-console-win32-x64-msvc: 0.1.8 + farm-plugin-replace-dirname-darwin-arm64@0.2.1: + optional: true + + farm-plugin-replace-dirname-darwin-x64@0.2.1: + optional: true + + farm-plugin-replace-dirname-linux-arm64-gnu@0.2.1: + optional: true + + farm-plugin-replace-dirname-linux-arm64-musl@0.2.1: + optional: true + + farm-plugin-replace-dirname-linux-x64-gnu@0.2.1: + optional: true + + farm-plugin-replace-dirname-linux-x64-musl@0.2.1: + optional: true + + farm-plugin-replace-dirname-win32-arm64-msvc@0.2.1: + optional: true + + farm-plugin-replace-dirname-win32-ia32-msvc@0.2.1: + optional: true + + farm-plugin-replace-dirname-win32-x64-msvc@0.2.1: + optional: true + + farm-plugin-replace-dirname@0.2.1: + dependencies: + '@changesets/cli': 2.27.2 + '@farmfe/utils': 0.0.1 + cac: 6.7.14 + optionalDependencies: + farm-plugin-replace-dirname-darwin-arm64: 0.2.1 + farm-plugin-replace-dirname-darwin-x64: 0.2.1 + farm-plugin-replace-dirname-linux-arm64-gnu: 0.2.1 + farm-plugin-replace-dirname-linux-arm64-musl: 0.2.1 + farm-plugin-replace-dirname-linux-x64-gnu: 0.2.1 + farm-plugin-replace-dirname-linux-x64-musl: 0.2.1 + farm-plugin-replace-dirname-win32-arm64-msvc: 0.2.1 + farm-plugin-replace-dirname-win32-ia32-msvc: 0.2.1 + farm-plugin-replace-dirname-win32-x64-msvc: 0.2.1 + farmup@0.0.12: dependencies: '@farmfe/core': link:packages/core @@ -20536,13 +20627,6 @@ snapshots: dependencies: global-modules: 1.0.0 - get-intrinsic@1.2.2: - dependencies: - function-bind: 1.1.2 - has-proto: 1.0.1 - has-symbols: 1.0.3 - hasown: 2.0.0 - get-intrinsic@1.2.4: dependencies: es-errors: 1.3.0 @@ -20688,7 +20772,7 @@ snapshots: dependencies: array-union: 2.1.0 dir-glob: 3.0.1 - fast-glob: 3.3.1 + fast-glob: 3.3.2 ignore: 5.2.4 merge2: 1.4.1 slash: 3.0.0 @@ -25281,7 +25365,7 @@ snapshots: dependencies: '@rollup/pluginutils': 5.1.0(rollup@4.14.1) escape-string-regexp: 5.0.0 - fast-glob: 3.3.1 + fast-glob: 3.3.2 local-pkg: 0.4.3 magic-string: 0.30.10 mlly: 1.4.2