Skip to content

Commit

Permalink
Merge pull request #44 from embroider-build/non-monorepo
Browse files Browse the repository at this point in the history
Standalone content-tag implemented via conditional exports in package.json
  • Loading branch information
ef4 authored Dec 12, 2023
2 parents a396fcf + b6b06b4 commit 3b659f6
Show file tree
Hide file tree
Showing 18 changed files with 2,600 additions and 87 deletions.
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
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ 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
node-version: 18
cache: 'npm'
- run: npm ci
- run: ./build.sh
- run: npm test
- run: npm run lint:package
- run: npm run lint:published-types
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ 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
registry-url: https://registry.npmjs.org
- run: npm publish
working-directory: pkg
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
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`

## 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
48 changes: 48 additions & 0 deletions index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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;
};
}

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;
}

module.exports = { Preprocessor };
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

0 comments on commit 3b659f6

Please sign in to comment.