Skip to content

Commit 4f9a8ad

Browse files
feat: nocode plugin support style-lodaer & butter registry (#839)
Co-authored-by: targeral <[email protected]>
1 parent 5cd1829 commit 4f9a8ad

File tree

9 files changed

+146
-21
lines changed

9 files changed

+146
-21
lines changed

Diff for: .changeset/unlucky-experts-deliver.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modern-js/plugin-nocode': patch
3+
---
4+
5+
nocode plugin support style-loader

Diff for: packages/cli/plugin-nocode/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"semver": "^7.3.5",
6161
"signale": "^1.4.0",
6262
"slash": "^3.0.0",
63+
"style-loader": "^3.3.1",
6364
"uuid": "^8.3.2",
6465
"webpack": "^5.65.0",
6566
"webpack-sources": "^3.2.2"

Diff for: packages/cli/plugin-nocode/src/cli.ts

+29-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
import { PLUGIN_SCHEMAS } from '@modern-js/utils';
1+
import { join } from 'path';
2+
import { Import, PLUGIN_SCHEMAS } from '@modern-js/utils';
23
import { WebpackConfigTarget, getWebpackConfig } from '@modern-js/webpack';
34
import type { Configuration } from 'webpack';
4-
import type { CliPlugin } from '@modern-js/core';
5+
import { MODE, STARRY_MODEL_RUNTIME } from './contants';
56
import dev from './dev';
67
import { register } from './register';
78

9+
const core: typeof import('@modern-js/core') = Import.lazy(
10+
'@modern-js/core',
11+
require,
12+
);
813
process.env.RUN_PLATFORM = 'true';
914

10-
export default (): CliPlugin => ({
11-
name: '@modern-js/plugin-nocode',
12-
setup: api => ({
15+
const getMode = (appDirectory: string) => {
16+
let mode = MODE.BLOCK;
17+
try {
18+
const { dependencies } = require(join(appDirectory, 'package.json'));
19+
if (STARRY_MODEL_RUNTIME in dependencies) {
20+
mode = MODE.MODEL;
21+
}
22+
} catch (err) {}
23+
return mode;
24+
};
25+
26+
export default core.createPlugin(
27+
() => ({
1328
commands({ program }) {
1429
program
1530
.command('deploy [subcmd]')
@@ -26,8 +41,8 @@ export default (): CliPlugin => ({
2641
});
2742

2843
const devCommand = program.commandsMap.get('dev');
29-
const { appDirectory, internalDirectory } = api.useAppContext();
30-
const modernConfig = api.useResolvedConfigContext();
44+
const { appDirectory, internalDirectory } = core.useAppContext();
45+
const modernConfig = core.useResolvedConfigContext();
3146
if (devCommand) {
3247
devCommand.command('nocode').action(async () => {
3348
const webpackConfig = getWebpackConfig(
@@ -38,14 +53,15 @@ export default (): CliPlugin => ({
3853
internalDirectory,
3954
webpackConfig,
4055
modernConfig,
56+
getMode(appDirectory),
4157
);
4258
});
4359
}
4460
},
4561
validateSchema() {
4662
return PLUGIN_SCHEMAS['@modern-js/plugin-nocode'];
4763
},
48-
platformBuild() {
64+
platformBuild({ isTsProject: _ }) {
4965
return {
5066
name: 'nocode',
5167
title: 'Run Nocode log',
@@ -62,8 +78,8 @@ export default (): CliPlugin => ({
6278
}: {
6379
isTsProject: boolean;
6480
}) => {
65-
const { appDirectory, internalDirectory } = api.useAppContext();
66-
const modernConfig = api.useResolvedConfigContext();
81+
const { appDirectory, internalDirectory } = core.useAppContext();
82+
const modernConfig = core.useResolvedConfigContext();
6783
const webpackConfig = getWebpackConfig(
6884
WebpackConfigTarget.CLIENT,
6985
) as Configuration;
@@ -72,9 +88,11 @@ export default (): CliPlugin => ({
7288
internalDirectory,
7389
webpackConfig,
7490
modernConfig,
91+
getMode(appDirectory),
7592
);
7693
},
7794
};
7895
},
7996
}),
80-
});
97+
{ name: '@modern-js/plugin-nocode' },
98+
) as any;

Diff for: packages/cli/plugin-nocode/src/compiler/index.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as path from 'path';
2-
import type { Configuration, WebpackPluginInstance } from 'webpack';
2+
import type {
3+
Configuration,
4+
RuleSetRule,
5+
WebpackPluginInstance,
6+
} from 'webpack';
37
import { fs, logger } from '@modern-js/utils';
48
import webpack from 'webpack';
59
import { EDITOR_ENTRY, DEFAULT_ENTRY, MODE } from '../contants';
@@ -16,21 +20,18 @@ const getEntryFile = (prefix: string) => {
1620
return prefix;
1721
};
1822

19-
const compile = async (
23+
export const handleWebpackConfig = (
2024
webpackConfig: Configuration,
2125
{
2226
umdEntryFile,
23-
editorEntryFile,
2427
appDirectory,
2528
isDev,
2629
}: {
2730
appDirectory: string;
2831
umdEntryFile: string;
29-
editorEntryFile: string;
3032
isDev: boolean;
3133
},
3234
) => {
33-
logger.info('compile UMD entry', umdEntryFile);
3435
webpackConfig.externals = [
3536
'react',
3637
'react/jsx-runtime',
@@ -41,6 +42,9 @@ const compile = async (
4142
'@modern-js-reduck/react',
4243
'@modern-js-reduck/store',
4344
'@modern-js-block/runtime',
45+
'@modern-js-model/runtime',
46+
// 星夜区块单独发布的 reduck 版本
47+
'@modern-js-model/reduck-core',
4448
'@modern-js/runtime',
4549
'@modern-js/runtime-core',
4650
'@modern-js/plugin-router',
@@ -100,6 +104,29 @@ const compile = async (
100104
webpackConfig.plugins = webpackConfig.plugins.filter(
101105
p => p.constructor.name !== 'HtmlWebpackPlugin',
102106
);
107+
(webpackConfig?.module?.rules as RuleSetRule[])?.[1]?.oneOf?.forEach(rule => {
108+
if (
109+
Array.isArray(rule.use) &&
110+
typeof rule.use[0] === 'object' &&
111+
(rule.use[0]?.loader || '').includes('mini-css-extract-plugin')
112+
) {
113+
rule.use[0].loader = 'style-loader';
114+
}
115+
});
116+
};
117+
118+
const compile = async (
119+
webpackConfig: Configuration,
120+
options: {
121+
appDirectory: string;
122+
umdEntryFile: string;
123+
editorEntryFile: string;
124+
isDev: boolean;
125+
},
126+
) => {
127+
const { umdEntryFile, editorEntryFile, isDev } = options;
128+
logger.info('compile UMD entry', umdEntryFile);
129+
handleWebpackConfig(webpackConfig, options);
103130
await build(webpackConfig, { editorEntryFile, isDev });
104131
};
105132

Diff for: packages/cli/plugin-nocode/src/contants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ export const MODE = {
55
MODEL: 'MODEL',
66
BLOCK: 'BLOCK',
77
};
8+
9+
export const STARRY_BLOCK_RUNTIME = '@modern-js-block/runtime';
10+
export const STARRY_MODEL_RUNTIME = '@modern-js-model/runtime';

Diff for: packages/cli/plugin-nocode/src/register/butter-auth.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ const checkLoggedIn = async (token: any) => {
3535
return res.data.isLogin;
3636
};
3737

38-
const checkAuth = async (token: any) => {
38+
const checkAuth = async (token: string) => {
3939
const isLoggedIn = await checkLoggedIn(token);
4040
if (!isLoggedIn) {
4141
throw new Error('not logged in yet');
4242
}
4343
return token;
4444
};
4545

46-
const waitForAuth = async (token: any, time = 0) => {
46+
const waitForAuth: (token: string, time?: number) => Promise<string> = async (
47+
token: string,
48+
time = 0,
49+
) => {
4750
await delay(AUTH_WAIT_INTERVAL);
4851

4952
if (time >= AUTH_WAIT_COUNT) {
@@ -53,7 +56,6 @@ const waitForAuth = async (token: any, time = 0) => {
5356
try {
5457
return await checkAuth(token);
5558
} catch (err) {
56-
// eslint-disable-next-line @typescript-eslint/return-await
5759
return await waitForAuth(token, time + 1);
5860
}
5961
};
@@ -91,7 +93,7 @@ const readLocalConfig = async () => {
9193
return data;
9294
};
9395

94-
const writeLocalConfig = async data =>
96+
const writeLocalConfig = async (data: any) =>
9597
fs.writeJson(BUTTER_CONFIG, data, { spaces: 2 });
9698

9799
const readLocalToken = async () => {

Diff for: packages/cli/plugin-nocode/src/register/env.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const SERVICE_ID = isStaging ? STG_SERVICE_ID : PROD_SERVICE_ID;
1414
// if (isLocalDebug) {
1515
// BUTTER_HOST = 'http://localhost:3000';
1616
// }
17-
const BUTTER_HOST = `https://butter.bytedance.com`;
17+
const BUTTER_HOST = process.env.BUTTER_HOST || `https://butter.bytedance.com`;
1818

1919
const BUTTER_REGISTER_ENDPOINT = `${BUTTER_HOST}/api/v2/register`;
2020
const BUTTER_UNREGISTER_ENDPOINT = `${BUTTER_HOST}/api/v2/unregister`;
@@ -28,6 +28,7 @@ const BUTTER_TYPE_ROUTE_MAP = {
2828
PLUGIN: 'plugins',
2929
PRESET: 'presets',
3030
BLOCK: 'components',
31+
MODEL: 'models',
3132
};
3233

3334
export {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import path from 'path';
2+
import { BaseWebpackConfig } from '@modern-js/webpack';
3+
import { defaultsConfig, IAppContext } from '@modern-js/core';
4+
import { RuleSetRule } from 'webpack';
5+
import { handleWebpackConfig } from '../src/compiler';
6+
7+
export const userConfig: any = {
8+
...defaultsConfig,
9+
};
10+
11+
const fixtures = path.resolve(__dirname, './');
12+
const appContext = {
13+
appDirectory: fixtures,
14+
internalDirectory: '/node_modules/.modern-js',
15+
srcDirectory: '/src',
16+
sharedDirectory: './shared',
17+
entrypoints: [
18+
{
19+
entryName: 'page-a',
20+
entry: path.resolve(fixtures, './demo/src/page-a/index.jsx'),
21+
},
22+
],
23+
internalDirAlias: '@_modern_js_internal',
24+
internalSrcAlias: '@_modern_js_src',
25+
};
26+
27+
describe('plugin-nocode', () => {
28+
it('expect add style-loader to webpack config in nocode plugin', () => {
29+
try {
30+
userConfig._raw = userConfig;
31+
userConfig.source.include = ['query-string'];
32+
const config = new BaseWebpackConfig(
33+
appContext as IAppContext,
34+
userConfig,
35+
).config();
36+
37+
const hasStyleLoader = config?.module?.rules?.[1]?.oneOf.findIndex(
38+
(rule: RuleSetRule) => {
39+
return (
40+
Array.isArray(rule.use) &&
41+
typeof rule.use[0] === 'object' &&
42+
rule?.use?.[0]?.loader === 'style-loader'
43+
);
44+
},
45+
);
46+
expect(hasStyleLoader).toBeLessThan(0);
47+
handleWebpackConfig(config, {
48+
isDev: true,
49+
appDirectory: appContext.appDirectory,
50+
umdEntryFile: './',
51+
});
52+
53+
const hasStyleLoaderAfterHandled =
54+
config?.module?.rules?.[1]?.oneOf.findIndex((rule: RuleSetRule) => {
55+
return (
56+
Array.isArray(rule.use) &&
57+
typeof rule.use[0] === 'object' &&
58+
rule.use[0].loader === 'style-loader'
59+
);
60+
});
61+
expect(hasStyleLoaderAfterHandled).toBeGreaterThanOrEqual(0);
62+
} catch (e) {
63+
console.error(e);
64+
}
65+
});
66+
});

Diff for: pnpm-lock.yaml

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

0 commit comments

Comments
 (0)