@travetto/pack 4.0.0-rc.4 → 4.0.0-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/__index__.ts +2 -2
- package/package.json +5 -4
- package/{support/bin → src}/types.ts +13 -1
- package/support/bin/docker-operation.ts +1 -1
- package/support/bin/operation.ts +2 -2
- package/support/bin/preamble.ts +18 -0
- package/support/bin/shell.ts +1 -1
- package/support/cli.pack_docker.ts +1 -1
- package/support/pack.base.ts +1 -1
- package/support/pack.dockerfile.ts +2 -2
- package/support/rollup/build.ts +29 -0
- package/support/{bin → rollup}/config.ts +24 -38
- package/support/rollup/rollup-travetto-entry.ts +43 -0
- package/support/rollup/rollup-travetto-import.ts +60 -0
- package/support/{bin/rollup-sourcemaps.ts → rollup/rollup-travetto-sourcemaps.ts} +6 -2
- package/support/bin/rollup-esm-dynamic-import.ts +0 -55
- package/support/bin/rollup.ts +0 -38
- /package/{support/bin → src}/config-util.ts +0 -0
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ Options:
|
|
|
39
39
|
-sm, --sourcemap Bundle source maps (default: false)
|
|
40
40
|
-is, --include-sources Include source with source maps (default: false)
|
|
41
41
|
-x, --eject-file <string> Eject commands to file
|
|
42
|
-
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/
|
|
42
|
+
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/rollup/build")
|
|
43
43
|
--env-file <string> Env Flag File Name (default: ".env")
|
|
44
44
|
--manifest-file <string> Manifest File Name (default: "manifest.json")
|
|
45
45
|
-m, --module <module> Module to run for
|
|
@@ -103,7 +103,7 @@ Options:
|
|
|
103
103
|
-sm, --sourcemap Bundle source maps (default: false)
|
|
104
104
|
-is, --include-sources Include source with source maps (default: false)
|
|
105
105
|
-x, --eject-file <string> Eject commands to file
|
|
106
|
-
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/
|
|
106
|
+
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/rollup/build")
|
|
107
107
|
--env-file <string> Env Flag File Name (default: ".env")
|
|
108
108
|
--manifest-file <string> Manifest File Name (default: "manifest.json")
|
|
109
109
|
-m, --module <module> Module to run for
|
|
@@ -130,7 +130,7 @@ Options:
|
|
|
130
130
|
-sm, --sourcemap Bundle source maps (default: false)
|
|
131
131
|
-is, --include-sources Include source with source maps (default: false)
|
|
132
132
|
-x, --eject-file <string> Eject commands to file
|
|
133
|
-
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/
|
|
133
|
+
-r, --rollup-configuration <string> Rollup configuration file (default: "@travetto/pack/support/rollup/build")
|
|
134
134
|
--env-file <string> Env Flag File Name (default: ".env")
|
|
135
135
|
--manifest-file <string> Manifest File Name (default: "manifest.json")
|
|
136
136
|
-df, --docker-factory <string> Docker Factory source (default: "@travetto/pack/support/pack.dockerfile")
|
|
@@ -195,7 +195,7 @@ echo "Writing entry scripts todo-app.sh args=(run:rest)"
|
|
|
195
195
|
|
|
196
196
|
echo "#!/bin/sh" > $DIST/todo-app.sh
|
|
197
197
|
echo "cd \$(dirname \"\$0\")" >> $DIST/todo-app.sh
|
|
198
|
-
echo "node
|
|
198
|
+
echo "node todo-app.js run:rest \$@" >> $DIST/todo-app.sh
|
|
199
199
|
chmod 755 $DIST/todo-app.sh
|
|
200
200
|
|
|
201
201
|
# Writing entry scripts todo-app.cmd args=(run:rest)
|
|
@@ -204,7 +204,7 @@ echo "Writing entry scripts todo-app.cmd args=(run:rest)"
|
|
|
204
204
|
|
|
205
205
|
echo "" > $DIST/todo-app.cmd
|
|
206
206
|
echo "cd %~p0" >> $DIST/todo-app.cmd
|
|
207
|
-
echo "node
|
|
207
|
+
echo "node todo-app.js run:rest %*" >> $DIST/todo-app.cmd
|
|
208
208
|
chmod 755 $DIST/todo-app.cmd
|
|
209
209
|
|
|
210
210
|
# Copying over resources
|
|
@@ -233,7 +233,7 @@ export BUNDLE_FORMAT=commonjs
|
|
|
233
233
|
export BUNDLE_ENV_FILE=.env
|
|
234
234
|
export TRV_MANIFEST=$TRV_OUT/node_modules/$MOD
|
|
235
235
|
cd $TRV_OUT
|
|
236
|
-
npx rollup -c $TRV_OUT/node_modules/@travetto/pack/support/
|
|
236
|
+
npx rollup -c $TRV_OUT/node_modules/@travetto/pack/support/rollup/build.js
|
|
237
237
|
cd $ROOT
|
|
238
238
|
|
|
239
239
|
# Generating Docker File $DIST/Dockerfile @travetto/pack/support/pack.dockerfile
|
package/__index__.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { CommonPackConfig, DockerPackFactory, DockerPackConfig } from './
|
|
2
|
-
export { PackConfigUtil } from './
|
|
1
|
+
export type { CommonPackConfig, DockerPackFactory, DockerPackConfig } from './src/types';
|
|
2
|
+
export { PackConfigUtil } from './src/config-util';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/pack",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.5",
|
|
4
4
|
"description": "Code packing utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"travetto",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"main": "__index__.ts",
|
|
18
18
|
"files": [
|
|
19
19
|
"__index__.ts",
|
|
20
|
+
"src",
|
|
20
21
|
"support"
|
|
21
22
|
],
|
|
22
23
|
"repository": {
|
|
@@ -28,12 +29,12 @@
|
|
|
28
29
|
"@rollup/plugin-json": "^6.1.0",
|
|
29
30
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
30
31
|
"@rollup/plugin-terser": "^0.4.4",
|
|
31
|
-
"@travetto/base": "^4.0.0-rc.
|
|
32
|
-
"@travetto/terminal": "^4.0.0-rc.
|
|
32
|
+
"@travetto/base": "^4.0.0-rc.4",
|
|
33
|
+
"@travetto/terminal": "^4.0.0-rc.4",
|
|
33
34
|
"rollup": "^4.9.6"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
|
-
"@travetto/cli": "^4.0.0-rc.
|
|
37
|
+
"@travetto/cli": "^4.0.0-rc.4"
|
|
37
38
|
},
|
|
38
39
|
"peerDependenciesMeta": {
|
|
39
40
|
"@travetto/cli": {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { type OutputOptions } from 'rollup';
|
|
2
|
+
import type terser from '@rollup/plugin-terser';
|
|
3
|
+
|
|
1
4
|
export type CommonPackConfig = {
|
|
2
5
|
workspace: string;
|
|
3
6
|
output: string;
|
|
@@ -57,4 +60,13 @@ export type ShellCommandImpl = {
|
|
|
57
60
|
};
|
|
58
61
|
|
|
59
62
|
export type DockerPackFactory = (cfg: DockerPackConfig) => (string | Promise<string>);
|
|
60
|
-
export type DockerPackFactoryModule = { factory: DockerPackFactory };
|
|
63
|
+
export type DockerPackFactoryModule = { factory: DockerPackFactory };
|
|
64
|
+
|
|
65
|
+
export type CoreRollupConfig = {
|
|
66
|
+
envFile?: string;
|
|
67
|
+
output: OutputOptions;
|
|
68
|
+
entry: string;
|
|
69
|
+
files: string[];
|
|
70
|
+
ignore: Set<string>;
|
|
71
|
+
minify: Parameters<typeof terser>[0];
|
|
72
|
+
};
|
|
@@ -4,7 +4,7 @@ import { path, RuntimeIndex } from '@travetto/manifest';
|
|
|
4
4
|
import { cliTpl } from '@travetto/cli';
|
|
5
5
|
|
|
6
6
|
import { ActiveShellCommand } from './shell';
|
|
7
|
-
import { DockerPackConfig, DockerPackFactoryModule } from '
|
|
7
|
+
import { DockerPackConfig, DockerPackFactoryModule } from '../../src/types';
|
|
8
8
|
import { PackOperation } from './operation';
|
|
9
9
|
import { PackUtil } from './util';
|
|
10
10
|
|
package/support/bin/operation.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { path, RuntimeIndex, RuntimeContext } from '@travetto/manifest';
|
|
|
4
4
|
import { cliTpl } from '@travetto/cli';
|
|
5
5
|
import { Env } from '@travetto/base';
|
|
6
6
|
|
|
7
|
-
import { CommonPackConfig } from '
|
|
7
|
+
import { CommonPackConfig } from '../../src/types';
|
|
8
8
|
import { PackUtil } from './util';
|
|
9
9
|
import { ActiveShellCommand, ShellCommands } from './shell';
|
|
10
10
|
|
|
@@ -160,7 +160,7 @@ export class PackOperation {
|
|
|
160
160
|
text: [
|
|
161
161
|
ShellCommands[type].scriptOpen(),
|
|
162
162
|
ShellCommands[type].chdirScript(),
|
|
163
|
-
ShellCommands[type].callCommandWithAllArgs('node',
|
|
163
|
+
ShellCommands[type].callCommandWithAllArgs('node', cfg.mainFile, ...cfg.entryArguments),
|
|
164
164
|
].map(x => x.join(' '))
|
|
165
165
|
}));
|
|
166
166
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readFileSync as readSyncPreamble } from 'node:fs';
|
|
2
|
+
|
|
3
|
+
// @ts-expect-error -- Lock to prevent __proto__ pollution in JSON
|
|
4
|
+
const objectProto = Object.prototype.__proto__;
|
|
5
|
+
Object.defineProperty(Object.prototype, '__proto__', {
|
|
6
|
+
get() { return objectProto; },
|
|
7
|
+
set(val) { Object.setPrototypeOf(this, val); }
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
if (!process.env.TRV_MODULE && '%%ENV_FILE%%') {
|
|
11
|
+
try {
|
|
12
|
+
readSyncPreamble('%%ENV_FILE%%', 'utf8')
|
|
13
|
+
.split('\n')
|
|
14
|
+
.map(x => x.match(/\s*(?<key>[^ =]+)\s*=\s*(?<value>\S+)/)?.groups)
|
|
15
|
+
.filter((x): x is Exclude<typeof x, null | undefined> => !!x)
|
|
16
|
+
.forEach(x => process.env[x.key] = x.value);
|
|
17
|
+
} catch { }
|
|
18
|
+
}
|
package/support/bin/shell.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Ignore, Required } from '@travetto/schema';
|
|
|
4
4
|
|
|
5
5
|
import { DockerPackOperation } from './bin/docker-operation';
|
|
6
6
|
import { BasePackCommand, PackOperationShape } from './pack.base';
|
|
7
|
-
import { DockerPackConfig } from '
|
|
7
|
+
import { DockerPackConfig } from '../src/types';
|
|
8
8
|
|
|
9
9
|
const NODE_MAJOR = +process.version.match(/\d+/)![0];
|
|
10
10
|
const DEFAULT_USER_ID = 2000;
|
package/support/pack.base.ts
CHANGED
|
@@ -60,7 +60,7 @@ export abstract class BasePackCommand implements CliCommandShape {
|
|
|
60
60
|
ejectFile?: string;
|
|
61
61
|
|
|
62
62
|
@CliFlag({ desc: 'Rollup configuration file', short: 'r' })
|
|
63
|
-
rollupConfiguration = '@travetto/pack/support/
|
|
63
|
+
rollupConfiguration = '@travetto/pack/support/rollup/build';
|
|
64
64
|
|
|
65
65
|
@CliFlag({ desc: 'Env Flag File Name' })
|
|
66
66
|
envFile = '.env';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DockerPackFactory } from '
|
|
2
|
-
import { PackConfigUtil } from '
|
|
1
|
+
import { DockerPackFactory } from '../src/types';
|
|
2
|
+
import { PackConfigUtil } from '../src/config-util';
|
|
3
3
|
|
|
4
4
|
export const factory: DockerPackFactory = cfg => PackConfigUtil.dockerStandardFile(cfg);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import commonjsRequire from '@rollup/plugin-commonjs';
|
|
2
|
+
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
3
|
+
import terser from '@rollup/plugin-terser';
|
|
4
|
+
import jsonImport from '@rollup/plugin-json';
|
|
5
|
+
import type { RollupOptions } from 'rollup';
|
|
6
|
+
|
|
7
|
+
import { getCoreConfig } from './config';
|
|
8
|
+
import { travettoImportPlugin } from './rollup-travetto-import';
|
|
9
|
+
import { travettoSourcemaps } from './rollup-travetto-sourcemaps';
|
|
10
|
+
import { travettoEntryPlugin } from './rollup-travetto-entry';
|
|
11
|
+
|
|
12
|
+
export default function buildConfig(): RollupOptions {
|
|
13
|
+
// Load up if not defined
|
|
14
|
+
const config = getCoreConfig();
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
input: [config.entry],
|
|
18
|
+
output: config.output,
|
|
19
|
+
plugins: [
|
|
20
|
+
jsonImport(),
|
|
21
|
+
travettoEntryPlugin(config),
|
|
22
|
+
travettoImportPlugin(config),
|
|
23
|
+
commonjsRequire({ ignore: id => config.ignore.has(id) }),
|
|
24
|
+
nodeResolve({ preferBuiltins: true }),
|
|
25
|
+
travettoSourcemaps(config),
|
|
26
|
+
...(config.output.compact ? [terser(config.minify)] : [])
|
|
27
|
+
]
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -1,38 +1,10 @@
|
|
|
1
|
-
import type fs from 'node:fs';
|
|
2
1
|
import type { OutputOptions } from 'rollup';
|
|
3
2
|
|
|
4
3
|
import type terser from '@rollup/plugin-terser';
|
|
5
4
|
|
|
6
5
|
import { ManifestModule, ManifestModuleUtil, NodeModuleType, path, RuntimeIndex, RuntimeContext } from '@travetto/manifest';
|
|
7
6
|
import { EnvProp } from '@travetto/base';
|
|
8
|
-
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
-
function __init(mod: typeof fs, file?: string, freezeProto?: boolean): void {
|
|
11
|
-
if (freezeProto !== false) {
|
|
12
|
-
// @ts-expect-error -- Lock to prevent __proto__ pollution in JSON
|
|
13
|
-
const objectProto = Object.prototype.__proto__;
|
|
14
|
-
Object.defineProperty(Object.prototype, '__proto__', {
|
|
15
|
-
get() { return objectProto; },
|
|
16
|
-
set(val) { Object.setPrototypeOf(this, val); }
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (file) {
|
|
21
|
-
if (process.env.TRV_MODULE) { return; }
|
|
22
|
-
try {
|
|
23
|
-
mod.readFileSync(file, 'utf8')
|
|
24
|
-
.split('\n')
|
|
25
|
-
.map(x => x.match(/\s*(?<key>[^ =]+)\s*=\s*(?<value>\S+)/)?.groups)
|
|
26
|
-
.filter((x): x is Exclude<typeof x, null | undefined> => !!x)
|
|
27
|
-
.forEach(x => process.env[x.key] = x.value);
|
|
28
|
-
} catch { }
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const INTRO = (envFile: string | undefined, sourceMap?: boolean): Record<NodeModuleType, string> => ({
|
|
33
|
-
commonjs: `(${__init.toString()})(require('node:fs'), '${envFile}', ${!!sourceMap})`,
|
|
34
|
-
module: `(${__init.toString()})(await import('node:fs'), '${envFile}', ${!!sourceMap})`
|
|
35
|
-
});
|
|
7
|
+
import { CoreRollupConfig } from '../../src/types';
|
|
36
8
|
|
|
37
9
|
function getFilesFromModule(m: ManifestModule): string[] {
|
|
38
10
|
return [
|
|
@@ -41,7 +13,7 @@ function getFilesFromModule(m: ManifestModule): string[] {
|
|
|
41
13
|
...(m.files.bin ?? [])
|
|
42
14
|
.filter(f => !(/bin\/trv[.]js$/.test(f[0]) && m.name === '@travetto/cli')),
|
|
43
15
|
...(m.files.support ?? [])
|
|
44
|
-
.filter(f => !/support\/(test|doc)/.test(f[0]))
|
|
16
|
+
.filter(f => !/support\/(test|doc|pack)/.test(f[0]))
|
|
45
17
|
]
|
|
46
18
|
.filter(([, t]) => t === 'ts' || t === 'js' || t === 'json')
|
|
47
19
|
.map(([f]) => ManifestModuleUtil.sourceToOutputExt(path.resolve(m.outputFolder, f)));
|
|
@@ -55,9 +27,8 @@ export function getOutput(): OutputOptions {
|
|
|
55
27
|
return {
|
|
56
28
|
format,
|
|
57
29
|
interop: format === 'commonjs' ? 'auto' : undefined,
|
|
58
|
-
intro: INTRO(new EnvProp('BUNDLE_ENV_FILE').val)[format],
|
|
59
30
|
sourcemapPathTransform: (src, map): string =>
|
|
60
|
-
path.resolve(path.dirname(map), src)
|
|
31
|
+
RuntimeContext.stripWorkspacePath(path.resolve(path.dirname(map), src)),
|
|
61
32
|
sourcemap: new EnvProp('BUNDLE_SOURCEMAP').bool ?? false,
|
|
62
33
|
sourcemapExcludeSources: !(new EnvProp('BUNDLE_SOURCES').bool ?? false),
|
|
63
34
|
compact: new EnvProp('BUNDLE_COMPRESS').bool ?? true,
|
|
@@ -70,21 +41,21 @@ export function getEntry(): string {
|
|
|
70
41
|
return process.env.BUNDLE_ENTRY!;
|
|
71
42
|
}
|
|
72
43
|
|
|
73
|
-
export function getFiles(): string[] {
|
|
44
|
+
export function getFiles(entry?: string): string[] {
|
|
74
45
|
return [...RuntimeIndex.getModuleList('all')]
|
|
75
46
|
.map(x => RuntimeIndex.getManifestModule(x))
|
|
76
47
|
.filter(m => m.prod)
|
|
77
|
-
.flatMap(getFilesFromModule)
|
|
48
|
+
.flatMap(getFilesFromModule)
|
|
49
|
+
.filter(x => (!entry || !x.endsWith(entry)) && !x.includes('@travetto/pack/support'));
|
|
78
50
|
}
|
|
79
51
|
|
|
80
|
-
export function getIgnoredModules():
|
|
52
|
+
export function getIgnoredModules(): ManifestModule[] {
|
|
81
53
|
return [...RuntimeIndex.getModuleList('all')]
|
|
82
54
|
.map(x => RuntimeIndex.getManifestModule(x))
|
|
83
|
-
.filter(m => !m.prod)
|
|
84
|
-
.map(m => m.name);
|
|
55
|
+
.filter(m => !m.prod);
|
|
85
56
|
}
|
|
86
57
|
|
|
87
|
-
export function
|
|
58
|
+
export function getMinifyConfig(): Parameters<typeof terser>[0] {
|
|
88
59
|
return {
|
|
89
60
|
mangle: true,
|
|
90
61
|
keep_classnames: true,
|
|
@@ -97,3 +68,18 @@ export function getTerserConfig(): Parameters<typeof terser>[0] {
|
|
|
97
68
|
}
|
|
98
69
|
};
|
|
99
70
|
}
|
|
71
|
+
|
|
72
|
+
export function getCoreConfig(): CoreRollupConfig {
|
|
73
|
+
const output = getOutput();
|
|
74
|
+
const entry = getEntry();
|
|
75
|
+
const files = getFiles(entry);
|
|
76
|
+
const ignoreModules = getIgnoredModules();
|
|
77
|
+
const ignoreFiles = ignoreModules.flatMap(getFilesFromModule);
|
|
78
|
+
const minify = getMinifyConfig();
|
|
79
|
+
const envFile = new EnvProp('BUNDLE_ENV_FILE').val;
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
output, entry, files, envFile, minify,
|
|
83
|
+
ignore: new Set([...ignoreModules.map(x => x.name), ...ignoreFiles]),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { Plugin } from 'rollup';
|
|
3
|
+
|
|
4
|
+
import { RuntimeIndex } from '@travetto/manifest';
|
|
5
|
+
|
|
6
|
+
import { CoreRollupConfig } from '../../src/types';
|
|
7
|
+
|
|
8
|
+
export const GLOBAL_IMPORT = '__trv_imp';
|
|
9
|
+
|
|
10
|
+
export function travettoEntryPlugin(config: CoreRollupConfig): Plugin {
|
|
11
|
+
const imports = config.files
|
|
12
|
+
.map(x => x.split('node_modules/').pop()!)
|
|
13
|
+
.flatMap(x => x.endsWith('/__index__.js') ? [x.replace('/__index__.js', ''), x] : [x]);
|
|
14
|
+
|
|
15
|
+
const op = config.output.format === 'module' ? 'import' : 'require';
|
|
16
|
+
const importer = `
|
|
17
|
+
function trvImp(path) {
|
|
18
|
+
switch (path) {
|
|
19
|
+
${imports.map(x => ` case '${x}': return ${op}('${x}')`).join('\n')}
|
|
20
|
+
default: return ${op}(path); // Fall back for built-ins
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
globalThis.${GLOBAL_IMPORT} = trvImp;
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const out: Plugin = {
|
|
27
|
+
name: 'travetto-entry',
|
|
28
|
+
|
|
29
|
+
intro() {
|
|
30
|
+
return readFileSync(RuntimeIndex.getFromImport('@travetto/pack/support/bin/preamble')!.outputFile, 'utf8')
|
|
31
|
+
.replaceAll('%%ENV_FILE%%', config.envFile ?? '')
|
|
32
|
+
.replace(/\/\/# source.*$/m, '');
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
load(id) {
|
|
36
|
+
if (id.endsWith(config.entry)) {
|
|
37
|
+
return `${importer}${readFileSync(id, 'utf8')}`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { AstNode, Plugin } from 'rollup';
|
|
2
|
+
import { walk } from 'estree-walker';
|
|
3
|
+
import magicString from 'magic-string';
|
|
4
|
+
|
|
5
|
+
import { GLOBAL_IMPORT } from './rollup-travetto-entry';
|
|
6
|
+
import { CoreRollupConfig } from '../../src/types';
|
|
7
|
+
|
|
8
|
+
type TNode = AstNode & { source?: { type: string }, callee?: TNode & { name?: string }, args?: TNode[] };
|
|
9
|
+
|
|
10
|
+
export function travettoImportPlugin(config: CoreRollupConfig): Plugin {
|
|
11
|
+
const fileSet = new Set(config.files);
|
|
12
|
+
|
|
13
|
+
const out: Plugin = {
|
|
14
|
+
name: 'travetto-import',
|
|
15
|
+
|
|
16
|
+
resolveDynamicImport(specifier, importer, options) {
|
|
17
|
+
const key = typeof specifier === 'string' ? specifier : '';
|
|
18
|
+
if (config.ignore.has(key)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
transform(code, id) {
|
|
24
|
+
const parsed = this.parse(code);
|
|
25
|
+
|
|
26
|
+
if (id.includes('support/entry') || (!fileSet.has(id) && !id.includes('@travetto'))) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let ms: magicString | undefined;
|
|
31
|
+
|
|
32
|
+
walk(parsed, {
|
|
33
|
+
enter: (node) => {
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
35
|
+
const impNode = node as TNode;
|
|
36
|
+
if (impNode.type === 'ImportExpression' && impNode.source?.type !== 'Literal') {
|
|
37
|
+
if (!/["']/.test(code.substring(impNode.start, impNode.end))) {
|
|
38
|
+
(ms ??= new magicString(code)).overwrite(impNode.start, impNode.start + 6, GLOBAL_IMPORT);
|
|
39
|
+
}
|
|
40
|
+
} else if (impNode.type === 'CallExpression' && impNode.callee?.type === 'Identifier' && impNode.callee.name === 'require') {
|
|
41
|
+
if (!/["']/.test(code.substring(impNode.start, impNode.end))) {
|
|
42
|
+
(ms ??= new magicString(code)).overwrite(impNode.start, impNode.start + 7, GLOBAL_IMPORT);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (ms !== undefined) {
|
|
49
|
+
return {
|
|
50
|
+
code: ms.toString(),
|
|
51
|
+
map: ms.generateMap({ file: id, includeContent: true, hires: true })
|
|
52
|
+
};
|
|
53
|
+
} else {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
@@ -2,14 +2,18 @@ import path from 'node:path';
|
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
|
|
4
4
|
import { LoadResult, Plugin, PluginContext } from 'rollup';
|
|
5
|
+
import { CoreRollupConfig } from '../../src/types';
|
|
5
6
|
|
|
6
7
|
function toString(error: unknown): string {
|
|
7
8
|
return error instanceof Error ? error.stack ?? error.toString() : JSON.stringify(error);
|
|
8
9
|
}
|
|
9
10
|
// Pulled from https://github.com/Azure/azure-sdk-for-js/blob/main/common/tools/dev-tool/src/config/rollup.base.config.ts#L128
|
|
10
|
-
export function
|
|
11
|
+
export function travettoSourcemaps(config: CoreRollupConfig): Plugin {
|
|
12
|
+
if (config.output.sourcemap === 'hidden' || config.output.sourcemap === false) {
|
|
13
|
+
return { name: 'travetto-source-maps' };
|
|
14
|
+
}
|
|
11
15
|
return {
|
|
12
|
-
name: '
|
|
16
|
+
name: 'travetto-source-maps',
|
|
13
17
|
async load(this: PluginContext, id: string): Promise<LoadResult> {
|
|
14
18
|
if (!id.endsWith('.js')) {
|
|
15
19
|
return null;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { AstNode, Plugin } from 'rollup';
|
|
2
|
-
import { walk } from 'estree-walker';
|
|
3
|
-
import magicString from 'magic-string';
|
|
4
|
-
|
|
5
|
-
const BRAND = '__imp';
|
|
6
|
-
|
|
7
|
-
const DYNAMIC_IMPORT = (imports: string[]): string => `
|
|
8
|
-
function ${BRAND}(path) {
|
|
9
|
-
switch (path) {
|
|
10
|
-
${imports.map(p => ` case '${p}': return import('${p}');`).join('\n')}
|
|
11
|
-
default: return import(path); // Fall back for built-ins
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
globalThis.${BRAND} = ${BRAND}`;
|
|
15
|
-
|
|
16
|
-
export function travettoImportPlugin(entry: string, files: string[]): Plugin {
|
|
17
|
-
const imports = files
|
|
18
|
-
.map(x => x.split('node_modules/').pop()!)
|
|
19
|
-
.flatMap(x => x.endsWith('__index__.js') ? [x.replace('__index__.js', ''), x] : [x]);
|
|
20
|
-
|
|
21
|
-
const out: Plugin = {
|
|
22
|
-
name: 'travetto-import',
|
|
23
|
-
transform(code, id) {
|
|
24
|
-
const parsed = this.parse(code);
|
|
25
|
-
|
|
26
|
-
let ms: magicString | undefined;
|
|
27
|
-
|
|
28
|
-
if (id.includes(entry)) {
|
|
29
|
-
(ms ??= new magicString(code).append(DYNAMIC_IMPORT(imports)));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
walk(parsed, {
|
|
33
|
-
enter: (node) => {
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
35
|
-
const impNode = node as AstNode & { source?: { type: string } };
|
|
36
|
-
if (impNode.type !== 'ImportExpression' || impNode.source?.type === 'Literal') {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
(ms ??= new magicString(code)).overwrite(impNode.start, impNode.start + 6, BRAND);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
if (ms !== undefined) {
|
|
44
|
-
return {
|
|
45
|
-
code: ms.toString(),
|
|
46
|
-
map: ms.generateMap({ file: id, includeContent: true, hires: true })
|
|
47
|
-
};
|
|
48
|
-
} else {
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
return out;
|
|
55
|
-
}
|
package/support/bin/rollup.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import commonjsRequire from '@rollup/plugin-commonjs';
|
|
2
|
-
import nodeResolve from '@rollup/plugin-node-resolve';
|
|
3
|
-
import terser from '@rollup/plugin-terser';
|
|
4
|
-
import jsonImport from '@rollup/plugin-json';
|
|
5
|
-
import type { RollupOptions } from 'rollup';
|
|
6
|
-
|
|
7
|
-
import { RuntimeContext } from '@travetto/manifest';
|
|
8
|
-
|
|
9
|
-
import { getEntry, getOutput, getTerserConfig, getFiles, getIgnoredModules } from './config';
|
|
10
|
-
import { travettoImportPlugin } from './rollup-esm-dynamic-import';
|
|
11
|
-
import { sourcemaps } from './rollup-sourcemaps';
|
|
12
|
-
|
|
13
|
-
const NEVER_INCLUDE = new Set<string>([]);
|
|
14
|
-
|
|
15
|
-
export default function buildConfig(): RollupOptions {
|
|
16
|
-
const output = getOutput();
|
|
17
|
-
const entry = getEntry();
|
|
18
|
-
const files = getFiles();
|
|
19
|
-
const ignore = getIgnoredModules();
|
|
20
|
-
const ignoreRe = new RegExp(`^(${ignore.join('|')})`);
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
input: [entry],
|
|
24
|
-
output,
|
|
25
|
-
plugins: [
|
|
26
|
-
jsonImport(),
|
|
27
|
-
commonjsRequire({
|
|
28
|
-
ignore: id => ignoreRe.test(id) || NEVER_INCLUDE.has(id),
|
|
29
|
-
dynamicRequireRoot: RuntimeContext.workspace.path,
|
|
30
|
-
dynamicRequireTargets: (output.format === 'commonjs' ? files : [])
|
|
31
|
-
}),
|
|
32
|
-
...(output.format === 'module' ? [travettoImportPlugin(entry, files)] : []),
|
|
33
|
-
nodeResolve({ preferBuiltins: true }),
|
|
34
|
-
...(output.sourcemap !== 'hidden' && output.sourcemap !== false ? [sourcemaps()] : []),
|
|
35
|
-
...(output.compact ? [terser(getTerserConfig())] : [])
|
|
36
|
-
]
|
|
37
|
-
};
|
|
38
|
-
}
|
|
File without changes
|