Skip to content

Commit

Permalink
Rename option.generatePreloadJsList to enableNamedRegisterForSystemJS…
Browse files Browse the repository at this point in the history
…ModuleFormat. (#61)

* Rename option.generatePreloadJsList to enableNamedRegisterForSystemJSModuleFormat.

* Update version to 2.2.8
  • Loading branch information
dumganhar authored Mar 4, 2024
1 parent f35fefc commit 3cdfaa6
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
7 changes: 5 additions & 2 deletions .api/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ declare module "@cocos/ccbuild" {
*/
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 @@ -306,7 +306,7 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro

// 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 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro

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 @@ export async function buildJsEngine(options: Required<buildEngine.Options>): Pro
}
}

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 @@ function toNamedRegister(
{ 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 @@ function toNamedRegister(
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 @@ -45,10 +44,10 @@ export function rpNamedChunk(): rollup.Plugin {
renderChunk: async function(this, code, chunk, options): Promise<RenderChunkResult> {

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 @@ -204,9 +204,12 @@ export namespace buildEngine {
// 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

0 comments on commit 3cdfaa6

Please sign in to comment.