Skip to content

Commit 327e5aa

Browse files
committed
add prefix loader
1 parent 7319096 commit 327e5aa

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

packages/nextjs/rollup.npm.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,19 @@ export default [
2929
},
3030
}),
3131
),
32+
...makeNPMConfigVariants(
33+
makeBaseNPMConfig({
34+
entrypoints: ['src/config/prefixLoader.ts'],
35+
36+
packageSpecificConfig: {
37+
output: {
38+
// make it so Rollup calms down about the fact that we're doing `export { loader as default }`
39+
exports: 'default',
40+
41+
// preserve the original file structure (i.e., so that everything is still relative to `src`)
42+
entryFileNames: 'config/[name].js',
43+
},
44+
},
45+
}),
46+
),
3247
];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
// TODO Use real webpack types (different for webpack 4 and 5?)
5+
type LoaderThis = {
6+
getOptions: () => {
7+
distDir: string;
8+
};
9+
};
10+
11+
/**
12+
* Inject templated code into the beginning of a module.
13+
*/
14+
function prefixLoader(this: LoaderThis, userCode: string): string {
15+
const { distDir } = this.getOptions();
16+
17+
const templatePath = path.resolve(__dirname, 'prefixLoaderTemplate.js');
18+
let templateCode = fs.readFileSync(templatePath).toString();
19+
20+
// Fill in the placeholder
21+
templateCode = templateCode.replace('__DIST_DIR__', distDir);
22+
23+
return `${templateCode}\n${userCode}`;
24+
}
25+
26+
export { prefixLoader as default };

0 commit comments

Comments
 (0)