Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: unbundled esm #8613

Merged
merged 18 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
node_modules
*.map
/src/compiler/compile/internal_exports.js
/src/shared/version.js
/compiler.d.ts
/compiler.*js
/index.*js
/ssr.*js
/action
/internal
/store
/easing
/motion
/transition
/animate
/compiler.cjs
/index.d.ts
/action.d.ts
/internal.d.ts
/store.d.ts
/easing.d.ts
/motion.d.ts
/transition.d.ts
/animate.d.ts
/scratch/
/test/*/samples/_
/test/runtime/shards
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
/site
!/src
src/compiler/compile/internal_exports.js
src/shared/version.js
!/test
/test/**/*.svelte
/test/**/_expected*
/test/**/_actual*
/test/**/expected*
/test/**/_output
/types
!rollup.config.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment explaining why this needs to be ignored

Copy link
Member Author

@dummdidumm dummdidumm May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ! means it's not ignored, which it was previously 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, duh. the format of this file is rather peculiar where it ignores everything in the root directory by default and then only opts files in. I wonder if we could swap that once the CJS version is removed

!vitest.config.js
File renamed without changes.
3 changes: 0 additions & 3 deletions elements/package.json

This file was deleted.

50 changes: 21 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,75 +6,67 @@
"module": "index.mjs",
"main": "index",
"files": [
"src",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should separate the dependencies and devDependencies in this file as was being done in #8523. i'm not sure if you want to try to merge that PR first or copy those changes into this PR

Copy link
Contributor

@gtm-nayan gtm-nayan May 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If copying over the changes, source-map needs to be kept and so does the util polyfill for the UMD bundle.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. I updated #8523 to address that

"types",
"compiler.*",
"register.js",
"index.*",
"ssr.*",
"internal",
"store",
"animate",
"transition",
"easing",
"motion",
"action",
"elements",
"index.d.ts",
"internal.d.ts",
"store.d.ts",
"animate.d.ts",
"transition.d.ts",
"easing.d.ts",
"motion.d.ts",
"action.d.ts",
"elements.d.ts",
Comment on lines +13 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these needed in addition to the exports map because of the moduleResolution thing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

"README.md"
],
"exports": {
"./package.json": "./package.json",
".": {
"types": "./types/runtime/index.d.ts",
"browser": {
"import": "./index.mjs",
"require": "./index.js"
"import": "./src/runtime/index.js"
},
"import": "./ssr.mjs",
"require": "./ssr.js"
"import": "./src/runtime/ssr.js"
},
"./compiler": {
"types": "./types/compiler/index.d.ts",
"import": "./compiler.mjs",
"require": "./compiler.js"
"import": "./src/compiler/index.js",
"require": "./compiler.cjs"
},
"./action": {
"types": "./types/runtime/action/index.d.ts"
},
"./animate": {
"types": "./types/runtime/animate/index.d.ts",
"import": "./animate/index.mjs",
"require": "./animate/index.js"
"import": "./src/runtime/animate/index.js"
},
"./easing": {
"types": "./types/runtime/easing/index.d.ts",
"import": "./easing/index.mjs",
"require": "./easing/index.js"
"import": "./src/runtime/easing/index.js"
},
"./internal": {
"types": "./types/runtime/internal/index.d.ts",
"import": "./internal/index.mjs",
"require": "./internal/index.js"
"import": "./src/runtime/internal/index.js"
},
"./motion": {
"types": "./types/runtime/motion/index.d.ts",
"import": "./motion/index.mjs",
"require": "./motion/index.js"
"import": "./src/runtime/motion/index.js"
},
"./register": {
"require": "./register.js"
},
"./store": {
"types": "./types/runtime/store/index.d.ts",
"import": "./store/index.mjs",
"require": "./store/index.js"
"import": "./src/runtime/store/index.js"
},
"./transition": {
"types": "./types/runtime/transition/index.d.ts",
"import": "./transition/index.mjs",
"require": "./transition/index.js"
"import": "./src/runtime/transition/index.js"
},
"./elements": {
"types": "./elements/index.d.ts"
"types": "./elements.d.ts"
}
},
"engines": {
Expand Down
82 changes: 82 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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/Pretter (which need CJS) and REPL (which needs UMD because browser) can use it
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
{
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we still have sourcemaps for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We decided to remove it because rarely any tool depends on it, and those who do can still backtrack manually

},
external: []
}
];
150 changes: 0 additions & 150 deletions rollup.config.mjs

This file was deleted.

3 changes: 2 additions & 1 deletion src/compiler/compile/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
extract_svelte_ignore_from_comments
} from '../utils/extract_svelte_ignore.js';
import check_enable_sourcemap from './utils/check_enable_sourcemap.js';
import { VERSION } from '../../shared/version.js';

const regex_leading_directory_separator = /^[/\\]/;
const regex_starts_with_term_export = /^Export/;
Expand Down Expand Up @@ -320,7 +321,7 @@ export default class Component {
if (result) {
const { compile_options, name } = this;
const { format = 'esm' } = compile_options;
const banner = `${this.file ? `${this.file} ` : ''}generated by Svelte v${'__VERSION__'}`;
const banner = `${this.file ? `${this.file} ` : ''}generated by Svelte v${VERSION}`;

/** @type {any} */
const program = { type: 'Program', body: result.js };
Expand Down
Loading