-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add build option: generatePreloadJsList (#56)
* Add build option: generatePreloadJsList * Update api * Update version to 2.2.4
- Loading branch information
Showing
6 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
modules/build-engine/src/engine-js/rollup-plugins/systemjs-named-register-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { babel as Transformer } from '@ccbuild/transformer'; | ||
import babel = Transformer.core; | ||
|
||
import { rollup as Bundler } from '@ccbuild/bundler'; | ||
import rollup = Bundler.core; | ||
|
||
interface Options { | ||
name: string; | ||
} | ||
|
||
function toNamedRegister( | ||
{ types }: typeof babel, | ||
options: Options, | ||
): babel.PluginObj { | ||
// options.name 为上面代码传递进来的 chunkId | ||
if (!options || !options.name) { | ||
throw new Error('\'name\' options is required.'); | ||
} | ||
|
||
return { | ||
visitor: { | ||
CallExpression: (path): void => { | ||
if (types.isMemberExpression(path.node.callee) && | ||
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) {}); | ||
path.node.arguments.unshift(types.stringLiteral(options.name)); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function getChunkUrl(chunk: rollup.RenderedChunk): string { | ||
return `cocos-js/${chunk.fileName}`; | ||
} | ||
|
||
type RenderChunkResult = { code: string; map?: rollup.SourceMapInput } | string | null | undefined; | ||
|
||
export function rpNamedChunk(): rollup.Plugin { | ||
return { | ||
name: 'named-chunk', | ||
renderChunk: async function(this, code, chunk, options): Promise<RenderChunkResult> { | ||
|
||
const chunkId = getChunkUrl(chunk); | ||
// 这里输入为 System.register([], function(){...}); 格式的 code | ||
// 输出的 transformResult.code 为 System.register('chunk_id', [], function(){...}); 格式 | ||
const transformResult = await babel.transformAsync(code, { | ||
sourceMaps: true, // 这里需要强制为 true 吗? | ||
compact: false, | ||
plugins: [[toNamedRegister, { name: chunkId }]], | ||
}); | ||
if (!transformResult) { | ||
this.warn('Failed to render chunk.'); | ||
return null; | ||
} | ||
return { | ||
code: transformResult.code!, | ||
map: transformResult.map, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters