mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
4.9 KiB
183 lines
4.9 KiB
2 years ago
|
import fs from 'node:fs';
|
||
|
import { createRequire } from 'node:module';
|
||
5 years ago
|
import replace from '@rollup/plugin-replace';
|
||
5 years ago
|
import resolve from '@rollup/plugin-node-resolve';
|
||
|
import commonjs from '@rollup/plugin-commonjs';
|
||
|
import json from '@rollup/plugin-json';
|
||
|
import sucrase from '@rollup/plugin-sucrase';
|
||
|
import typescript from '@rollup/plugin-typescript';
|
||
2 years ago
|
|
||
|
const require = createRequire(import.meta.url);
|
||
|
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
|
||
8 years ago
|
|
||
6 years ago
|
const is_publish = !!process.env.PUBLISH;
|
||
|
|
||
6 years ago
|
const ts_plugin = is_publish
|
||
6 years ago
|
? typescript({
|
||
|
typescript: require('typescript')
|
||
|
})
|
||
|
: sucrase({
|
||
|
transforms: ['typescript']
|
||
|
});
|
||
|
|
||
2 years ago
|
// The following external and path logic is necessary so that the bundled runtime pieces and the index file
|
||
|
// reference each other correctly instead of bundling their references to each other
|
||
|
|
||
|
/**
|
||
|
* Ensures that relative imports inside `src/runtime` like `./internal` and `../store` are externalized correctly
|
||
|
*/
|
||
|
const external = (id, parent_id) => {
|
||
|
const parent_segments = parent_id.replace(/\\/g, '/').split('/');
|
||
|
// 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
|
||
|
*/
|
||
|
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}`;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
fs.writeFileSync(`./compiler.d.ts`, `export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index';`);
|
||
6 years ago
|
|
||
6 years ago
|
export default [
|
||
|
/* runtime */
|
||
6 years ago
|
{
|
||
6 years ago
|
input: `src/runtime/index.ts`,
|
||
6 years ago
|
output: [
|
||
|
{
|
||
6 years ago
|
file: `index.mjs`,
|
||
6 years ago
|
format: 'esm',
|
||
2 years ago
|
paths: id => replace_relative_internal_import(id, 'index.mjs')
|
||
6 years ago
|
},
|
||
|
{
|
||
6 years ago
|
file: `index.js`,
|
||
6 years ago
|
format: 'cjs',
|
||
2 years ago
|
paths: id => replace_relative_internal_import(id, 'index.js')
|
||
6 years ago
|
}
|
||
6 years ago
|
],
|
||
6 years ago
|
external,
|
||
|
plugins: [ts_plugin]
|
||
6 years ago
|
},
|
||
6 years ago
|
|
||
4 years ago
|
{
|
||
|
input: `src/runtime/ssr.ts`,
|
||
|
output: [
|
||
|
{
|
||
|
file: `ssr.mjs`,
|
||
|
format: 'esm',
|
||
2 years ago
|
paths: id => replace_relative_internal_import(id, 'index.mjs')
|
||
4 years ago
|
},
|
||
|
{
|
||
|
file: `ssr.js`,
|
||
|
format: 'cjs',
|
||
2 years ago
|
paths: id => replace_relative_internal_import(id, 'index.js')
|
||
4 years ago
|
}
|
||
|
],
|
||
|
external,
|
||
|
plugins: [ts_plugin]
|
||
|
},
|
||
|
|
||
6 years ago
|
...fs.readdirSync('src/runtime')
|
||
|
.filter(dir => fs.statSync(`src/runtime/${dir}`).isDirectory())
|
||
|
.map(dir => ({
|
||
|
input: `src/runtime/${dir}/index.ts`,
|
||
|
output: [
|
||
|
{
|
||
|
file: `${dir}/index.mjs`,
|
||
|
format: 'esm',
|
||
2 years ago
|
paths: id => replace_relative_svelte_imports(id, 'index.mjs')
|
||
6 years ago
|
},
|
||
|
{
|
||
|
file: `${dir}/index.js`,
|
||
|
format: 'cjs',
|
||
2 years ago
|
paths: id => replace_relative_svelte_imports(id, 'index.js')
|
||
6 years ago
|
}
|
||
|
],
|
||
|
external,
|
||
6 years ago
|
plugins: [
|
||
5 years ago
|
replace({
|
||
|
__VERSION__: pkg.version
|
||
|
}),
|
||
6 years ago
|
ts_plugin,
|
||
6 years ago
|
{
|
||
2 years ago
|
writeBundle(_options, bundle) {
|
||
6 years ago
|
if (dir === 'internal') {
|
||
|
const mod = bundle['index.mjs'];
|
||
|
if (mod) {
|
||
6 years ago
|
fs.writeFileSync('src/compiler/compile/internal_exports.ts', `// This file is automatically generated\nexport default new Set(${JSON.stringify(mod.exports)});`);
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
fs.writeFileSync(`${dir}/package.json`, JSON.stringify({
|
||
|
main: './index',
|
||
|
module: './index.mjs',
|
||
|
types: './index.d.ts'
|
||
|
}, null, ' '));
|
||
|
|
||
|
fs.writeFileSync(`${dir}/index.d.ts`, `export * from '../types/runtime/${dir}/index';`);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
]
|
||
6 years ago
|
})),
|
||
|
|
||
|
/* compiler.js */
|
||
|
{
|
||
|
input: 'src/compiler/index.ts',
|
||
|
plugins: [
|
||
|
replace({
|
||
2 years ago
|
__VERSION__: pkg.version,
|
||
|
'process.env.NODE_DEBUG': false // appears inside the util package
|
||
6 years ago
|
}),
|
||
2 years ago
|
{
|
||
|
resolveId(id) {
|
||
|
// util is a built-in module in Node.js, but we want a self-contained compiler bundle
|
||
|
// that also works in the browser, so we load its polyfill instead
|
||
|
if (id === 'util') {
|
||
|
return require.resolve('./node_modules/util'); // just 'utils' would resolve this to the built-in module
|
||
|
}
|
||
|
}
|
||
|
},
|
||
6 years ago
|
resolve(),
|
||
|
commonjs({
|
||
|
include: ['node_modules/**']
|
||
|
}),
|
||
|
json(),
|
||
|
ts_plugin
|
||
|
],
|
||
4 years ago
|
output: [
|
||
|
{
|
||
|
file: 'compiler.js',
|
||
|
format: is_publish ? 'umd' : 'cjs',
|
||
|
name: 'svelte',
|
||
|
sourcemap: true,
|
||
|
},
|
||
|
{
|
||
|
file: 'compiler.mjs',
|
||
|
format: 'esm',
|
||
|
name: 'svelte',
|
||
|
sourcemap: true,
|
||
|
}
|
||
|
],
|
||
6 years ago
|
external: is_publish
|
||
|
? []
|
||
|
: id => id === 'acorn' || id === 'magic-string' || id.startsWith('css-tree')
|
||
|
}
|
||
8 years ago
|
];
|