Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🏗 Share dependencies in bento.js #36432

Merged
merged 37 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4bb5008
🏗 Push shared dependencies into `bento.js`
alanorozco Oct 20, 2021
cd3b8eb
test-bento-runtime-amp-script
alanorozco Oct 27, 2021
5f5fa65
nl
alanorozco Nov 11, 2021
3d3ef7d
babel name
alanorozco Nov 11, 2021
d521fa3
name
alanorozco Nov 11, 2021
ba390d5
test
alanorozco Nov 16, 2021
4a1eb71
use esbuild for extension files
alanorozco Nov 16, 2021
cb47e1c
cleanup
alanorozco Nov 16, 2021
2d7766b
try/catch
alanorozco Nov 16, 2021
bda558f
comment
alanorozco Nov 16, 2021
adad82d
clean up
alanorozco Nov 16, 2021
6fce318
no createPortal
alanorozco Nov 16, 2021
9b82e73
typo
alanorozco Nov 16, 2021
03fcfd1
inline writeGeneratedBentoRuntime/esbuild
alanorozco Nov 16, 2021
3323f9b
intermediate packages
alanorozco Nov 16, 2021
a22ab25
test
alanorozco Nov 16, 2021
c8c8dac
update runtime packages
alanorozco Nov 16, 2021
9041023
no toChildArray
alanorozco Nov 17, 2021
868bd0e
flat name list
alanorozco Nov 17, 2021
a9613e0
comment for pkg
alanorozco Nov 18, 2021
1a1e31c
derive symbols
alanorozco Nov 18, 2021
0f53533
rename
alanorozco Nov 18, 2021
00afffb
flat
alanorozco Nov 18, 2021
9668675
comment
alanorozco Nov 18, 2021
0ab63ae
shouldUseClosure
alanorozco Nov 18, 2021
35a9af3
comment
alanorozco Nov 18, 2021
4230fba
move util
alanorozco Nov 19, 2021
3eb46ea
move deps
alanorozco Nov 19, 2021
dc31b1c
no need to share core/context/values
alanorozco Nov 19, 2021
1a880a9
output every time
alanorozco Nov 19, 2021
6a3ca76
alphabetical
alanorozco Nov 19, 2021
18ff569
exports are direct children of program
alanorozco Nov 19, 2021
f5bc441
parallel
alanorozco Nov 19, 2021
d05fece
comment
alanorozco Nov 19, 2021
3e71ec2
name
alanorozco Nov 19, 2021
bff8fe3
rename
alanorozco Nov 19, 2021
e7eeb8c
test
alanorozco Nov 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
intermediate packages
  • Loading branch information
alanorozco committed Dec 3, 2021
commit 3323f9bb11c154bab43d27c10554adae1e13e9c1
39 changes: 35 additions & 4 deletions build-system/babel-config/bento-element-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
const bentoRuntimePackages = require('../compile/generate/metadata/bento-runtime-packages');
const {generateIntermediatePackage} = require('../compile/generate/bento');
const {getMinifiedConfig} = require('./minified-config');
const {getUnminifiedConfig} = require('./unminified-config');
const {outputFileSync, pathExistsSync} = require('fs-extra');

/**
* @param {string} original
* @param {string[]} names
* @return {string}
*/
function writeIntermediatePackage(original, names) {
const basename = original.replace(/[^a-z0-9]/g, '_');
const filePath = `build/bento-package/${basename}.js`;
if (!pathExistsSync(filePath)) {
outputFileSync(filePath, generateIntermediatePackage(original, names));
}
// #build is an alias to the root build/
return `#${filePath}`;
}

/**
* @return {[string, {packages: {[original: string]: {pkg: string, names: string[]}}}]}
*/
function getImportPlugin() {
const packages = Object.fromEntries(
Object.entries(bentoRuntimePackages).map(([original, names]) => {
const pkg = writeIntermediatePackage(original, names);
return [original, {pkg, names}];
})
);
return [
'./build-system/babel-plugins/babel-plugin-bento-imports',
{packages},
];
}

