Browse Source

add typescript compilation mechanism

master
Conduitry 1 year ago
parent
commit
3a38be169d
6 changed files with 60 additions and 15 deletions
  1. +0
    -7
      .eslintrc.yaml
  2. +0
    -0
      .prettierrc.yaml
  3. +9
    -6
      package.json
  4. +40
    -2
      rollup.config.js
  5. +0
    -0
      src/CheapWatch.ts
  6. +11
    -0
      tsconfig.json

+ 0
- 7
.eslintrc.yaml View File

@ -1,7 +0,0 @@
env:
es6: true
node: true
extends: 'eslint:recommended'
parserOptions:
ecmaVersion: 2017
sourceType: module

.prettierrc → .prettierrc.yaml View File


+ 9
- 6
package.json View File

@ -10,9 +10,8 @@
"watcher"
],
"main": "./dist/CheapWatch.cjs.js",
"module": "./CheapWatch.js",
"module": "./dist/CheapWatch.es.js",
"files": [
"CheapWatch.js",
"dist"
],
"engines": {
@ -29,12 +28,16 @@
},
"homepage": "https://conduitry.io/cheap-watch",
"devDependencies": {
"rollup": "*"
"rollup": "*",
"typescript": "*",
"@types/node": "*"
},
"scripts": {
"build": "rollup -c",
"build": "tsc && rollup -c",
"dev": "rollup -cw",
"prepare": "npm run build",
"prepublishOnly": "npm test",
"pretest": "npm run build",
"test": "node test.js",
"prepublishOnly": "npm test"
"test": "node test.js"
}
}

+ 40
- 2
rollup.config.js View File

@ -1,6 +1,43 @@
let transform;
if (process.env.ROLLUP_WATCH === 'true') {
const { transpileModule } = require('typescript');
const tsconfig = require('./tsconfig.json');
transform = (code, id) => {
if (id.endsWith('.ts')) {
const { outputText, sourceMapText } = transpileModule(code, tsconfig);
return { code: outputText, map: JSON.parse(sourceMapText) };
}
};
} else {
const { readFile, unlink } = require('fs');
const { promisify } = require('util');
transform = async (code, id) => {
if (id.endsWith('.ts')) {
id = id.slice(0, -2) + 'js';
const [js, map] = await Promise.all(
[id, id + '.map'].map(async path => {
const data = await promisify(readFile)(path);
unlink(path, () => null);
return data.toString();
}),
);
return { code: js, map: JSON.parse(map) };
}
};
}
export default {
input: './CheapWatch.js',
external: name => /^[-a-z]+$/.test(name),
input: './src/CheapWatch',
external: name => /^[a-z]/.test(name),
plugins: {
resolveId(importee, importer) {
if (/\/[^.]+$/.test(importee)) {
return this.resolveId(importee + '.ts', importer);
}
},
transform,
},
output: [
{
file: './dist/CheapWatch.cjs.js',
@ -8,5 +45,6 @@ export default {
sourcemap: true,
interop: false,
},
{ file: './dist/CheapWatch.es.js', format: 'es', sourcemap: true },
],
};

CheapWatch.js → src/CheapWatch.ts View File


+ 11
- 0
tsconfig.json View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"removeComments": true,
"sourceMap": true,
"target": "esnext"
},
"include": ["src"]
}

Loading…
Cancel
Save