Skip to content

Commit

Permalink
feat: add support for preserving comments in code
Browse files Browse the repository at this point in the history
Useful for preserving `/*#__PURE__*/` annotations that aid tree shaking.
  • Loading branch information
tido64 committed Apr 19, 2023
1 parent 6900e87 commit 227989b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Object {
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
"unstable_disableNormalizePseudoGlobals": false,
"unstable_preserveComments": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -329,6 +330,7 @@ Object {
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
"unstable_disableNormalizePseudoGlobals": false,
"unstable_preserveComments": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -507,6 +509,7 @@ Object {
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
"unstable_disableNormalizePseudoGlobals": false,
"unstable_preserveComments": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down Expand Up @@ -685,6 +688,7 @@ Object {
"unstable_dependencyMapReservedName": null,
"unstable_disableModuleWrapping": false,
"unstable_disableNormalizePseudoGlobals": false,
"unstable_preserveComments": false,
"workerPath": "metro/src/DeltaBundler/Worker",
},
"transformerPath": "",
Expand Down
1 change: 1 addition & 0 deletions packages/metro-config/src/defaults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
unstable_disableModuleWrapping: false,
unstable_disableNormalizePseudoGlobals: false,
unstable_compactOutput: false,
unstable_preserveComments: false,
},
watcher: {
additionalExts,
Expand Down
21 changes: 21 additions & 0 deletions packages/metro-transform-worker/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const baseConfig: JsTransformerConfig = {
unstable_disableModuleWrapping: false,
unstable_disableNormalizePseudoGlobals: false,
unstable_allowRequireContext: false,
unstable_preserveComments: false,
};

beforeEach(() => {
Expand Down Expand Up @@ -583,3 +584,23 @@ it('counts all line endings correctly', async () => {
standardEndingsResult.output[0].data.lineCount,
);
});

it('allows preservation of comments', async () => {
const result = await Transformer.transform(
{...baseConfig, unstable_preserveComments: true},
'/root',
'local/file.js',
Buffer.from('/*#__PURE__*/arbitrary(code);', 'utf8'),
// $FlowFixMe[prop-missing] Added when annotating Transformer.
{
dev: false,
minify: false,
type: 'module',
},
);
expect(result.output[0].data.code).toMatchInlineSnapshot(`
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
/*#__PURE__*/arbitrary(code);
});"
`);
});
7 changes: 4 additions & 3 deletions packages/metro-transform-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export type JsTransformerConfig = $ReadOnly<{
unstable_compactOutput: boolean,
/** Enable `require.context` statements which can be used to import multiple files in a directory. */
unstable_allowRequireContext: boolean,
unstable_preserveComments: boolean,
}>;

export type {CustomTransformOptions} from 'metro-babel-transformer';
Expand Down Expand Up @@ -299,7 +300,7 @@ async function transformJS(
babelrc: false,
code: false,
configFile: false,
comments: false,
comments: config.unstable_preserveComments,
filename: file.filename,
plugins,
sourceMaps: false,
Expand All @@ -322,7 +323,7 @@ async function transformJS(
babelrc: false,
code: false,
configFile: false,
comments: false,
comments: config.unstable_preserveComments,
filename: file.filename,
plugins: [
[metroTransformPlugins.constantFoldingPlugin, babelPluginOpts],
Expand Down Expand Up @@ -407,7 +408,7 @@ async function transformJS(
const result = generate(
wrappedAst,
{
comments: false,
comments: config.unstable_preserveComments,
compact: config.unstable_compactOutput,
filename: file.filename,
retainLines: false,
Expand Down

0 comments on commit 227989b

Please sign in to comment.