diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 1b3ace9..8f7d65c 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,5 +12,5 @@ jobs: name: Node.js uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: - os: 'ubuntu-latest' + os: 'ubuntu-latest, windows-latest' version: '16, 18, 20, 22' diff --git a/package.json b/package.json index 2c41b28..7ec2d67 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@types/node": "^20.6.2", "coffee": "^5.5.0", "egg-bin": "^6.5.2", - "tshy": "^1.0.0" + "tshy": "^2.0.0" }, "type": "module", "tshy": { @@ -43,12 +43,10 @@ "./package.json": "./package.json", ".": { "import": { - "source": "./src/index.ts", "types": "./dist/esm/index.d.ts", "default": "./dist/esm/index.js" }, "require": { - "source": "./src/index.ts", "types": "./dist/commonjs/index.d.ts", "default": "./dist/commonjs/index.js" } diff --git a/src/index.ts b/src/index.ts index 0081460..0161bf9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,19 @@ #!/usr/bin/env node -import { writeFileSync, readFileSync, readdirSync, statSync } from 'node:fs' +import { writeFileSync, readFileSync, readdirSync, statSync, copyFileSync } from 'node:fs' import path from 'node:path' -const cwd = process.cwd(); +const cwd = process.cwd() + path.sep; const pkg = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf-8')); // make sure commonjs *.d.ts can import types pkg.types = pkg.exports['.'].require.types; writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n') +writeFileSync('dist/package.json', JSON.stringify({ + name: pkg.name, + version: pkg.version, +}, null, 2) + '\n') // Replace all `( import.meta.url )` into `('import_meta_url_placeholder_by_tshy_after')` on commonjs. const IMPORT_META_URL = '(' + 'import.meta.url' + ')'; @@ -32,7 +36,39 @@ function replaceImportMetaUrl(baseDir: string) { continue; } writeFileSync(filepath, content.replaceAll(IMPORT_META_URL, IMPORT_META_URL_PLACE_HOLDER)); - console.log('Auto fix "import.meta.url" on %s', filepath); + console.log('Auto fix "import.meta.url" on %s', filepath.replace(cwd, '')); } } replaceImportMetaUrl(path.join(cwd, 'dist/commonjs')); + +// Copy image/json/web files +const fileExts = [ + '.jpg', '.jpeg', '.png', '.gif', '.webp', '.ico', + '.json', '.html', '.htm', '.css', +]; +const sourceDir = path.join(cwd, 'src'); +const commonjsDir = path.join(cwd, 'dist/commonjs'); +const esmDir = path.join(cwd, 'dist/esm'); + +function copyFiles(baseDir: string) { + const names = readdirSync(baseDir); + for (const name of names) { + const filepath = path.join(baseDir, name); + const stat = statSync(filepath); + if (stat.isDirectory()) { + copyFiles(filepath); + continue; + } + const extname = path.extname(filepath); + if (!fileExts.includes(extname)) { + continue; + } + let targetFilepath = filepath.replace(sourceDir, commonjsDir); + copyFileSync(filepath, targetFilepath); + console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, '')); + targetFilepath = filepath.replace(sourceDir, esmDir); + copyFileSync(filepath, targetFilepath); + console.log('Copy %s to %s', filepath.replace(cwd, ''), targetFilepath.replace(cwd, '')); + } +} +copyFiles(sourceDir); diff --git a/test/fixtures/demo/src/bar.json b/test/fixtures/demo/src/bar.json new file mode 100644 index 0000000..f2a886f --- /dev/null +++ b/test/fixtures/demo/src/bar.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/test/fixtures/demo/src/config/favicon.png b/test/fixtures/demo/src/config/favicon.png new file mode 100644 index 0000000..5b8a258 Binary files /dev/null and b/test/fixtures/demo/src/config/favicon.png differ diff --git a/test/fixtures/demo/src/config/foo.json b/test/fixtures/demo/src/config/foo.json new file mode 100644 index 0000000..f2a886f --- /dev/null +++ b/test/fixtures/demo/src/config/foo.json @@ -0,0 +1,3 @@ +{ + "hello": "world" +} diff --git a/test/fixtures/demo/src/tsconfig.json b/test/fixtures/demo/src/tsconfig.json deleted file mode 100644 index b02cfc2..0000000 --- a/test/fixtures/demo/src/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "compilerOptions": { - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "inlineSources": true, - "jsx": "react", - "module": "nodenext", - "moduleResolution": "nodenext", - "noUncheckedIndexedAccess": true, - "resolveJsonModule": true, - "skipLibCheck": false, - "sourceMap": true, - "strict": true, - "target": "es2022" - } -} diff --git a/test/fixtures/demo/tsconfig.json b/test/fixtures/demo/tsconfig.json index 7f39495..b02cfc2 100644 --- a/test/fixtures/demo/tsconfig.json +++ b/test/fixtures/demo/tsconfig.json @@ -1,7 +1,5 @@ { "compilerOptions": { - "declaration": true, - "declarationMap": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "inlineSources": true, @@ -10,7 +8,7 @@ "moduleResolution": "nodenext", "noUncheckedIndexedAccess": true, "resolveJsonModule": true, - "skipLibCheck": true, + "skipLibCheck": false, "sourceMap": true, "strict": true, "target": "es2022" diff --git a/test/index.test.ts b/test/index.test.ts index c156b3d..8c3a6e3 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -37,5 +37,25 @@ describe('test/index.test.ts', () => { .end(); const pkg = JSON.parse(fs.readFileSync(packageFile, 'utf-8')); assert.equal(pkg.types, './dist/commonjs/index.d.ts'); + + // should copy image/json files + let pngFile = path.join(cwd, 'dist/commonjs/config/favicon.png'); + assert.equal(fs.statSync(pngFile).isFile(), true); + pngFile = path.join(cwd, 'dist/esm/config/favicon.png'); + assert.equal(fs.statSync(pngFile).isFile(), true); + let jsonFile = path.join(cwd, 'dist/commonjs/config/foo.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/esm/config/foo.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/commonjs/bar.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + jsonFile = path.join(cwd, 'dist/esm/bar.json'); + assert.equal(fs.statSync(jsonFile).isFile(), true); + + // should dist/package.json exists, include name and version + const distPackageFile = path.join(cwd, 'dist/package.json'); + const distPkg = JSON.parse(fs.readFileSync(distPackageFile, 'utf-8')); + assert.equal(distPkg.name, 'tshy-after'); + assert.equal(distPkg.version, '1.0.0'); }); });