Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: plugin compile config file replace __dirname__filename, import.meta.url #1609

Merged
merged 20 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/silent-bags-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
22 changes: 10 additions & 12 deletions examples/css-url/farm.config.ts
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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()]
});
14 changes: 6 additions & 8 deletions examples/react-ssr/farm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';

import { fileURLToPath } from 'url';
/**
* @type {import('@farmfe/core').UserConfig}
*/
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 2 additions & 5 deletions examples/react/farm.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig(() => {
sourcemap: true,
persistentCache: false,
presetEnv: false,
progress: false,
progress: false
// output: {
// publicPath: '/dist/'
// },
Expand All @@ -17,9 +17,6 @@ export default defineConfig(() => {
path: '/__farm_hmr'
}
},
plugins: [
'@farmfe/plugin-react',
'@farmfe/plugin-sass'
]
plugins: ['@farmfe/plugin-react', '@farmfe/plugin-sass']
};
});
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 10 additions & 40 deletions packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,7 +14,8 @@ import {
resolveAsyncPlugins,
resolveConfigHook,
resolveConfigResolvedHook,
resolveFarmPlugins
resolveFarmPlugins,
rustPluginResolver
} from '../plugin/index.js';
import { Server } from '../server/index.js';
import {
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export async function createCompiler(
rustPlugins,
compilation: compilationConfig
} = resolvedUserConfig;

const compiler = new Compiler(
{
config: compilationConfig,
Expand Down
Loading
Loading