Files
react/scripts/rollup/plugins/strip-unused-imports.js
Andrew ClarkandGitHub 9cdf8a99ed [Codemod] Update copyright header to Meta (#25315)
* Facebook -> Meta in copyright

rg --files | xargs sed -i 's#Copyright (c) Facebook, Inc. and its affiliates.#Copyright (c) Meta Platforms, Inc. and affiliates.#g'

* Manual tweaks
2022-10-18 11:19:24 -04:00

30 lines
920 B
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = function stripUnusedImports(pureExternalModules) {
return {
name: 'scripts/rollup/plugins/strip-unused-imports',
renderChunk(code) {
pureExternalModules.forEach(module => {
// Ideally this would use a negative lookbehind: (?<!= *)
// But this isn't supported by the Node <= 8.9.
// So instead we try to handle the most common cases:
// 1. foo,bar=require("bar"),baz
// 2. foo;bar = require('bar');baz
// 3. require('bar');
const regExp = new RegExp(
`([,;]| {2})require\\(["']${module}["']\\)[,;]`,
'g'
);
code = code.replace(regExp, '$1');
});
return {code};
},
};
};