Skip to content
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
35 changes: 35 additions & 0 deletions .github/workflows/ release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: automatic release
on:
push:
branches:
- master
tags:
- "!*"
jobs:
release:
name: check version and tagging
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: Setup Node.js environment
uses: actions/setup-node@v2
with:
node-version: 13.x
registry-url: "https://registry.npmjs.org"
- name: install can-npm-publish and dependencies
run: |
npm install -D can-npm-publish
npm ci
- name: check version and add tag
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
REPO: ${{github.repository}}
COMMIT: ${{github.sha}}
run: ./tools/release.sh
- name: build
run: npm run build
- name: release
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-css-modules-vite-plugin",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const formatClassNames = (classNameKeys: Map<string, boolean>) => {
let exportTypes = "",
exportClassNames = "export type ClassNames = ";
const exportStyle = "export default classNames;";
for (const classNameKey of classNameKeys.keys()) {
exportTypes = `${exportTypes}\n${formatExportType(classNameKey)}`;
exportClassNames =
exportClassNames !== "export type ClassNames = "
? `${exportClassNames} | '${classNameKey}'`
: `${exportClassNames} '${classNameKey}'`;
}

return `declare const classNames: {${exportTypes}\n};\n${exportStyle}\n${exportClassNames}`;
};
const formatExportType = (key: string) => ` readonly '${key}': '${key}';`;
44 changes: 28 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ResolvedConfig } from "vite";
import { getViteConfig } from "./config";
import { parseCss } from "./css";
import { extractClassNameKeys } from "./extract";
import { formatClassNames } from "./format";
import { isCSSFile } from "./util";

const factory: ts.server.PluginModuleFactory = (mod: {
Expand All @@ -14,11 +15,12 @@ const factory: ts.server.PluginModuleFactory = (mod: {
const create = (info: ts.server.PluginCreateInfo): ts.LanguageService => {
// resolve vite.config.ts
const config: ResolvedConfig | undefined = getViteConfig(__dirname);

const ls = info.languageService;
const lsh = info.languageServiceHost;

const formatExportType = (key: string) => ` readonly '${key}': '${key}';`;
if (!config) {
return ls;
}

// オリジナルのメソッドを退避しておく
const delegate = {
Expand Down Expand Up @@ -76,20 +78,9 @@ const factory: ts.server.PluginModuleFactory = (mod: {
postcssJs.objectify(postcss.parse(css))
);

let exportTypes = "",
exportClassNames = "export type ClassNames = ";
const exportStyle = "export default classNames;";
for (const classNameKey of classNameKeys.keys()) {
exportTypes = `${exportTypes}\n${formatExportType(classNameKey)}`;
exportClassNames =
exportClassNames !== "export type ClassNames = "
? `${exportClassNames} | '${classNameKey}'`
: `${exportClassNames} '${classNameKey}'`;
}

let outputFileString = "";
outputFileString = `declare const classNames: {${exportTypes}\n};\n${exportStyle}\n${exportClassNames}`;
scriptSnapshot = ts.ScriptSnapshot.fromString(outputFileString);
scriptSnapshot = ts.ScriptSnapshot.fromString(
formatClassNames(classNameKeys)
);
}
}
return delegate.createLanguageServiceSourceFile(
Expand All @@ -109,6 +100,27 @@ const factory: ts.server.PluginModuleFactory = (mod: {
textChangeRange,
aggressiveChecks
): ts.SourceFile => {
const fileName = sourceFile.fileName;
if (isCSSFile(fileName)) {
if (config) {
let css = scriptSnapshot.getText(0, scriptSnapshot.getLength());
if (fileName.endsWith(".css")) {
} else {
try {
css = parseCss(css, fileName, config);
} catch (e) {
log(`${e}`);
}
}
const classNameKeys = extractClassNameKeys(
postcssJs.objectify(postcss.parse(css))
);

scriptSnapshot = ts.ScriptSnapshot.fromString(
formatClassNames(classNameKeys)
);
}
}
return delegate.updateLanguageServiceSourceFile(
sourceFile,
scriptSnapshot,
Expand Down
23 changes: 23 additions & 0 deletions tools/ release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# check this version is enable to release or not
npx can-npm-publish
if [ $? -eq 1 ] ; then
exit 255
fi

# get current version from package.json
TAG=$(cat package.json | grep version | cut -d " " -f 4 | tr -d "," | tr -d '"')
echo "add new tag to GitHub: ${TAG}"

# Add tag to GitHub
API_URL="https://api.github.com/repos/${REPO}/git/refs"

curl -s -X POST $API_URL \
-H "Authorization: token $GITHUB_TOKEN" \
-d @- << EOS
{
"ref": "refs/tags/v${TAG}",
"sha": "${COMMIT}"
}
EOS