Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernise #191

Merged
merged 26 commits into from
Jun 12, 2024
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
24 changes: 0 additions & 24 deletions .eslintrc

This file was deleted.

53 changes: 53 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: CI
on:
push:
branches: [master]
pull_request:
permissions:
contents: read # to fetch code (actions/checkout)

jobs:
Tests:
runs-on: ${{ matrix.os }}
timeout-minutes: 15
strategy:
matrix:
include:
- node-version: 18
os: windows-latest
- node-version: 18
os: macOS-latest
- node-version: 18
os: ubuntu-latest
- node-version: 20
os: ubuntu-latest
- node-version: 22
os: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm test
env:
CI: true
Lint:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 18
cache: pnpm
- name: install
run: pnpm install --frozen-lockfile
- name: type check
run: pnpm build
- name: lint
run: pnpm lint
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/test/cli/*/actual
/test/cli/*/expected
/test/cli/*/files
/test/samples
14 changes: 14 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"useTabs": true,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"overrides": [
{
"files": "*.md",
"options": {
"useTabs": false
}
}
]
}
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

Sourcemaps are great - if you have a JavaScript file, and you minify it, your minifier can generate a map that lets you debug as though you were looking at the original uncompressed code.

But if you have more than one transformation - say you want to transpile your JavaScript, concatenate several files into one, and minify the result - it gets a little trickier. Each intermediate step needs to be able to both *ingest* a sourcemap and *generate* one, all the time pointing back to the original source.
But if you have more than one transformation - say you want to transpile your JavaScript, concatenate several files into one, and minify the result - it gets a little trickier. Each intermediate step needs to be able to both _ingest_ a sourcemap and _generate_ one, all the time pointing back to the original source.

Most compilers don't do that. ([UglifyJS](https://github.com/mishoo/UglifyJS2) is an honourable exception.) So when you fire up devtools, instead of looking at the original source you find yourself looking at the final intermediate step in the chain of transformations.

**Sorcery aims to fix that.** Given a file at the end of a transformation chain (e.g., your minified JavaScript), it will follow the entire chain back to the original source, and generate a new sourcemap that describes the whole process. How? Magic.

This is a work-in-progress - suitable for playing around with, but don't rely on it to debug air traffic control software or medical equipment. Other than that, it can't do much harm.


## Usage

### As a node module
Expand All @@ -22,9 +21,9 @@ npm install sorcery
```

```js
var sorcery = require( 'sorcery' );
import * as sorcery from 'sorcery';

sorcery.load( 'some/generated/code.min.js' ).then( function ( chain ) {
sorcery.load('some/generated/code.min.js').then(function (chain) {
// generate a flattened sourcemap
var map = chain.apply(); // { version: 3, file: 'code.min.js', ... }

Expand All @@ -37,14 +36,14 @@ sorcery.load( 'some/generated/code.min.js' ).then( function ( chain ) {
// write to a new file - this will create `output.js` and
// `output.js.map`, and will preserve relative paths. It
// returns a Promise
chain.write( 'output.js' );
chain.write('output.js');

// write to a new file but use an absolute path for the
// sourceMappingURL
chain.write( 'output.js', { absolutePath: true });
chain.write('output.js', { absolutePath: true });

// write to a new file, but append the flattened sourcemap as a data URI
chain.write( 'output.js', { inline: true });
chain.write('output.js', { inline: true });

// overwrite the existing file
chain.write();
Expand All @@ -54,22 +53,22 @@ sorcery.load( 'some/generated/code.min.js' ).then( function ( chain ) {
// `source`, `line`, `column` and (if applicable) `name` properties.
// Note - for consistency with other tools, line numbers are always
// one-based, column numbers are always zero-based. It's daft, I know.
var loc = chain.trace( x, y );
var loc = chain.trace(x, y);
});

// You can also use sorcery synchronously:
var chain = sorcery.loadSync( 'some/generated/code.min.js' );
var chain = sorcery.loadSync('some/generated/code.min.js');
var map = chain.apply();
var loc = chain.trace( x, y );
var loc = chain.trace(x, y);
chain.writeSync();
```

#### Advanced options

You can pass an optional second argument to `sorcery.load()` and `sorcery.loadSync()`, with zero or more of the following properties:

* `content` - a map of `filename: contents` pairs. `filename` will be resolved against the current working directory if needs be
* `sourcemaps` - a map of `filename: sourcemap` pairs, where `filename` is the name of the file the sourcemap is related to. This will override any `sourceMappingURL` comments in the file itself.
- `content` - a map of `filename: contents` pairs. `filename` will be resolved against the current working directory if needs be
- `sourcemaps` - a map of `filename: sourcemap` pairs, where `filename` is the name of the file the sourcemap is related to. This will override any `sourceMappingURL` comments in the file itself.

For example:

Expand Down Expand Up @@ -129,7 +128,6 @@ sorcery -d -i some/generated/code.min.js
sorcery -i some/generated/code.min.js -o newfile.js
```


## License

MIT
23 changes: 0 additions & 23 deletions appveyor.yml

This file was deleted.

27 changes: 12 additions & 15 deletions bin/help.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
Sorcery version <%= version %>
=====================================
# Sorcery version <%= version %>

Usage:
sorcery [options]
Usage:
sorcery [options]

Options:
-h, --help Show help message
-v, --version Show version
-i, --input <file|folder> Input file
-o, --output <file|folder> Output file (if absent, will overwrite input)
-d, --datauri Append map as a data URI, rather than separate file
-x, --excludeContent Don't populate the sourcesContent array
Options:
-h, --help Show help message
-v, --version Show version
-i, --input <file|folder> Input file
-o, --output <file|folder> Output file (if absent, will overwrite input)
-d, --datauri Append map as a data URI, rather than separate file
-x, --excludeContent Don't populate the sourcesContent array


Example:
Example:

sorcery --input some/generated/code.min.js
sorcery --input tmp --output dist


For more information visit https://github.com/Rich-Harris/sorcery
For more information visit https://github.com/Rich-Harris/sorcery
18 changes: 10 additions & 8 deletions bin/showHelp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var fs = require( 'fs' ),
path = require( 'path' );
import fs from 'node:fs';
import path from 'node:path';

module.exports = function ( stream ) {
fs.readFile( path.join( __dirname, 'help.md' ), function ( err, result ) {
export default function (stream) {
fs.readFile(path.join(__dirname, 'help.md'), function (err, result) {
var help;

if ( err ) throw err;
if (err) throw err;

help = result.toString().replace( '<%= version %>', require( '../package.json' ).version );
( stream || process.stderr ).write( '\n' + help + '\n' );
help = result
.toString()
.replace('<%= version %>', require('../package.json').version);
(stream || process.stderr).write('\n' + help + '\n');
});
};
}
88 changes: 39 additions & 49 deletions bin/sorcery
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env node

var path = require( 'path' );
var minimist = require( 'minimist' );
var sander = require( 'sander' );
var showHelp = require( './showHelp' );
import fs from 'node:fs';
import path from 'node:path';
import minimist from 'minimist';
import glob from 'tiny-glob/sync.js';
import showHelp from './showHelp.js';
import * as sorcery from '../src/index.js';

var command;
var sorcery = require( '../' );

var validExtensions = { js: true };

command = minimist( process.argv.slice( 2 ), {
command = minimist(process.argv.slice(2), {
alias: {
i: 'input',
o: 'output',
Expand All @@ -20,55 +22,43 @@ command = minimist( process.argv.slice( 2 ), {
}
});

if ( command.help ) {
showHelp( process.stdout );
}

else if ( process.argv.length <= 2 && process.stdin.isTTY ) {
showHelp( process.stderr );
}

else if ( command.version ) {
console.log( 'Sorcery version ' + require( '../package.json' ).version );
}

else if ( !command.input ) {
console.error( 'Error: You must supply an --input (-i) argument. Type sorcery --help for more info' );
}
if (command.help) {
showHelp(process.stdout);
} else if (process.argv.length <= 2 && process.stdin.isTTY) {
showHelp(process.stderr);
} else if (command.version) {
console.log('Sorcery version ' + require('../package.json').version);
} else if (!command.input) {
console.error(
'Error: You must supply an --input (-i) argument. Type sorcery --help for more info'
);
} else {
const stats = fs.statSync(command.input);

else {
sander.stat( command.input ).then( function ( stats ) {
if ( stats.isDirectory() ) {
return sander.lsr( command.input ).then( function ( files ) {
files = files.filter( function ( file ) {
return validExtensions[ path.extname( file ).slice( 1 ) ];
});
if (stats.isDirectory()) {
const files = glob('**', { cwd: command.input, filesOnly: true }).filter(
function (file) {
return validExtensions[path.extname(file).slice(1)];
}
);

return files.reduce( function ( promise, file ) {
return promise.then( function () {
var input = path.join( command.input, file );
var output = path.join( command.output || command.input, file );
for (const file of files) {
var input = path.join(command.input, file);
var output = path.join(command.output || command.input, file);

return sorcery.load( input ).then( function ( chain ) {
return chain.write( output, {
inline: command.datauri,
includeContent: !command.excludeContent
});
});
});
}, Promise.resolve() );
});
}
var chain = await sorcery.load(input);

return sorcery.load( command.input ).then( function ( chain ) {
return chain.write( command.output || command.input, {
await chain.write(output, {
inline: command.datauri,
includeContent: !command.excludeContent
});
}
} else {
const chain = await sorcery.load(command.input);

await chain.write(command.output || command.input, {
inline: command.datauri,
includeContent: !command.excludeContent
});
}).catch( function ( err ) {
setTimeout( function () {
throw err;
});
});
}
}
Loading