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

Rename option.generatePreloadJsList to enableNamedRegisterForSystemJSModuleFormat. #61

Merged
merged 2 commits into from
Mar 4, 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
7 changes: 5 additions & 2 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "@cocos/ccbuild" {

Check warning on line 1 in .api/public.d.ts

View workflow job for this annotation

GitHub Actions / test

File ignored by default.
/**
* @group Merged Types
*/
Expand Down Expand Up @@ -130,9 +130,12 @@
*/
generateDecoratorsForJSB?: boolean;
/**
* Whether to generate a json file that contains all output script file paths.
* Whether to generate 'named' register code for systemjs module format.
* SystemJS default register code: System.register([], function(){...});
* SystemJS named register code: System.register('module_name', [], function(){...});
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
generatePreloadJsList?: boolean;
enableNamedRegisterForSystemJSModuleFormat?: boolean;
}
export interface Result {
/**
Expand Down
7 changes: 7 additions & 0 deletions modules/build-engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/babel__core": "7.1.20",
"@types/babel__generator": "7.6.4",
"@types/babel__traverse": "7.18.2",
"@babel/core": "^7.20.12",
"@babel/parser": "^7.20.13",
"@babel/traverse": "^7.20.13",

"@types/resolve": "^1.20.2",
"dedent": "^0.7.0",
"fs-extra": "~11.1.1",
Expand Down
11 changes: 1 addition & 10 deletions modules/build-engine/src/engine-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
presetEnvOptions.targets = options.targets;
}

const babelPlugins: any[] = [];

Check warning on line 177 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (!options.targets) {
babelPlugins.push([babelPluginTransformForOf, {
loose: true,
Expand Down Expand Up @@ -230,7 +230,7 @@
if (!process.env.ENGINE_PATH) {
throw new Error('ENGINE_PATH environment variable not set');
}
babelOptions.presets?.push([(): any => ({ plugins: [[decoratorRecorder]] })]);

Check warning on line 233 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}

const rollupPlugins: rollup.Plugin[] = [];
Expand All @@ -253,7 +253,7 @@

{
name: '@cocos/ccbuild|module-overrides',
resolveId(source, importer): string | null {

Check warning on line 256 in modules/build-engine/src/engine-js/index.ts

View workflow job for this annotation

GitHub Actions / test

'importer' is defined but never used
if (moduleOverrides[source]) {
return source;
} else {
Expand Down Expand Up @@ -306,7 +306,7 @@

// The named-registered format of `System.register('cocos-js/cc.js', [], function() {...})` needs to be generated when the feature of preloading JS list is enabled.
// Otherwise, we will generate the default register code without name like `System.register([], function() {...})`.
if (options.generatePreloadJsList) {
if (options.enableNamedRegisterForSystemJSModuleFormat && options.moduleFormat === 'system') {
rollupPlugins.push(rpNamedChunk());
}

Expand Down Expand Up @@ -439,13 +439,8 @@

const rollupOutput = await rollupBuild.write(rollupOutputOptions);

const outputJSFileNames = [];
const validEntryChunks: Record<string, string> = {};
for (const output of rollupOutput.output) {
if (options.generatePreloadJsList && output.fileName.endsWith('js')) {
outputJSFileNames.push(output.fileName);
}

if (output.type === 'chunk') {
if (output.isEntry) {
const chunkName = output.name;
Expand All @@ -456,10 +451,6 @@
}
}

if (options.generatePreloadJsList) {
fs.outputFileSync(ps.join(options.out, 'engine-js-list.json'), JSON.stringify(outputJSFileNames, undefined, 2));
}

Object.assign(result.exports, validEntryChunks);

result.dependencyGraph = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{ types }: typeof babel,
options: Options,
): babel.PluginObj {
// options.name 为上面代码传递进来的 chunkId
if (!options || !options.name) {
throw new Error('\'name\' options is required.');
}
Expand All @@ -24,8 +23,8 @@
types.isIdentifier(path.node.callee.object) && path.node.callee.object.name === 'System' &&
types.isIdentifier(path.node.callee.property) && path.node.callee.property.name === 'register' &&
path.node.arguments.length === 2) {
// 当发现 System.register([], function (exports, module) {}); 的时候,插入当前 chunk 的名称,变为:
// System.register('my_chunk_name', [], function (exports, module) {});
// Change `System.register([], function (exports, module) {});` to
// `System.register('my_chunk_name', [], function (exports, module) {});`
path.node.arguments.unshift(types.stringLiteral(options.name));
}
},
Expand All @@ -42,13 +41,13 @@
export function rpNamedChunk(): rollup.Plugin {
return {
name: 'named-chunk',
renderChunk: async function(this, code, chunk, options): Promise<RenderChunkResult> {

Check warning on line 44 in modules/build-engine/src/engine-js/rollup-plugins/systemjs-named-register-plugin.ts

View workflow job for this annotation

GitHub Actions / test

'options' is defined but never used

const chunkId = getChunkUrl(chunk);
// 这里输入为 System.register([], function(){...}); 格式的 code
// 输出的 transformResult.code 为 System.register('chunk_id', [], function(){...}); 格式
// Input format: System.register([], function(){...});
// Output format: transformResult.code 为 System.register('chunk_id', [], function(){...});
const transformResult = await babel.transformAsync(code, {
sourceMaps: true, // 这里需要强制为 true 吗?
sourceMaps: true,
compact: false,
plugins: [[toNamedRegister, { name: chunkId }]],
});
Expand Down
7 changes: 5 additions & 2 deletions modules/build-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import fs from 'fs-extra';
import { buildTsEngine } from './engine-ts';

function verifyCache (options: buildEngine.Options): boolean {

Check warning on line 7 in modules/build-engine/src/index.ts

View workflow job for this annotation

GitHub Actions / test

'options' is defined but never used
// TODO
return false;
}
Expand Down Expand Up @@ -204,9 +204,12 @@
// forceJitValue?: boolean,

/**
* Whether to generate a json file that contains all output script file paths.
* Whether to generate 'named' register code for systemjs module format.
* SystemJS default register code: System.register([], function(){...});
* SystemJS named register code: System.register('module_name', [], function(){...});
* @note It's only avaiable when options.moduleFormat is 'system'.
*/
generatePreloadJsList?: boolean;
enableNamedRegisterForSystemJSModuleFormat?: boolean;
}

export interface Result {
Expand Down
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cocos/ccbuild",
"version": "2.2.7",
"version": "2.2.8",
"description": "The next generation of build tool for Cocos engine.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
Loading