mirror of https://github.com/sveltejs/svelte
chore: unbundled esm (#8613)
- remove esm bundle step - introduce generated version.js because we can no longer use replace because we don't bundle esm - remove register hook, cjs compiler output and cjs runtime - keep umd compiler version for prettier/eslint/browser but without sourcemaps - move devdependencies to dependencies where necessary - various cleanup --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>pull/8628/head
parent
d658e7036e
commit
a40af4dd11
@ -1,4 +0,0 @@
|
||||
if (!process.env.PUBLISH) {
|
||||
console.error('npm publish must be run with the PUBLISH environment variable set');
|
||||
process.exit(1);
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"types": "./index.d.ts"
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,56 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { compile } = require('./compiler.js');
|
||||
|
||||
const extensions = ['.svelte', '.html'];
|
||||
let compileOptions = {};
|
||||
|
||||
function capitalise(name) {
|
||||
return name[0].toUpperCase() + name.slice(1);
|
||||
}
|
||||
|
||||
function register(options = {}) {
|
||||
if (options.extensions) {
|
||||
extensions.forEach(deregisterExtension);
|
||||
options.extensions.forEach(registerExtension);
|
||||
}
|
||||
|
||||
compileOptions = Object.assign({}, options);
|
||||
delete compileOptions.extensions;
|
||||
}
|
||||
|
||||
function deregisterExtension(extension) {
|
||||
delete require.extensions[extension];
|
||||
}
|
||||
|
||||
function registerExtension(extension) {
|
||||
require.extensions[extension] = function(module, filename) {
|
||||
const name = path.parse(filename).name
|
||||
.replace(/^\d/, '_$&')
|
||||
.replace(/[^a-zA-Z0-9_$]/g, '');
|
||||
|
||||
const options = Object.assign({}, compileOptions, {
|
||||
filename,
|
||||
name: capitalise(name),
|
||||
generate: 'ssr',
|
||||
format: 'cjs'
|
||||
});
|
||||
|
||||
const { js, warnings } = compile(fs.readFileSync(filename, 'utf-8'), options);
|
||||
|
||||
if (options.dev) {
|
||||
warnings.forEach(warning => {
|
||||
console.warn(`\nSvelte Warning in ${warning.filename}:`);
|
||||
console.warn(warning.message);
|
||||
console.warn(warning.frame);
|
||||
})
|
||||
}
|
||||
|
||||
return module._compile(js.code, filename);
|
||||
};
|
||||
}
|
||||
|
||||
registerExtension('.svelte');
|
||||
registerExtension('.html');
|
||||
|
||||
module.exports = register;
|
@ -0,0 +1,82 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import replace from '@rollup/plugin-replace';
|
||||
import resolve from '@rollup/plugin-node-resolve';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
import json from '@rollup/plugin-json';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
|
||||
|
||||
// Create auto-generated .js files
|
||||
fs.writeFileSync(
|
||||
'./src/shared/version.js',
|
||||
`/** @type {string} */\nexport const VERSION = '${pkg.version}';`
|
||||
);
|
||||
|
||||
const internal = await import('./src/runtime/internal/index.js');
|
||||
fs.writeFileSync(
|
||||
'src/compiler/compile/internal_exports.js',
|
||||
`// This file is automatically generated\n` +
|
||||
`export default new Set(${JSON.stringify(Object.keys(internal))});`
|
||||
);
|
||||
|
||||
// Generate d.ts files for runtime entrypoints so that TS can find it also without `"moduleResolution": "bundler"`
|
||||
fs.readdirSync('src/runtime', { withFileTypes: true })
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.forEach((dirent) =>
|
||||
fs.writeFileSync(
|
||||
`${dirent.name}.d.ts`,
|
||||
`export * from './types/runtime/${dirent.name}/index.js';`
|
||||
)
|
||||
);
|
||||
|
||||
fs.writeFileSync('./index.d.ts', `export * from './types/runtime/index.js';`);
|
||||
|
||||
fs.writeFileSync(
|
||||
'./compiler.d.ts',
|
||||
`export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index.js';`
|
||||
);
|
||||
|
||||
/**
|
||||
* @type {import("rollup").RollupOptions[]}
|
||||
*/
|
||||
export default [
|
||||
// Generate UMD build of the compiler so that Eslint/Prettier (which need CJS) and REPL (which needs UMD because browser) can use it
|
||||
{
|
||||
input: 'src/compiler/index.js',
|
||||
plugins: [
|
||||
replace({
|
||||
preventAssignment: true,
|
||||
values: {
|
||||
'process.env.NODE_DEBUG': false // appears inside the util package
|
||||
}
|
||||
}),
|
||||
{
|
||||
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
|
||||
}
|
||||
// Must import from the `css-tree` browser bundled distribution due to `createRequire` usage if importing from css-tree directly
|
||||
if (id === 'css-tree') {
|
||||
return require.resolve('./node_modules/css-tree/dist/csstree.esm.js');
|
||||
}
|
||||
}
|
||||
},
|
||||
resolve(),
|
||||
commonjs({
|
||||
include: ['node_modules/**']
|
||||
}),
|
||||
json()
|
||||
],
|
||||
output: {
|
||||
file: 'compiler.cjs',
|
||||
format: 'umd',
|
||||
name: 'svelte',
|
||||
sourcemap: false
|
||||
},
|
||||
external: []
|
||||
}
|
||||
];
|
@ -1,150 +0,0 @@
|
||||
import fs from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import replace from '@rollup/plugin-replace';
|
||||
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';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
|
||||
|
||||
const is_publish = !!process.env.PUBLISH;
|
||||
|
||||
const ts_plugin = is_publish
|
||||
? typescript({
|
||||
typescript: require('typescript'),
|
||||
paths: {
|
||||
'svelte/*': ['./src/runtime/*']
|
||||
}
|
||||
})
|
||||
: sucrase({
|
||||
transforms: ['typescript']
|
||||
});
|
||||
|
||||
fs.writeFileSync(
|
||||
`./compiler.d.ts`,
|
||||
`export { compile, parse, preprocess, walk, VERSION } from './types/compiler/index.js';`
|
||||
);
|
||||
|
||||
const runtime_entrypoints = Object.fromEntries(
|
||||
fs
|
||||
.readdirSync('src/runtime', { withFileTypes: true })
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => [dirent.name, `src/runtime/${dirent.name}/index.js`])
|
||||
);
|
||||
|
||||
/**
|
||||
* @type {import("rollup").RollupOptions[]}
|
||||
*/
|
||||
export default [
|
||||
{
|
||||
input: {
|
||||
...runtime_entrypoints,
|
||||
index: 'src/runtime/index.js',
|
||||
ssr: 'src/runtime/ssr.js'
|
||||
},
|
||||
output: ['es', 'cjs'].map(
|
||||
/** @returns {import('rollup').OutputOptions} */
|
||||
(format) => {
|
||||
const ext = format === 'es' ? 'mjs' : 'js';
|
||||
return {
|
||||
entryFileNames: (entry) => {
|
||||
if (entry.isEntry) {
|
||||
if (entry.name === 'index') return `index.${ext}`;
|
||||
else if (entry.name === 'ssr') return `ssr.${ext}`;
|
||||
|
||||
return `${entry.name}/index.${ext}`;
|
||||
}
|
||||
},
|
||||
chunkFileNames: `internal/[name]-[hash].${ext}`,
|
||||
format,
|
||||
minifyInternalExports: false,
|
||||
dir: '.',
|
||||
};
|
||||
}
|
||||
),
|
||||
plugins: [
|
||||
replace({
|
||||
preventAssignment: true,
|
||||
values: {
|
||||
__VERSION__: pkg.version,
|
||||
},
|
||||
}),
|
||||
ts_plugin,
|
||||
{
|
||||
writeBundle(options, bundle) {
|
||||
if (options.format !== 'es') return;
|
||||
|
||||
for (const entry of Object.values(bundle)) {
|
||||
const dir = entry.name;
|
||||
if (!entry.isEntry || !runtime_entrypoints[dir]) continue;
|
||||
|
||||
if (dir === 'internal') {
|
||||
const mod = bundle[`internal/index.mjs`];
|
||||
if (mod) {
|
||||
fs.writeFileSync(
|
||||
'src/compiler/compile/internal_exports.js',
|
||||
`// This file is automatically generated\n` +
|
||||
`export default new Set(${JSON.stringify(mod.exports)});`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
`${dir}/index.d.ts`,
|
||||
`export * from '../types/runtime/${dir}/index.js';`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
/* compiler.js */
|
||||
{
|
||||
input: 'src/compiler/index.js',
|
||||
plugins: [
|
||||
replace({
|
||||
preventAssignment: true,
|
||||
values: {
|
||||
__VERSION__: pkg.version,
|
||||
'process.env.NODE_DEBUG': false // appears inside the util package
|
||||
},
|
||||
}),
|
||||
{
|
||||
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
|
||||
}
|
||||
},
|
||||
},
|
||||
resolve(),
|
||||
commonjs({
|
||||
include: ['node_modules/**']
|
||||
}),
|
||||
json(),
|
||||
ts_plugin
|
||||
],
|
||||
output: [
|
||||
{
|
||||
file: 'compiler.js',
|
||||
format: is_publish ? 'umd' : 'cjs',
|
||||
name: 'svelte',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: 'compiler.mjs',
|
||||
format: 'esm',
|
||||
name: 'svelte',
|
||||
sourcemap: true,
|
||||
}
|
||||
],
|
||||
external: is_publish
|
||||
? []
|
||||
: (id) =>
|
||||
id === 'acorn' || id === 'magic-string' || id.startsWith('css-tree')
|
||||
}
|
||||
];
|
@ -1,2 +1,2 @@
|
||||
export type { CompileOptions, ModuleFormat, EnableSourcemap, CssHashGetter } from './interfaces';
|
||||
export type { CompileOptions, EnableSourcemap, CssHashGetter } from './interfaces';
|
||||
export type * from './preprocess/public.js';
|
||||
|
@ -1,6 +0,0 @@
|
||||
export default {
|
||||
options: {
|
||||
accessors: true,
|
||||
format: 'cjs'
|
||||
}
|
||||
};
|
@ -1,36 +0,0 @@
|
||||
/* generated by Svelte vX.Y.Z */
|
||||
"use strict";
|
||||
|
||||
const { SvelteComponent, init, safe_not_equal } = require("svelte/internal");
|
||||
const { f: f_1, g: g_1 } = require("./d");
|
||||
const { h: h_1 } = require("./e");
|
||||
const { i: j } = require("./f");
|
||||
exports.e = require("./c").d;
|
||||
exports.c = require("./b").c;
|
||||
exports.a = require("./a").a;
|
||||
exports.b = require("./a").b;
|
||||
|
||||
class Component extends SvelteComponent {
|
||||
constructor(options) {
|
||||
super();
|
||||
init(this, options, null, null, safe_not_equal, {});
|
||||
}
|
||||
|
||||
get f() {
|
||||
return f_1;
|
||||
}
|
||||
|
||||
get g() {
|
||||
return g_1;
|
||||
}
|
||||
|
||||
get h() {
|
||||
return h_1;
|
||||
}
|
||||
|
||||
get j() {
|
||||
return j;
|
||||
}
|
||||
}
|
||||
|
||||
exports.default = Component;
|
@ -1,11 +0,0 @@
|
||||
<script context="module">
|
||||
export { a, b } from './a';
|
||||
export { c } from './b';
|
||||
export { d as e } from './c';
|
||||
</script>
|
||||
|
||||
<script>
|
||||
export { f, g } from './d';
|
||||
export { h } from './e';
|
||||
export { i as j } from './f';
|
||||
</script>
|
Loading…
Reference in new issue