/**
* @param {!Object} config
Expand All @@ -8,10 +42,7 @@ const {getUnminifiedConfig} = require('./unminified-config');
function mergeWithConfig(config) {
return {
...config,
plugins: [
'./build-system/babel-plugins/babel-plugin-bento-imports',
...config.plugins,
],
plugins: [getImportPlugin(), ...config.plugins],
};
}

Expand Down
40 changes: 12 additions & 28 deletions build-system/babel-plugins/babel-plugin-bento-imports/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* @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.
* Replaces `import` statements of listed module specifiers with a different
* module.
* This is similar to `module-resolver`, except that it preserves specifiers to
* the original package if a specific name is not listed.
*/

const bentoRuntimePackages = require('../../compile/generate/metadata/bento-runtime-packages');

module.exports = function (babel) {
const {types: t} = babel;

Expand All @@ -27,32 +26,22 @@ module.exports = function (babel) {

const visitor = {
ImportDeclaration(path, state) {
// Options for tests
const packages = state.opts.packages || bentoRuntimePackages;
const {packages} = state.opts;

const source = path.node.source.value;
const names = packages[source];
if (!names) {
const def = packages[source];
if (!def) {
return;
}

const {names, pkg} = def;
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}}`;
return `import * as ${node.local.name} from '${pkg}';`;
}
// 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}']`;
return `import {${node.imported.name} as ${node.local.name}} from '${pkg}';`;
}
preservedSpecifiers.push(node);
});
Expand All @@ -62,13 +51,8 @@ module.exports = function (babel) {
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)}]`,
});
const template = statements.filter(Boolean).join('\n');
const declaration = babel.template(template)();

// Insert below this sequence of ImportDeclarations
getLastInSequenceOfType(path).insertAfter(declaration);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const a = BENTO["~foo"]['a'];
const b = BENTO["~foo"]['b'];
const x = BENTO["~foo"]['c'];
import { a } from '#build/overridden_foo';
import { b } from '#build/overridden_foo';
import { c as x } from '#build/overridden_foo';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
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']
};
import * as foo from '#build/overridden_foo';
import { fooBar } from '#build/overridden_bar_baz';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
const foo = {
'a': BENTO["~foo"]['a'],
'b': BENTO["~foo"]['b'],
'c': BENTO["~foo"]['c']
};
import * as foo from '#build/overridden_foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"plugins": [
[
"../../..",
{
"packages": {
"~foo": {
"pkg": "#build/overridden_foo",
"names": [
"a",
"b",
"c"
]
},
"~bar/baz": {
"pkg": "#build/overridden_bar_baz",
"names": [
"fooBar"
]
}
}
}
]
]
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { __unlistedShouldBePreserved } from '~foo';
const a = BENTO["~foo"]['a'];
import { a } from '#build/overridden_foo';
4 changes: 0 additions & 4 deletions build-system/compile/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const MAX_PARALLEL_CLOSURE_INVOCATIONS =
* noAddDeps?: boolean,
* continueOnError?: boolean,
* errored?: boolean,
* babelCaller?: string,
* }}
*/
let OptionsDef;
Expand Down Expand Up @@ -138,9 +137,6 @@ function getSrcs(entryModuleFilenames, options) {
.split(path.win32.sep)
.join(path.posix.sep)
);
} else {
// Ensure that entrypoint itself is listed.
srcs.push(filename);
}
});
if (options.extraGlobs) {
Expand Down
12 changes: 12 additions & 0 deletions build-system/compile/generate/bento.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ function generateBentoRuntime(packages = bentoRuntimePackages) {
`);
}

/**
* @param {string} original
* @param {string[]} names
* @return {?string}
*/
function generateIntermediatePackage(original, names) {
return names
.map((name) => `export const ${name} = BENTO['${original}'].${name};`)
.join('\n');
}

module.exports = {
generateBentoRuntime,
generateIntermediatePackage,
};