chore: produce single bundle for runtime with multiple entrypoints (#8504)

* single runtime bundle

* formatting

* dedupe output options

* fix tests apparently

* skip writeBundle for cjs build

* revert quotes

* remove manualChunks

* some node16 module resolution compliance

* disable minifyInternalExports (doesn't really make sense for a library since users'
build step will do it again anyway)
pull/8515/head
gtmnayan 2 years ago committed by Simon Holthausen
parent 5d4f3bd9e5
commit 2f423475f7

@ -14,136 +14,113 @@ const is_publish = !!process.env.PUBLISH;
const ts_plugin = is_publish const ts_plugin = is_publish
? typescript({ ? typescript({
typescript: require('typescript') typescript: require('typescript'),
}) })
: sucrase({ : sucrase({
transforms: ['typescript'] transforms: ['typescript'],
}); });
// The following external and path logic is necessary so that the bundled runtime pieces and the index file fs.writeFileSync(
// reference each other correctly instead of bundling their references to each other `./compiler.d.ts`,
`export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index.js';`
);
/** const runtime_entrypoints = Object.fromEntries(
* Ensures that relative imports inside `src/runtime` like `./internal` and `../store` are externalized correctly fs
*/ .readdirSync('src/runtime', { withFileTypes: true })
const external = (id, parent_id) => { .filter((dirent) => dirent.isDirectory())
const parent_segments = parent_id.replace(/\\/g, '/').split('/'); .map((dirent) => [dirent.name, `src/runtime/${dirent.name}/index.ts`])
// TODO needs to be adjusted when we move to JS modules );
if (parent_segments[parent_segments.length - 3] === 'runtime') {
return /\.\.\/\w+$/.test(id);
} else {
return id === './internal' && parent_segments[parent_segments.length - 2] === 'runtime';
}
}
/**
* Transforms externalized import paths like `../store` into correct relative imports with correct index file extension import
*/
const replace_relative_svelte_imports = (id, ending) => {
id = id.replace(/\\/g, '/');
// TODO needs to be adjusted when we move to JS modules
return /src\/runtime\/\w+$/.test(id) && `../${id.split('/').pop()}/${ending}`;
}
/** /**
* Transforms externalized `./internal` import path into correct relative import with correct index file extension import * @type {import("rollup").RollupOptions[]}
*/ */
const replace_relative_internal_import = (id, ending) => {
id = id.replace(/\\/g, '/');
// TODO needs to be adjusted when we move to JS modules
return id.endsWith('src/runtime/internal') && `./internal/${ending}`;
}
fs.writeFileSync(`./compiler.d.ts`, `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';`);
export default [ export default [
/* runtime */
{ {
input: `src/runtime/index.ts`, input: {
output: [ ...runtime_entrypoints,
{ index: 'src/runtime/index.ts',
file: `index.mjs`, ssr: 'src/runtime/ssr.ts'
format: 'esm', },
paths: id => replace_relative_internal_import(id, 'index.mjs') output: ['es', 'cjs'].map(
}, /** @returns {import('rollup').OutputOptions} */
{ (format) => {
file: `index.js`, const ext = format === 'es' ? 'mjs' : 'js';
format: 'cjs', return {
paths: id => replace_relative_internal_import(id, 'index.js') entryFileNames: (entry) => {
} if (entry.isEntry) {
], if (entry.name === 'index') return `index.${ext}`;
external, else if (entry.name === 'ssr') return `ssr.${ext}`;
plugins: [ts_plugin]
},
{ return `${entry.name}/index.${ext}`;
input: `src/runtime/ssr.ts`, }
output: [ },
{ chunkFileNames: `internal/[name]-[hash].${ext}`,
file: `ssr.mjs`, format,
format: 'esm', minifyInternalExports: false,
paths: id => replace_relative_internal_import(id, 'index.mjs') dir: '.',
}, };
{
file: `ssr.js`,
format: 'cjs',
paths: id => replace_relative_internal_import(id, 'index.js')
} }
], ),
external, plugins: [
plugins: [ts_plugin] replace({
}, preventAssignment: true,
values: {
...fs.readdirSync('src/runtime') __VERSION__: pkg.version,
.filter(dir => fs.statSync(`src/runtime/${dir}`).isDirectory())
.map(dir => ({
input: `src/runtime/${dir}/index.ts`,
output: [
{
file: `${dir}/index.mjs`,
format: 'esm',
paths: id => replace_relative_svelte_imports(id, 'index.mjs')
}, },
{ }),
file: `${dir}/index.js`, ts_plugin,
format: 'cjs', {
paths: id => replace_relative_svelte_imports(id, 'index.js') writeBundle(options, bundle) {
} if (options.format !== 'es') return;
],
external, for (const entry of Object.values(bundle)) {
plugins: [ const dir = entry.name;
replace({ if (!entry.isEntry || !runtime_entrypoints[dir]) continue;
__VERSION__: pkg.version
}),
ts_plugin,
{
writeBundle(_options, bundle) {
if (dir === 'internal') { if (dir === 'internal') {
const mod = bundle['index.mjs']; const mod = bundle[`internal/index.mjs`];
if (mod) { if (mod) {
fs.writeFileSync('src/compiler/compile/internal_exports.ts', `// This file is automatically generated\nexport default new Set(${JSON.stringify(mod.exports)});`); fs.writeFileSync(
'src/compiler/compile/internal_exports.ts',
`// This file is automatically generated\n` +
`export default new Set(${JSON.stringify(mod.exports)});`
);
} }
} }
fs.writeFileSync(`${dir}/package.json`, JSON.stringify({ fs.writeFileSync(
main: './index', `${dir}/package.json`,
module: './index.mjs', JSON.stringify(
types: './index.d.ts' {
}, null, ' ')); main: './index.js',
module: './index.mjs',
types: './index.d.ts',
},
null,
' '
)
);
fs.writeFileSync(`${dir}/index.d.ts`, `export * from '../types/runtime/${dir}/index';`); fs.writeFileSync(
`${dir}/index.d.ts`,
`export * from '../types/runtime/${dir}/index.js';`
);
} }
} }
] }
})), ]
},
/* compiler.js */ /* compiler.js */
{ {
input: 'src/compiler/index.ts', input: 'src/compiler/index.ts',
plugins: [ plugins: [
replace({ replace({
__VERSION__: pkg.version, preventAssignment: true,
'process.env.NODE_DEBUG': false // appears inside the util package values: {
__VERSION__: pkg.version,
'process.env.NODE_DEBUG': false // appears inside the util package
},
}), }),
{ {
resolveId(id) { resolveId(id) {
@ -152,7 +129,7 @@ export default [
if (id === 'util') { if (id === 'util') {
return require.resolve('./node_modules/util'); // just 'utils' would resolve this to the built-in module return require.resolve('./node_modules/util'); // just 'utils' would resolve this to the built-in module
} }
} },
}, },
resolve(), resolve(),
commonjs({ commonjs({
@ -177,6 +154,7 @@ export default [
], ],
external: is_publish external: is_publish
? [] ? []
: id => id === 'acorn' || id === 'magic-string' || id.startsWith('css-tree') : (id) =>
id === 'acorn' || id === 'magic-string' || id.startsWith('css-tree')
} }
]; ];

Loading…
Cancel
Save