|
| 1 | +const _path = require("path"); |
| 2 | +const fs = require("fs"); |
| 3 | + |
| 4 | +const mappings = require("../configs/mozilla-central-mappings"); |
| 5 | + |
| 6 | +// Add two additional mappings that cannot be reused when creating the |
| 7 | +// webpack bundles. |
| 8 | +mappings["devtools-reps"] = "devtools/client/shared/components/reps/reps.js"; |
| 9 | +mappings["devtools-source-map"] = "devtools/client/shared/source-map/index.js"; |
| 10 | + |
| 11 | +function isRequire(t, node) { |
| 12 | + return node && t.isCallExpression(node) && node.callee.name == "require"; |
| 13 | +} |
| 14 | + |
| 15 | +// List of vendored modules. |
| 16 | +// Should be synchronized with vendors.js |
| 17 | +const VENDORS = [ |
| 18 | + "classnames", |
| 19 | + "devtools-components", |
| 20 | + "devtools-config", |
| 21 | + "devtools-contextmenu", |
| 22 | + "devtools-environment", |
| 23 | + "devtools-modules", |
| 24 | + "devtools-splitter", |
| 25 | + "devtools-utils", |
| 26 | + "fuzzaldrin-plus", |
| 27 | + "react-transition-group/Transition", |
| 28 | + "reselect", |
| 29 | + "Svg", |
| 30 | + "url", |
| 31 | +]; |
| 32 | + |
| 33 | +/** |
| 34 | + * This Babel plugin is used to transpile a single Debugger module into a module that |
| 35 | + * can be loaded in Firefox via the regular DevTools loader. |
| 36 | + */ |
| 37 | +module.exports = function({ types: t }) { |
| 38 | + return { |
| 39 | + visitor: { |
| 40 | + ModuleDeclaration(path, state) { |
| 41 | + const source = path.node.source; |
| 42 | + const value = source && source.value; |
| 43 | + if (value && value.includes(".css")) { |
| 44 | + path.remove(); |
| 45 | + } |
| 46 | + }, |
| 47 | + |
| 48 | + StringLiteral(path, state) { |
| 49 | + const { filePath } = state.opts; |
| 50 | + let value = path.node.value; |
| 51 | + |
| 52 | + if (!isRequire(t, path.parent)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Handle require() to files mapped to other mozilla-central files. |
| 57 | + // e.g. require("devtools-reps") |
| 58 | + // -> require("devtools/client/shared/components/reps/reps.js") |
| 59 | + if (Object.keys(mappings).includes(value)) { |
| 60 | + path.replaceWith(t.stringLiteral(mappings[value])); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Handle require() to loadash submodules |
| 65 | + // e.g. require("lodash/escapeRegExp") |
| 66 | + // -> require("devtools/client/shared/vendor/lodash").escapeRegExp |
| 67 | + if (value.startsWith("lodash/")) { |
| 68 | + const lodashSubModule = value.split("/").pop(); |
| 69 | + path.replaceWith( |
| 70 | + t.stringLiteral(mappings.lodash) |
| 71 | + ); |
| 72 | + path.parentPath.replaceWith( |
| 73 | + t.memberExpression( |
| 74 | + path.parent, |
| 75 | + t.identifier(lodashSubModule) |
| 76 | + ) |
| 77 | + ); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Handle require() to files bundled in vendor.js. |
| 82 | + // e.g. require("some-module"); |
| 83 | + // -> require("devtools/client/debugger/new/dist/vendors").vendored["some-module"]; |
| 84 | + const isVendored = VENDORS.some(vendored => value.endsWith(vendored)); |
| 85 | + if (isVendored) { |
| 86 | + // components/shared/Svg is required using various relative paths. |
| 87 | + // Transform paths such as "../shared/Svg" to "Svg". |
| 88 | + if (value.endsWith("/Svg")) { |
| 89 | + value = "Svg"; |
| 90 | + } |
| 91 | + |
| 92 | + // Transform the required path to require vendors.js |
| 93 | + path.replaceWith( |
| 94 | + t.stringLiteral("devtools/client/debugger/new/dist/vendors") |
| 95 | + ); |
| 96 | + |
| 97 | + // Append `.vendored["some-module"]` after the require(). |
| 98 | + path.parentPath.replaceWith( |
| 99 | + t.memberExpression( |
| 100 | + t.memberExpression( |
| 101 | + path.parent, |
| 102 | + t.identifier("vendored") |
| 103 | + ), |
| 104 | + t.stringLiteral(value), |
| 105 | + true |
| 106 | + ) |
| 107 | + ); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Handle implicit index.js requires: |
| 112 | + // in a node environment, require("my/folder") will automatically load |
| 113 | + // my/folder/index.js if available. The DevTools load does not handle |
| 114 | + // this case, so we need to explicitly transform such requires to point |
| 115 | + // to the index.js file. |
| 116 | + const dir = _path.dirname(filePath); |
| 117 | + const depPath = _path.join(dir, `${value}.js`); |
| 118 | + const exists = fs.existsSync(depPath); |
| 119 | + if ( |
| 120 | + !exists && |
| 121 | + !value.endsWith("index") && |
| 122 | + !value.startsWith("devtools") |
| 123 | + ) { |
| 124 | + path.replaceWith(t.stringLiteral(`${value}/index`)); |
| 125 | + return; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }; |
| 130 | +}; |
0 commit comments