Skip to content

Commit 35ea95b

Browse files
committed
remove cjs and register hook
1 parent f35e998 commit 35ea95b

File tree

13 files changed

+9
-251
lines changed

13 files changed

+9
-251
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* **breaking** Bundlers must specify the `browser` condition when building a frontend bundle for the browser ([#8516](https://github.com/sveltejs/svelte/pull/8516))
88
* **breaking** Minimum supported vite-plugin-svelte version is now 2.1.1. SvelteKit users can upgrade to 1.15.9 or newer to ensure a compatible version ([#8516](https://github.com/sveltejs/svelte/pull/8516))
99
* **breaking** Minimum supported TypeScript version is now TypeScript 5 (it will likely work with lower versions, but we make no guarantees about that) ([#8488](https://github.com/sveltejs/svelte/pull/8488))
10+
* **breaking** Remove `svelte/register` hook, CJS runtime version and CJS compiler output ([#8613](https://github.com/sveltejs/svelte/pull/8613))
1011
* **breaking** Stricter types for `createEventDispatcher` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224))
1112
* **breaking** Stricter types for `Action` and `ActionReturn` (see PR for migration instructions) ([#7224](https://github.com/sveltejs/svelte/pull/7224))
1213
* **breaking** Stricter types for `onMount` - now throws a type error when returning a function asynchronously to catch potential mistakes around callback functions (see PR for migration instructions) ([#8136](https://github.com/sveltejs/svelte/pull/8136))

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "4.0.0-next.0",
44
"description": "Cybernetically enhanced web apps",
55
"type": "module",
6-
"module": "index.mjs",
7-
"main": "index",
6+
"module": "src/runtime/index.js",
7+
"main": "src/runtime/index.js",
88
"files": [
99
"src",
1010
"types",
@@ -54,9 +54,6 @@
5454
"types": "./types/runtime/motion/index.d.ts",
5555
"import": "./src/runtime/motion/index.js"
5656
},
57-
"./register": {
58-
"require": "./register.js"
59-
},
6057
"./store": {
6158
"types": "./types/runtime/store/index.d.ts",
6259
"import": "./src/runtime/store/index.js"

register.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

site/content/docs/04-run-time.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,8 @@ You can explore the various eases using the [ease visualiser](/examples/easing)
985985

986986
### `svelte/register`
987987

988+
> This API is removed in Svelte 4. `require` hooks are deprecated and current Node versions understand ESM. Use a bundler like Vite or our full-stack framework [SvelteKit](https://kit.svelte.dev) instead to create JavaScript modules from Svelte components.
989+
988990
To render Svelte components in Node.js without bundling, use `require('svelte/register')`. After that, you can use `require` to include any `.svelte` file.
989991

990992
```js

site/content/docs/05-compile-time.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ The following options can be passed to the compiler. None are required:
3939
| --- | --- | --- |
4040
| `filename` | string | `null`
4141
| `name` | string | `"Component"`
42-
| `format` | `"esm"` or `"cjs"` | `"esm"`
4342
| `generate` | `"dom"` or `"ssr"` or `false` | `"dom"`
4443
| `errorMode` | `"throw"` or `"warn"` | `"throw"`
4544
| `varsReport` | `"strict"` or `"full"` or `false` | `"strict"`
@@ -62,7 +61,6 @@ The following options can be passed to the compiler. None are required:
6261
| --- | --- | --- |
6362
| `filename` | `null` | `string` used for debugging hints and sourcemaps. Your bundler plugin will set it automatically.
6463
| `name` | `"Component"` | `string` that sets the name of the resulting JavaScript class (though the compiler will rename it if it would otherwise conflict with other variables in scope). It will normally be inferred from `filename`.
65-
| `format` | `"esm"` | If `"esm"`, creates a JavaScript module (with `import` and `export`). If `"cjs"`, creates a CommonJS module (with `require` and `module.exports`), which is useful in some server-side rendering situations or for testing.
6664
| `generate` | `"dom"` | If `"dom"`, Svelte emits a JavaScript class for mounting to the DOM. If `"ssr"`, Svelte emits an object with a `render` method suitable for server-side rendering. If `false`, no JavaScript or CSS is returned; just metadata.
6765
| `errorMode` | `"throw"` | If `"throw"`, Svelte throws when a compilation error occurred. If `"warn"`, Svelte will treat errors as warnings and add them to the warning report.
6866
| `varsReport` | `"strict"` | If `"strict"`, Svelte returns a variables report with only variables that are not globals nor internals. If `"full"`, Svelte returns a variables report with all detected variables. If `false`, no variables report is returned.

src/compiler/compile/Component.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ export default class Component {
320320
let css = null;
321321
if (result) {
322322
const { compile_options, name } = this;
323-
const { format = 'esm' } = compile_options;
324323
const banner = `${this.file ? `${this.file} ` : ''}generated by Svelte v${VERSION}`;
325324

326325
/** @type {any} */
@@ -388,7 +387,6 @@ export default class Component {
388387
);
389388
create_module(
390389
program,
391-
format,
392390
name,
393391
banner,
394392
compile_options.sveltePath,

src/compiler/compile/create_module.js

Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import list from '../utils/list.js';
2-
import { b, x } from 'code-red';
3-
const wrappers = { esm, cjs };
1+
import { b } from 'code-red';
42

53
/**
64
* @param {any} program
7-
* @param {import('../interfaces.js').ModuleFormat} format
85
* @param {import('estree').Identifier} name
96
* @param {string} banner
107
* @param {any} sveltePath
@@ -16,7 +13,6 @@ const wrappers = { esm, cjs };
1613
*/
1714
export default function create_module(
1815
program,
19-
format,
2016
name,
2117
banner,
2218
sveltePath = 'svelte',
@@ -39,11 +35,7 @@ export default function create_module(
3935
* @param {any} b
4036
*/ (a, b) => (a.name < b.name ? -1 : 1)
4137
);
42-
const formatter = wrappers[format];
43-
if (!formatter) {
44-
throw new Error(`options.format is invalid (must be ${list(Object.keys(wrappers))})`);
45-
}
46-
return formatter(
38+
return esm(
4739
program,
4840
name,
4941
banner,
@@ -174,124 +166,7 @@ function esm(
174166
}
175167

176168
/**
177-
* @param {any} program
178-
* @param {import('estree').Identifier} name
179-
* @param {string} banner
180-
* @param {string} sveltePath
181-
* @param {string} internal_path
182-
* @param {Array<{ name: string; alias: import('estree').Identifier }>} helpers
183-
* @param {Array<{ name: string; alias: import('estree').Identifier }>} globals
184-
* @param {import('estree').ImportDeclaration[]} imports
185-
* @param {Export[]} module_exports
186-
* @param {import('estree').ExportNamedDeclaration[]} exports_from
187-
*/
188-
function cjs(
189-
program,
190-
name,
191-
banner,
192-
sveltePath,
193-
internal_path,
194-
helpers,
195-
globals,
196-
imports,
197-
module_exports,
198-
exports_from
199-
) {
200-
const internal_requires = {
201-
type: 'VariableDeclaration',
202-
kind: 'const',
203-
declarations: [
204-
{
205-
type: 'VariableDeclarator',
206-
id: {
207-
type: 'ObjectPattern',
208-
properties: helpers.map(
209-
/** @param {any} h */ (h) => ({
210-
type: 'Property',
211-
method: false,
212-
shorthand: false,
213-
computed: false,
214-
key: { type: 'Identifier', name: h.name },
215-
value: h.alias,
216-
kind: 'init'
217-
})
218-
)
219-
},
220-
init: x`require("${internal_path}")`
221-
}
222-
]
223-
};
224-
const internal_globals = get_internal_globals(globals, helpers);
225-
const user_requires = imports.map(
226-
/** @param {any} node */ (node) => {
227-
const init = x`require("${edit_source(node.source.value, sveltePath)}")`;
228-
if (node.specifiers.length === 0) {
229-
return b`${init};`;
230-
}
231-
return {
232-
type: 'VariableDeclaration',
233-
kind: 'const',
234-
declarations: [
235-
{
236-
type: 'VariableDeclarator',
237-
id:
238-
node.specifiers[0].type === 'ImportNamespaceSpecifier'
239-
? { type: 'Identifier', name: node.specifiers[0].local.name }
240-
: {
241-
type: 'ObjectPattern',
242-
properties: node.specifiers.map(
243-
/** @param {any} s */ (s) => ({
244-
type: 'Property',
245-
method: false,
246-
shorthand: false,
247-
computed: false,
248-
key:
249-
s.type === 'ImportSpecifier'
250-
? s.imported
251-
: { type: 'Identifier', name: 'default' },
252-
value: s.local,
253-
kind: 'init'
254-
})
255-
)
256-
},
257-
init
258-
}
259-
]
260-
};
261-
}
262-
);
263-
const exports = module_exports.map(
264-
/** @param {any} x */
265-
(x) =>
266-
b`exports.${{ type: 'Identifier', name: x.as }} = ${{ type: 'Identifier', name: x.name }};`
267-
);
268-
const user_exports_from = exports_from.map(
269-
/** @param {any} node */ (node) => {
270-
const init = x`require("${edit_source(node.source.value, sveltePath)}")`;
271-
return node.specifiers.map(
272-
/** @param {any} specifier */ (specifier) => {
273-
return b`exports.${specifier.exported} = ${init}.${specifier.local};`;
274-
}
275-
);
276-
}
277-
);
278-
program.body = b`
279-
/* ${banner} */
280-
281-
"use strict";
282-
${internal_requires}
283-
${internal_globals}
284-
${user_requires}
285-
${user_exports_from}
286-
287-
${program.body}
288-
289-
exports.default = ${name};
290-
${exports}
291-
`;
292-
}
293-
294-
/** @typedef {Object} Export
169+
* @typedef {Object} Export
295170
* @property {string} name
296171
* @property {string} as
297172
*/

src/compiler/interfaces.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ export interface Warning {
159159
toString: () => string;
160160
}
161161

162-
export type ModuleFormat = 'esm' | 'cjs';
163-
164162
export type EnableSourcemap = boolean | { js: boolean; css: boolean };
165163

166164
export type CssHashGetter = (args: {
@@ -171,7 +169,6 @@ export type CssHashGetter = (args: {
171169
}) => string;
172170

173171
export interface CompileOptions {
174-
format?: ModuleFormat;
175172
name?: string;
176173
filename?: string;
177174
generate?: 'dom' | 'ssr' | false;

src/compiler/public.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export type { CompileOptions, ModuleFormat, EnableSourcemap, CssHashGetter } from './interfaces';
1+
export type { CompileOptions, EnableSourcemap, CssHashGetter } from './interfaces';
22
export type * from './preprocess/public.js';

test/compiler-errors/compiler-errors.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe('compiler-errors', () => {
2121
const cwd = path.resolve(`${__dirname}/samples/${dir}`);
2222

2323
const compileOptions = Object.assign({}, config.compileOptions || {}, {
24-
format: 'cjs',
2524
immutable: config.immutable,
2625
accessors: 'accessors' in config ? config.accessors : true,
2726
generate: 'dom'

test/js/samples/export-from-cjs/_config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/js/samples/export-from-cjs/expected.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/js/samples/export-from-cjs/input.svelte

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)