8000 feat(default-compiler): features for the Default Compiler by lamnhan · Pull Request #15 · tinijs/tinijs · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(default-compiler): features for the Default Compiler #15

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

Merged
merged 3 commits into from
Apr 29, 2024
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
18 changes: 18 additions & 0 deletions apps/tinijs.dev/app/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ export class AppPageModule extends TiniComponent {
return html`
<h1>Tini Modules</h1>
<p>Installable modules to extend functionalities.</p>

<h2>Official modules</h2>
<ul>
<li>
<a href="/module/content">Content</a> - file-based content management
system
</li>
<li><a href="/module/pwa">PWA</a> - turn a TiniJS app into a PWA</li>
</ul>

<h2>Community/local modules</h2>
<ul>
<li>Add your shared module here</li>
</ul>
<p>
Please see the <a href="/module/author-guide">Author Guide</a> for how
to create a module.
</p>
`;
}

Expand Down
5 changes: 4 additions & 1 deletion apps/tinijs.dev/content/cli-posts/102 - expansion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default defineTiniConfig({
'some-name-2'
],

// disable autoload official and local expansions, default: false (auto load)
// disable autoload official and local expansions, default: false (auto load all available)
// undefined or false: auto load all available
// true: disable all auto load
// string[]: disable specific ones, example: ['local', '@tinijs/content']
noAutoExpansions: true,
},

Expand Down
114 changes: 113 additions & 1 deletion apps/tinijs.dev/content/framework-posts/104 - compile/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,116 @@
}
+++

**TODO**: implements Default Compiler features and add instructions.
Compile is an optional but recommended step in the dev/build workflow for TiniJS apps. You can think of it as a transformation step that takes the source code and transforms it before forwarding the code to the build tool.

With the **Default Compiler**, you can use these features:
- Support `SCSS` in `` css`...` ``
- Minify the content `` html`...` ``
- Inject environment variables to `configs/<env>.ts` and choose a config file based on the target environment
- _More in the future ..._

You can config the compile step behaviors in the `tini.config.ts` file.

```js
export default defineTiniConfig({

// compile step is enabled by default
compile: {

// default to the Default Compiler (@tinijs/default-compiler)
// you can use your own compiler
compiler: 'a-compiler',

// you can set options for the Default Compiler
options: {
// ignore certain files, please see: https://github.com/micromatch/picomatch
ignorePatterns?: string[],
// compile html``, default is enable, set false to disable
compileTaggedHTML?: false | {
minify?: false; // minify the template literals or not (default is minified)
},
// compile css``, default is enable, set false to disable
// or set options for SASS: https://sass-lang.com/documentation/js-api/interfaces/stringoptions/
compileTaggedCSS?: false | SASSOptions,
}
},

// or, disable the compile step
compile: false,

});
```

## Compile HTML

Look for `` return html` `` and `` `; `` in your source code, the content inside the template literals will be compiled.

**Limitations**, nested `` html`...` `` will be kept as is, split the nested templates into smaller templates instead:

```ts

class XXX {

helloTemplate() {
return html`<h1>Hello World</h1>`;
}

render() {
return html`
<div>${this.helloTemplate()}</div>
`;
}
}
```

## Compile CSS

Look for `` css` `` and `` `; ``/`` `, `` in your source code, the content inside the template literals will be compiled.

**Limitations**, use `${}` inside the templates will break SASS compilation, add `/* no-sass */` to skip.

```ts
const style1 = css`
// scss code
// will be compiled
`;

const style2 = `
// scss code
// will not be compiled
`;

class XXX {

static styles = css`
/* no-sass */

// scss code
// will not be compiled

${style1}
${unsafeCSS(style2)}
`;

}
```

## Client app configs

Put `configs/<env>.ts` files in the `app` folder, the environment variables will be injected into the files.

**IMPORTANT**: all the config in `configs` folder are for the client app only, don't expose sensitive information.

```ts
export const config: AppConfig = {
foo: 'bar',
baz: process.env.BAZ,
qux: process.env.QUX,
};
```

During the `npm run dev` the file `configs/development.ts` will be used, during the `npm run build` the file `configs/production.ts` or `configs/<env>.ts` (flag `--target <env>`) will be used.

```bash
npx tini build --target qa|staging|xxx
```
88 changes: 55 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/cli/cli/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export const buildCommand = createCLICommand(
},
},
async (args, callbacks) => {
const targetEnv = args.target || 'production';
const tiniProject = await getTiniProject();
const {config: tiniConfig, hooks} = tiniProject;
// preparation
exposeEnvs(tiniConfig, targetEnv);
exposeEnvs(tiniConfig, args.target || 'production');
const compiler = await loadCompiler(tiniProject);
const builder = await loadBuilder(tiniProject);
// clean
Expand Down
44 changes: 35 additions & 9 deletions packages/cli/cli/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import {resolve} from 'pathe';
import {consola} from 'consola';
import {green} from 'colorette';
import {remove} from 'fs-extra/esm';
import picomatch from 'picomatch';

import {getTiniProject} from '@tinijs/project';
import {getTiniProject, getProjectDirs} from '@tinijs/project';

import {loadCompiler} from '../utils/build.js';
import {
exposeEnvs,
loadCompiler,
extractCompileOptions,
parseCompileFileContext,
} from '../utils/build.js';
import {createCLICommand} from '../utils/cli.js';

export const compileCommand = createCLICommand(
Expand All @@ -21,22 +27,33 @@ export const compileCommand = createCLICommand(
type: 'boolean',
description: 'Also watch for changes.',
},
target: {
alias: 't',
type: 'string',
description: 'Target: development (default), production, ...',
},
},
},
async (args, callbacks) => {
const tiniProject = await getTiniProject();
const {config: tiniConfig} = tiniProject;
const {srcDir, compileDir} = tiniConfig;
const {config: tiniConfig, hooks} = tiniProject;
const projectDirs = getProjectDirs(tiniConfig);
const {srcDir, compileDir} = projectDirs;
const compileOptions = extractCompileOptions<{
ignorePatterns?: string[];
}>(tiniConfig.compile);
// preparation
exposeEnvs(tiniConfig, args.target || 'development');
const compiler = await loadCompiler(tiniProject, true);
// compile
if (!compiler) {
callbacks?.onUnavailable();
} else {
await compiler.compile();
if (!args.watch) {
callbacks?.onSingleCompile(compileDir);
} else {
const srcDirPath = resolve(srcDir);
watch(srcDirPath, {ignoreInitial: true})
watch(resolve(srcDir), {ignoreInitial: true})
.on('add', path => {
callbacks?.onCompileFile('add', path);
compiler.compileFile(resolve(path));
Expand All @@ -45,11 +62,20 @@ export const compileCommand = createCLICommand(
callbacks?.onCompileFile('change', path);
compiler.compileFile(resolve(path));
})
.on('unlink', path => {
.on('unlink', async path => {
callbacks?.onCompileFile('unlink', path);
remove(
resolve(compileDir, resolve(path).replace(`${srcDirPath}/`, ''))
const ignoreMatcher = !compileOptions.ignorePatterns
? undefined
: picomatch(compileOptions.ignorePatterns);
const context = await parseCompileFileContext(
path,
projectDirs,
ignoreMatcher
);
if (!context) return;
await hooks.callHook('compile:beforeRemoveFile', context);
await remove(context.outPath);
await hooks.callHook('compile:afterRemoveFile', context);
});
callbacks?.onCompileAndWatch(compileDir);
}
Expand Down
Loading
0