Skip to content

Commit 8bcf6a8

Browse files
committed
convert module from commonjs to esmodule
1 parent bcb1ebe commit 8bcf6a8

8 files changed

+1909
-76
lines changed

.vscode/settings.json

-12
This file was deleted.

_components/button.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script type="module" lang="js">
22
export let title = ''
33
// since _components starts with underscore, should not generate .html automatically
44
</script>

build.js

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
const compile = require( 'svelte/compiler' ).compile
1+
import { compile } from 'svelte/compiler';
2+
import chokidar from 'chokidar';
3+
import esbuild from 'esbuild';
4+
import { readdirSync, statSync, existsSync, writeFileSync, readFileSync } from 'fs';
5+
import { join, basename, resolve, dirname, relative } from 'path';
6+
import { fileURLToPath } from 'url';
7+
import sveltePlugin from 'esbuild-svelte';
8+
import { sum } from 'lodash-es';
9+
import { parse, serialize } from 'parse5';
10+
import notifier from 'node-notifier';
11+
import svelteConfig from './svelte.config.js';
12+
import FiveServer from 'five-server';
213

3-
const chokidar = require( 'chokidar' );
4-
const esbuild = require( 'esbuild' );
5-
const {readdirSync, statSync, existsSync, writeFileSync, readFileSync} = require( 'fs' );
6-
const {join, basename, resolve, dirname, relative} = require( 'path' );
7-
const sveltePlugin = require( 'esbuild-svelte' );
8-
const {sum} = require( 'lodash' );
9-
const parse5 = require( 'parse5' );
10-
const notifier = require('node-notifier');
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = dirname(__filename);
1116

1217
process.on('uncaughtException', error => {
1318
notifier.notify({
@@ -57,12 +62,12 @@ function findPages( dir = '.', sink = [] ) {
5762
const _zId_prefix = `z_placeholder_${Math.floor( Math.random() * 1000000000 ).toString( 16 )}_`;
5863
const _zReplacer = s => debug_console_log( ['z-replace:', s, `"${_zId_prefix}${Buffer.from( s ).toString( 'base64' )}"`], 2 );
5964

60-
const zPlaceholderReplacer = content =>
61-
65+
const zPlaceholderReplacer = content => {
6266
content?.replace( /\#\{\s*\w+\s*\}/gs, _zReplacer ) // #{ key }
6367
.replace( /\/\*\!\s*\w+\s*\*\//gs, _zReplacer ) // map /*! mapKey */
6468
.replace( /\[\s*\/\*\s*\w+\s*\*\/\s*\]/gs, _zReplacer ) // map [/* mapKey */]
6569
.replace( /\{\s*\/\*\s*\w+\s*\*\/\s*\}/gs, _zReplacer ); // map {/* mapKey */}
70+
}
6671

6772
global.zPlaceholderReplacer = zPlaceholderReplacer;
6873

@@ -101,7 +106,7 @@ function createBuilder( entryPoints ) {
101106
bundle: true,
102107
outdir: '.',
103108
write: false,
104-
plugins: [svelteJsPathResolver, sveltePlugin( require( './svelte.config' ) )],
109+
plugins: [svelteJsPathResolver, sveltePlugin( svelteConfig ) ],
105110
incremental: !!watch,
106111
sourcemap: false,
107112
minify,
@@ -129,7 +134,7 @@ function layoutFor( path, content = {} ) {
129134
const m = statSync( path ).mtimeMs;
130135
if( cache && m===cache.m ) return cache;
131136

132-
const tree = parse5.parse(
137+
const tree = parse(
133138
path
134139
? readFileSync( path, 'utf-8' )
135140
: `<!DOCTYPE html>
@@ -260,7 +265,7 @@ function layoutFor( path, content = {} ) {
260265
let html = content.html || '';
261266
const innerCss = (content.css || {}).code || '';
262267

263-
return parse5.serialize( tree ).
268+
return serialize( tree ).
264269
replace( cssKEY, css + innerCss ).
265270
replace( jsKEY, js ).
266271
replace( appKEY, html ).
@@ -377,11 +382,11 @@ function layoutFor( path, content = {} ) {
377382
watcherReady = true;
378383
} )
379384
.on( 'error', err => console.log( 'ERROR:', err ) );
380-
}
381-
382-
const FiveServer = require( 'five-server' ).default;
385+
};
386+
387+
383388
serve &&
384-
(await new FiveServer().start( {
389+
(await new FiveServer().start( {
385390
open: true,
386391
workspace: __dirname,
387392
ignore: [...ignorePath, /\.(js|ts|svelte)$/, /\_layout\.html$/],

jsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "bundler",
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
"verbatimModuleSyntax": true,
7+
"isolatedModules": true,
8+
"resolveJsonModule": true,
9+
"sourceMap": true,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true,
12+
"checkJs": true,
13+
"baseUrl": ".",
14+
"paths": {
15+
"@/*": ["*"]
16+
},
17+
},
18+
"include": ["./**/*.js", "./**/*.svelte"]
19+
}

package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"name": "svelte-mpa-app",
33
"version": "0.0.0",
4+
"type": "module",
45
"scripts": {
56
"start": "npm run dev",
6-
"watch": "npm run build -- --watch",
7+
"watch": "node build.js --watch",
78
"dev": "node build.js --watch --serve",
89
"build": "node build.js",
910
"build:prod": "node build.js --minify",
1011
"serve": "node build.js --serve"
1112
},
1213
"devDependencies": {
13-
"chokidar": "^3.5.3",
14+
"chokidar": "^3.6.0",
1415
"esbuild": "^0.16.17",
1516
"esbuild-svelte": "^0.7.3",
16-
"five-server": "^0.1.9",
1717
"lodash": "^4.17.21",
1818
"node-notifier": "^10.0.1",
1919
"parse5": "^7.1.2",
@@ -27,5 +27,9 @@
2727
"trailingComma": "es5",
2828
"printWidth": 120,
2929
"arrowParens": "avoid"
30+
},
31+
"dependencies": {
32+
"five-server": "^0.3.2",
33+
"lodash-es": "^4.17.21"
3034
}
3135
}

0 commit comments

Comments
 (0)