Skip to content

Commit 30c2d1c

Browse files
committed
add manual watching plugin
1 parent 5201940 commit 30c2d1c

15 files changed

+73
-5
lines changed

packages/browser/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// packages with bundles have a different build directory structure
66
hasBundles: true,
7+
watchPackages: ['core'],
78
}),
89
);

packages/core/rollup.npm.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
watchPackages: ['hub'],
6+
}),
7+
);

packages/gatsby/rollup.npm.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
watchPackages: ['react', 'tracing'],
6+
}),
7+
);

packages/hub/rollup.npm.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
watchPackages: ['utils'],
6+
}),
7+
);

packages/integrations/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// packages with bundles have a different build directory structure
66
hasBundles: true,
7+
watchPackages: ['utils'],
78
}),
89
);

packages/nextjs/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export default makeNPMConfigVariants(
88
// prevent this nextjs code from ending up in our built package (this doesn't happen automatially because the name
99
// doesn't match an SDK dependency)
1010
externals: ['next/router'],
11+
watchPackages: ['integrations', 'node', 'react', 'tracing'],
1112
}),
1213
);

packages/node/rollup.npm.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
watchPackages: ['core'],
6+
}),
7+
);

packages/react/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'
33
export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
esModuleInterop: true,
6+
watchPackages: ['browser'],
67
}),
78
);

packages/serverless/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'
33
export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
entrypoints: ['src/index.ts', 'src/awslambda-auto.ts'],
6+
watchPackages: ['node', 'tracing'],
67
}),
78
);

packages/tracing/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// packages with bundles have a different build directory structure
66
hasBundles: true,
7+
watchPackages: ['browser'],
78
}),
89
);

packages/utils/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default makeNPMConfigVariants(
55
// We build the polyfills separately because they're not included in the top-level exports of the package, in order
66
// to keep them out of the public API.
77
entrypoints: ['src/index.ts', 'src/buildPolyfills/index.ts'],
8+
watchPackages: ['types'],
89
}),
910
);

packages/vue/rollup.npm.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js';
22

3-
export default makeNPMConfigVariants(makeBaseNPMConfig());
3+
export default makeNPMConfigVariants(
4+
makeBaseNPMConfig({
5+
watchPackages: ['browser'],
6+
}),
7+
);

packages/wasm/rollup.npm.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// packages with bundles have a different build directory structure
66
hasBundles: true,
7+
watchPackages: ['browser'],
78
}),
89
);

rollup/npmHelpers.js

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
makeRemoveBlankLinesPlugin,
1515
makeRemoveESLintCommentsPlugin,
1616
makeSucrasePlugin,
17+
makeWatchDependenciesPlugin,
1718
} from './plugins/index.js';
1819

1920
const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));
@@ -24,8 +25,10 @@ export function makeBaseNPMConfig(options = {}) {
2425
esModuleInterop = false,
2526
externals: packageSpecificExternals = [],
2627
hasBundles = false,
28+
watchPackages = [],
2729
} = options;
2830

31+
const watchDependenciesPlugin = makeWatchDependenciesPlugin(watchPackages);
2932
const nodeResolvePlugin = makeNodeResolvePlugin();
3033
const sucrasePlugin = makeSucrasePlugin();
3134
const constToVarPlugin = makeConstToVarPlugin();
@@ -77,6 +80,7 @@ export function makeBaseNPMConfig(options = {}) {
7780
},
7881

7982
plugins: [
83+
watchDependenciesPlugin,
8084
nodeResolvePlugin,
8185
sucrasePlugin,
8286
constToVarPlugin,

rollup/plugins/npmPlugins.js

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Sucrase plugin docs: https://github.com/rollup/plugins/tree/master/packages/sucrase
55
*/
66

7+
import * as path from 'path';
8+
79
// We need both replacement plugins because one handles regex and the other runs both before and after rollup does its
810
// bundling work.
911
import regexReplace from 'rollup-plugin-re';
@@ -98,4 +100,38 @@ export function makeRemoveBlankLinesPlugin() {
98100
});
99101
}
100102

103+
/**
104+
* Create a plugin to manually add packages to a build's watch.
105+
*
106+
* Unlike tsc, rollup doesn't seem to be able to follow the symlinks that yarn workspaces uses to link our packages into
107+
* one another, with the result that changes don't automatically cascade in watch mode. This plugin sets that up
108+
* manually.
109+
*
110+
* TODO: This currently only works when running `build:watch` or `build:dev:watch` at the repo level, not the individual
111+
* package level.
112+
*
113+
* TODO: At the moment, in the interests (current) time and (future) build spped, the watch list for each package is
114+
* hardcoded into its rollup config. While the values in those lists are likely to be highly stable over time, there's
115+
* also no mechanism to guarantee that changes in a package's intra-package dependencies will be reflected in the lists.
116+
* It shouldn't be that hard to write a script to generate the correct lists at build time, though, and hopefully it
117+
* wouldn't add a ton of time to the build. (It'd be a lot of file IO reading all of the `package.json` files, but
118+
* computers are also fast. We'd have to experiement.)
119+
*
120+
* @param watchPackages The packages to watch. Shouldn't include any transitive dependencies, even if they're also
121+
* direct dependencies. (In other words, if A depends on B and C, and B depends on C, then A should only list B, not C.)
122+
*
123+
* @returns A plugin which will add the given packages to a build's watch list if the build is running in watch mode.
124+
*/
125+
export function makeWatchDependenciesPlugin(watchPackages) {
126+
return {
127+
name: 'watch-dependencies',
128+
buildStart: function () {
129+
if (this.meta.watchMode) {
130+
const cwd = process.cwd();
131+
watchPackages.forEach(pkg => this.addWatchFile(path.resolve(cwd, `../${pkg}`)));
132+
}
133+
},
134+
};
135+
}
136+
101137
export { makeExtractPolyfillsPlugin } from './extractPolyfillsPlugin.js';

0 commit comments

Comments
 (0)