-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🏗 Push shared dependencies into
bento.js
- Loading branch information
1 parent
30413cf
commit 84708c9
Showing
36 changed files
with
510 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const {getPreClosureConfig} = require('./pre-closure-config'); | ||
|
||
/** | ||
* @return {!Object} | ||
*/ | ||
function getBentoPreClosureConfig() { | ||
const config = getPreClosureConfig(); | ||
return { | ||
...config, | ||
plugins: [ | ||
'./build-system/babel-plugins/babel-plugin-bento-imports', | ||
...config.plugins, | ||
], | ||
}; | ||
} | ||
|
||
module.exports = { | ||
getBentoPreClosureConfig, | ||
}; |
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,19 @@ | ||
const {getUnminifiedConfig} = require('./unminified-config'); | ||
|
||
/** | ||
* @return {!Object} | ||
*/ | ||
function getBentoUnminifiedConfig() { | ||
const config = getUnminifiedConfig(); | ||
return { | ||
...config, | ||
plugins: [ | ||
'./build-system/babel-plugins/babel-plugin-bento-imports', | ||
...config.plugins, | ||
], | ||
}; | ||
} | ||
|
||
module.exports = { | ||
getBentoUnminifiedConfig, | ||
}; |
95 changes: 95 additions & 0 deletions
95
build-system/babel-plugins/babel-plugin-bento-imports/index.js
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,95 @@ | ||
/** | ||
* @fileoverview | ||
* Replaces `import` statements of listed modules with `const` statements. | ||
* These reference the global `BENTO` which is meant to provide the contents of | ||
* these listed modules. | ||
*/ | ||
|
||
const bentoRuntimePackages = require('../../compile/generate/metadata/bento-runtime-packages'); | ||
|
||
module.exports = function (babel) { | ||
const {types: t} = babel; | ||
|
||
/** | ||
* @param {babel.NodePath} path | ||
* @return {babel.NodePath} | ||
*/ | ||
function getLastInSequenceOfType(path) { | ||
let last = path; | ||
while ( | ||
typeof last.key === 'number' && | ||
path.getSibling(last.key + 1).node?.type === last.node.type | ||
) { | ||
last = path.getSibling(last.key + 1); | ||
} | ||
return last; | ||
} | ||
|
||
const visitor = { | ||
ImportDeclaration(path, state) { | ||
// Options for tests | ||
const packages = state.opts.packages || bentoRuntimePackages; | ||
|
||
const source = path.node.source.value; | ||
const names = packages[source]; | ||
if (!names) { | ||
return; | ||
} | ||
|
||
const preservedSpecifiers = []; | ||
const statements = path.node.specifiers.map((node) => { | ||
// Full object reference for namespace imports | ||
// - import * as p from 'package'; | ||
// + const p = {'x': BENTO['package']['x'], ...} | ||
if (t.isImportNamespaceSpecifier(node)) { | ||
const properties = names | ||
.map((name) => `'${name}': PKG['${name}']`) | ||
.join(','); | ||
return `const ${node.local.name} = {${properties}}`; | ||
} | ||
// One statement per named import | ||
// - import {z, y} from 'package'; | ||
// + const z = BENTO['package']['z']; | ||
// + const y = BENTO['package']['y']; | ||
if (t.isImportSpecifier(node) && names.includes(node.imported.name)) { | ||
return `const ${node.local.name} = PKG['${node.imported.name}']`; | ||
} | ||
preservedSpecifiers.push(node); | ||
}); | ||
|
||
if (preservedSpecifiers.length === path.node.specifiers.length) { | ||
// all preserved, nothing to do | ||
return; | ||
} | ||
|
||
const template = statements.filter(Boolean).join(';\n'); | ||
|
||
const declaration = babel.template(template)({ | ||
// We need to specify a placeholder key because babel's `template` will | ||
// fail with unspecified UPPERCASE identifiers. | ||
'PKG': `BENTO[${JSON.stringify(source)}]`, | ||
}); | ||
|
||
// Insert below this sequence of ImportDeclarations | ||
getLastInSequenceOfType(path).insertAfter(declaration); | ||
|
||
if (preservedSpecifiers.length === 0) { | ||
path.remove(); | ||
} else { | ||
path.node.specifiers = preservedSpecifiers; | ||
} | ||
}, | ||
}; | ||
|
||
return { | ||
name: 'bento-imports', | ||
visitor: { | ||
// We visit on Program.enter to take priority over module-resolver | ||
Program: { | ||
enter(programPath, state) { | ||
programPath.traverse(visitor, state); | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |
1 change: 1 addition & 0 deletions
1
...l-plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/ignored/input.mjs
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 @@ | ||
import {ignored} from './unlisted'; |
19 changes: 19 additions & 0 deletions
19
...lugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/ignored/options.json
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,19 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../..", | ||
{ | ||
"packages": { | ||
"~foo": [ | ||
"a", | ||
"b", | ||
"c" | ||
], | ||
"~bar/baz": [ | ||
"fooBar" | ||
] | ||
} | ||
} | ||
] | ||
] | ||
} |
1 change: 1 addition & 0 deletions
1
...-plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/ignored/output.mjs
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 @@ | ||
import { ignored } from './unlisted'; |
1 change: 1 addition & 0 deletions
1
...lugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/local-name/input.mjs
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 @@ | ||
import {a, b, c as x} from '~foo'; |
19 changes: 19 additions & 0 deletions
19
...ins/babel-plugin-bento-imports/test/fixtures/transform-assertions/local-name/options.json
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,19 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../..", | ||
{ | ||
"packages": { | ||
"~foo": [ | ||
"a", | ||
"b", | ||
"c" | ||
], | ||
"~bar/baz": [ | ||
"fooBar" | ||
] | ||
} | ||
} | ||
] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...ugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/local-name/output.mjs
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,3 @@ | ||
const a = BENTO["~foo"]['a']; | ||
const b = BENTO["~foo"]['b']; | ||
const x = BENTO["~foo"]['c']; |
3 changes: 3 additions & 0 deletions
3
...bel-plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/mixed/input.mjs
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,3 @@ | ||
import * as foo from '~foo'; | ||
import {fooBar, __unlistedShouldBePreserved} from '~bar/baz'; | ||
import {ignored} from './unlisted'; |
19 changes: 19 additions & 0 deletions
19
...-plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/mixed/options.json
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,19 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../..", | ||
{ | ||
"packages": { | ||
"~foo": [ | ||
"a", | ||
"b", | ||
"c" | ||
], | ||
"~bar/baz": [ | ||
"fooBar" | ||
] | ||
} | ||
} | ||
] | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...el-plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/mixed/output.mjs
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,8 @@ | ||
import { __unlistedShouldBePreserved } from '~bar/baz'; | ||
import { ignored } from './unlisted'; | ||
const fooBar = BENTO["~bar/baz"]['fooBar']; | ||
const foo = { | ||
'a': BENTO["~foo"]['a'], | ||
'b': BENTO["~foo"]['b'], | ||
'c': BENTO["~foo"]['c'] | ||
}; |
1 change: 1 addition & 0 deletions
1
...plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/namespace/input.mjs
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 @@ | ||
import * as foo from '~foo'; |
19 changes: 19 additions & 0 deletions
19
...gins/babel-plugin-bento-imports/test/fixtures/transform-assertions/namespace/options.json
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,19 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../..", | ||
{ | ||
"packages": { | ||
"~foo": [ | ||
"a", | ||
"b", | ||
"c" | ||
], | ||
"~bar/baz": [ | ||
"fooBar" | ||
] | ||
} | ||
} | ||
] | ||
] | ||
} |
5 changes: 5 additions & 0 deletions
5
...lugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/namespace/output.mjs
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,5 @@ | ||
const foo = { | ||
'a': BENTO["~foo"]['a'], | ||
'b': BENTO["~foo"]['b'], | ||
'c': BENTO["~foo"]['c'] | ||
}; |
1 change: 1 addition & 0 deletions
1
...plugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/preserved/input.mjs
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 @@ | ||
import {a, __unlistedShouldBePreserved} from '~foo'; |
19 changes: 19 additions & 0 deletions
19
...gins/babel-plugin-bento-imports/test/fixtures/transform-assertions/preserved/options.json
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,19 @@ | ||
{ | ||
"plugins": [ | ||
[ | ||
"../../../..", | ||
{ | ||
"packages": { | ||
"~foo": [ | ||
"a", | ||
"b", | ||
"c" | ||
], | ||
"~bar/baz": [ | ||
"fooBar" | ||
] | ||
} | ||
} | ||
] | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
...lugins/babel-plugin-bento-imports/test/fixtures/transform-assertions/preserved/output.mjs
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,2 @@ | ||
import { __unlistedShouldBePreserved } from '~foo'; | ||
const a = BENTO["~foo"]['a']; |
3 changes: 3 additions & 0 deletions
3
build-system/babel-plugins/babel-plugin-bento-imports/test/index.js
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,3 @@ | ||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
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
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,14 @@ | ||
// For an explanation of the OWNERS rules and syntax, see: | ||
// https://github.com/ampproject/amp-github-apps/blob/main/owners/OWNERS.example | ||
|
||
{ | ||
rules: [ | ||
{ | ||
owners: [ | ||
{name: 'ampproject/wg-infra'}, | ||
{name: 'ampproject/wg-bento'}, | ||
{name: 'alanorozco', notify: true}, | ||
], | ||
}, | ||
], | ||
} |
Oops, something went wrong.