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

Standalone content-tag implemented via conditional exports in package.json #44

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
pkg/node/
index.d.ts
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ jobs:
run: cargo test --verbose
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack build --target nodejs
- name: Install wasm-opt
run: cargo install wasm-opt
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
- run: npm ci
- run: ./build.sh
Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice to have this consistent with release workflow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the release workflow already run npm run build, so this is covered

- run: npm test
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# remove scope or update it once we decide exactly where we want to deploy this thing
- run: wasm-pack build --target nodejs
- run: npm run build
- uses: actions/setup-node@v3
with:
node-version: 20
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target
/node_modules
/node_modules

pkg/node/
pkg/standalone/
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Tests:

`cargo test`:

Build wasm package:
Build the package:

`wasm-pack build --target=nodejs`
`npm run build`

which will output your wasm package in `./pkg`
which will output your wasm package in `./pkg`, which is its own workspace, used for local testing as well as the publishable assets.

## Running against a local copy of SWC

Expand All @@ -36,4 +36,4 @@ and replace:

```
$1 = { path = "../swc/crates/$1"
```
```
2 changes: 1 addition & 1 deletion Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
lto = true
opt-level = 'z'
codegen-units = 1

[dependencies]
swc_common = { git = "https://github.com/ef4/swc.git", branch = "content-tag", features=["tty-emitter"] }
swc = { git = "https://github.com/ef4/swc.git", branch = "content-tag" }
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@ npm install content-tag

## Usage

```js
### Node (CommonJS)

```js
let { Preprocessor } = require('content-tag');
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```


### Node (ESM)

```js
import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```

### Browser (ESM)

```js
import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```

Expand Down
17 changes: 17 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# we need npm packages for the post-wasm phase
npm install

rm -rf pkg/node
rm -rf pkg/standalone

# wasm-pack knows to use wasm-opt, when present
# NOTE: wasm-pack does not support multi-target building
# so we'll build twice, and then tweak package.json
# "exports" to point at the correct build depending on node or browser
wasm-pack build --target web --out-dir pkg/standalone --weak-refs --no-pack --release
wasm-pack build --target nodejs --out-dir pkg/node --weak-refs --no-pack --release

# Rename the node js file to cjs, because we emit type=module
mv pkg/node/content_tag.js pkg/node/content_tag.cjs
49 changes: 49 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* wasm-pack doesn't give us correct enough types.
*/



interface Parsed {
type: 'expression' | 'class-member';
tagName: 'template';
contents: string;
range: {
start: number;
end: number;
};
contentRange: {
start: number;
end: number;
};
startRange: {
end: number;
start: number;
};
endRange: {
start: number;
end: number;
};
}


/**
*/
export class Preprocessor {
free(): void;
/**
*/
constructor();
/**
* @param {string} src
* @param {string | undefined} filename
* @returns {string}
*/
process(src: string, filename?: string): string;
/**
* @param {string} src
* @param {string | undefined} filename
* @returns {any}
*/
parse(src: string, filename?: string): Parsed;
}
21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>

<!-- View this file with `npm run web` -->

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script type="module">
import {Preprocessor} from 'content-tag';

const p = new Preprocessor();
const output = p.process('<template>Hi from createPreprocessor</template>');

document.querySelector('#output').innerHTML = output;
</script>
</head>

<body>
<pre id="output"></pre>
</body>

</html>
Loading
Loading