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

feat: support dsd #45

Merged
merged 5 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 53 additions & 37 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,65 @@ import { readdir, readFile } from 'node:fs/promises';
import { build } from 'esbuild';
bennypowers marked this conversation as resolved.
Show resolved Hide resolved
import { fileURLToPath } from 'node:url';
import { resolve, dirname } from 'node:path';
import { importAssertPlugin } from 'esbuild-plugin-import-assertions';

const r = path => fileURLToPath(new URL(path, import.meta.url));

export async function run(watch = process.argv.includes('start')) {
const files = await readdir(r('./src/'));
const LICENSE = await readFile(r('./LICENSE'), 'utf8')
for (const file of await readdir(r('./src/'))) {
if (file.endsWith('.js')) {
await build({
entryPoints: [r(`./src/${file}`)],
outdir: '.',
banner: { js: `/**\n * @license\n${LICENSE}\n*/`, },
format: 'esm',
sourcemap: true,
bundle: true,
watch,
loader: {
'.css': 'js',
'.html': 'js',
},
plugins: [
importAssertPlugin,
{
name: 'html-template',
setup(api) {
api.onResolve({ filter: /\.html$/ }, args => ({
path: resolve(dirname(args.importer), args.path),
namespace: 'html-template',
}));
await build({
entryPoints: files
.filter(file => file.endsWith('.js'))
.map(file => r(`./src/${file}`)),
outdir: '.',
banner: { js: `/**\n * @license\n${LICENSE}\n*/`, },
format: 'esm',
bundle: true,
sourcemap: true,
target: [
'es2022',
],
loader: {
'.css': 'js',
'.html': 'js',
},
plugins: [
{
name: 'html-template',
setup(api) {
api.onResolve({ filter: /\.html$/ }, args => ({
path: resolve(dirname(args.importer), args.path),
namespace: 'html-template',
}));

api.onLoad({ filter: /\.html$/, namespace: 'html-template' }, async args => ({
contents: `\
const template = document.createElement('template');
template.innerHTML = \`${await readFile(args.path, 'utf8')}\`;
export default template;`,
loader: 'ts',
}));
}
}
]
})
}
}
api.onLoad({ filter: /\.html$/, namespace: 'html-template' }, async args => ({
contents: `\
const template = document.createElement('template');
template.innerHTML = \`${await readFile(args.path, 'utf8')}\`;
export default template;`,
loader: 'ts',
}));
}
},
{
name: 'css-constructible',
setup(api) {
api.onResolve({ filter: /\.css$/ }, args => ({
path: resolve(dirname(args.importer), args.path),
namespace: 'css-constructible',
}));

api.onLoad({ filter: /\.css$/, namespace: 'css-constructible' }, async args => ({
contents: `\
const sheet = new CSSStyleSheet();
sheet.replaceSync(/*css*/\`${await readFile(args.path, 'utf8')}\`);
export default sheet;`,
loader: 'ts',
}));
}
}
]
}).catch(() => process.exit(1));
}


Expand Down
Loading