Skip to content

Commit

Permalink
Update swc/core, remove some deps
Browse files Browse the repository at this point in the history
  • Loading branch information
konclave committed Sep 1, 2024
1 parent ee20b02 commit c4e8efe
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 89 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Publish
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+*'

jobs:
npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 22
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish Release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: gh release edit "${{ needs.release.outputs.tag }}" --draft=false
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## v2.2.0 - 2024-09-01

- Updated swc-core dependency to v1.7.20;
- Replaced some external dependencies with the internal implementation.
69 changes: 69 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const path = require('path');
const SourceMapGenerator = require('source-map').SourceMapGenerator;
const SourceMapConsumer = require('source-map').SourceMapConsumer;

function startsWithSingleDot(fpath) {
const first2chars = fpath.slice(0, 2);
return first2chars === '.' + path.sep || first2chars === './';
}

function replace(npath, ext) {
if (typeof npath !== 'string' || npath.length === 0) {
return npath;
}

const nFileName = path.basename(npath, path.extname(npath)) + ext;
const nFilepath = path.join(path.dirname(npath), nFileName);

if (startsWithSingleDot(npath)) {
return '.' + path.sep + nFilepath;
}
return nFilepath;
}

function replaceExtension(fp) {
return path.extname(fp) ? replace(fp, '.js') : fp;
}

function applySourceMap(file, sourceMap) {
if (typeof sourceMap === 'string' || sourceMap instanceof String) {
sourceMap = JSON.parse(sourceMap);
}

if (file.sourceMap && (typeof file.sourceMap === 'string' || file.sourceMap instanceof String)) {
file.sourceMap = JSON.parse(file.sourceMap);
}

// check source map properties
assertProperty(sourceMap, "file");
assertProperty(sourceMap, "mappings");
assertProperty(sourceMap, "sources");

// fix paths if Windows style paths
sourceMap.file = sourceMap.file.replace(/\\/g, '/');
sourceMap.sources = sourceMap.sources.map(function(filePath) {
return filePath.replace(/\\/g, '/');
});

if (file.sourceMap && file.sourceMap.mappings !== '') {
const generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
generator.applySourceMap(new SourceMapConsumer(file.sourceMap));
file.sourceMap = JSON.parse(generator.toString());
} else {
file.sourceMap = sourceMap;
}
};


function assertProperty(sourceMap, propertyName) {
if (!sourceMap.hasOwnProperty(propertyName)) {
const e = new Error('Source map to be applied is missing the \"' + propertyName + '\" property');
throw e;
}
}


module.exports = {
replaceExtension,
applySourceMap
}
7 changes: 1 addition & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
const swc = require('@swc/core');
const path = require('path');
const replaceExt = require('replace-ext');
const PluginError = require('plugin-error');
const applySourceMap = require('vinyl-sourcemaps-apply');
const { Transform } = require('stream');

function replaceExtension(fp) {
return path.extname(fp) ? replaceExt(fp, '.js') : fp;
}
const { replaceExtension, applySourceMap } = require('./helpers');

module.exports = function (opts) {
opts = opts || {};
Expand Down
Loading

0 comments on commit c4e8efe

Please sign in to comment.