diff --git a/.github/workflows/ release.yml b/.github/workflows/ release.yml new file mode 100644 index 0000000..e2b3bd1 --- /dev/null +++ b/.github/workflows/ release.yml @@ -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 }} diff --git a/package-lock.json b/package-lock.json index 39eddfb..4be5f4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ts-css-modules-vite-plugin", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 0d11389..b5910e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-css-modules-vite-plugin", - "version": "1.0.0", + "version": "1.0.1", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/format.ts b/src/format.ts new file mode 100644 index 0000000..21b2230 --- /dev/null +++ b/src/format.ts @@ -0,0 +1,15 @@ +export const formatClassNames = (classNameKeys: Map) => { + 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}';`; diff --git a/src/index.ts b/src/index.ts index c87812f..2895a78 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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: { @@ -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 = { @@ -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( @@ -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, diff --git a/tools/ release.sh b/tools/ release.sh new file mode 100644 index 0000000..b03b687 --- /dev/null +++ b/tools/ release.sh @@ -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 \ No newline at end of file