-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR does the following: 1. Adds 3 babel plugins to help transform ESM code to CJS: one to transform `path.dirname(fileURLToPath(import.meta)` to `__dirname`, another to turn `isEsm` to false, and lastly a final one to turn proxyquire into esmock. 3. Adds an ESM-path in compileProtos (searches for source code one level deeper, and generates `protos.js` and `protos.cjs` in es6 and amd, respectively)
- Loading branch information
1 parent
6052c9a
commit 0fb1cf9
Showing
15 changed files
with
3,878 additions
and
9 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
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,64 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Visitor, types} from '@babel/core'; | ||
|
||
export interface PluginOptions { | ||
opts?: { | ||
fromLibName?: string; | ||
toLibName?: string; | ||
}; | ||
} | ||
|
||
export default function replaceESMMockingLib(): { | ||
visitor: Visitor<PluginOptions>; | ||
} { | ||
return { | ||
visitor: { | ||
ImportDeclaration(path, state) { | ||
const opts = state.opts || {}; | ||
const fromLib = opts.fromLibName || 'esmock'; | ||
const toLib = opts.toLibName || 'proxyquire'; | ||
const {node} = path; | ||
|
||
node.specifiers.forEach(spec => { | ||
if (spec.local.name !== fromLib) { | ||
return; | ||
} | ||
spec.local.name = spec.local.name.replace(fromLib, toLib); | ||
}); | ||
|
||
if (node.source.value !== fromLib) { | ||
return; | ||
} | ||
node.source.value = node.source.value.replace(fromLib, toLib); | ||
}, | ||
CallExpression(path, state) { | ||
const opts = state.opts || {}; | ||
const fromLib = opts.fromLibName || 'esmock'; | ||
const toLib = opts.toLibName || 'proxyquire'; | ||
const {node} = path; | ||
|
||
if (types.isIdentifier(node.callee)) { | ||
if (node.callee.name !== fromLib) { | ||
return; | ||
} | ||
|
||
node.callee.name = toLib; | ||
path.parentPath.replaceWith(types.expressionStatement(node)); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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,58 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// eslint-disable-next-line node/no-extraneous-import | ||
import {smart} from '@babel/template'; | ||
import {Statement} from '@babel/types'; | ||
import {Visitor} from '@babel/core'; | ||
|
||
export interface PluginOptions { | ||
opts?: { | ||
replacementValue?: string; | ||
}; | ||
} | ||
|
||
export default function replaceImportMetaUrl(): { | ||
visitor: Visitor<PluginOptions>; | ||
} { | ||
return { | ||
visitor: { | ||
CallExpression(path, state) { | ||
const opts = state.opts || {}; | ||
const replacementValue = opts.replacementValue || '__dirname'; | ||
const {node} = path; | ||
if ( | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.property.type === 'Identifier' && | ||
node.callee.property.name === 'dirname' && | ||
node.arguments[0].type === 'CallExpression' && | ||
node.arguments[0].callee.type === 'Identifier' && | ||
node.arguments[0].callee.name === 'fileURLToPath' && | ||
node.arguments[0].arguments[0].type === 'MemberExpression' && | ||
node.arguments[0].arguments[0].object.type === 'MetaProperty' && | ||
node.arguments[0].arguments[0].object.meta.type === 'Identifier' && | ||
node.arguments[0].arguments[0].object.meta.name === 'import' && | ||
node.arguments[0].arguments[0].object.property.type === | ||
'Identifier' && | ||
node.arguments[0].arguments[0].object.property.name === 'meta' && | ||
node.arguments[0].arguments[0].property.type === 'Identifier' && | ||
node.arguments[0].arguments[0].property.name === 'url' | ||
) { | ||
const replacement = smart.ast`${replacementValue}` as Statement; | ||
path.replaceWith(replacement); | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
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,44 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {Visitor, types} from '@babel/core'; | ||
|
||
export interface PluginOptions { | ||
opts?: { | ||
variableIdentifier?: string; | ||
replacementValue?: boolean; | ||
}; | ||
} | ||
|
||
export default function toggleESMFlagVariable(): { | ||
visitor: Visitor<PluginOptions>; | ||
} { | ||
return { | ||
visitor: { | ||
VariableDeclarator(path, state) { | ||
const opts = state.opts || {}; | ||
const variableIdentifier = opts.variableIdentifier || 'isEsm'; | ||
const replacementValue = opts.replacementValue || false; | ||
const {node} = path; | ||
const identifier = node.id as types.Identifier; | ||
if ( | ||
identifier.name === variableIdentifier && | ||
node.init?.type === 'BooleanLiteral' | ||
) { | ||
node.init.value = replacementValue; | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
Oops, something went wrong.