Skip to content

Commit

Permalink
env: attempt to support build-self as ESM
Browse files Browse the repository at this point in the history
- need to use `createRequire` in ESM mode
  - deps: bump `@types/node` to support the types for that as well
  - this is a BREAKING CHANGE as it requires Node 14+
- fix `fs-extra` error due to it being CJS

- add `build-self/*` to `prebuild` clean
  - not to `build-self` script, as the `build-self/` directory has to exist for that
- output as `build-self/index.mjs` instead of `build-self/dist/rollup-plugin-typescript2.es.js`
  - so that types work out of the box -- due to `index.d.ts`
  - and so that Rollup correctly loads it as ESM and not CJS
  • Loading branch information
agilgur5 committed Jul 18, 2023
1 parent 70b358b commit 5c04ca1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Expand Up @@ -22,11 +22,11 @@
"homepage": "https://github.com/ezolenko/rollup-plugin-typescript2",
"author": "@ezolenko",
"scripts": {
"prebuild": "rimraf dist/*",
"build": "rollup -c rollup.config.ts --configPlugin \"typescript2={ tsconfigOverride: { compilerOptions: { resolveJsonModule: true } } }\"",
"_build-self": "rollup -c rollup.config.self.ts --configPlugin ./build-self/dist/rollup-plugin-typescript2.es.js",
"watch": "npm run _build-self -- -w ",
"prebuild": "rimraf dist/* build-self/*.ts build-self/*.*js",
"build": "rollup -c rollup.config.ts --configPlugin typescript2=\"{ tsconfigOverride: { compilerOptions: { resolveJsonModule: true } } }\"",
"_build-self": "rollup -c rollup.config.self.ts --configPlugin ./build-self/index.mjs=\"{ tsconfigOverride: { compilerOptions: { resolveJsonModule: true } } }\"",
"build-self": "rimraf dist/* && npm run _build-self",
"watch": "npm run _build-self -- -w ",
"lint": "tslint -c ./tslint.json src/*.ts __tests__/*.ts ./*.js",
"test": "jest",
"test:watch": "jest --watch",
Expand All @@ -53,7 +53,7 @@
"@types/graphlib": "2.1.7",
"@types/jest": "^27.5.0",
"@types/lodash": "4.14.161",
"@types/node": "8.0.47",
"@types/node": "^20.4.2",
"@types/object-hash": "1.3.3",
"@types/semver": "7.3.12",
"colors": "1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.base.ts
Expand Up @@ -59,7 +59,7 @@ export default {
},
{
format: "es",
file: "build-self/" + pkg.module,
file: "build-self/index.mjs",
sourcemap: true,
banner: "/* eslint-disable */",
exports: "auto",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.self.ts
@@ -1,4 +1,4 @@
import ts from "./build-self/dist/rollup-plugin-typescript2.es";
import ts from "./build-self/index";

import config from "./rollup.config.base";

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
@@ -1,3 +1,4 @@
import { createRequire } from "module";
import { relative, dirname, normalize as pathNormalize, resolve } from "path";
import * as tsTypes from "typescript";
import { PluginImpl, InputOptions, TransformResult, SourceMap, Plugin } from "rollup";
Expand All @@ -16,6 +17,8 @@ import { convertDiagnostic, printDiagnostics } from "./diagnostics";
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
import { createFilter } from "./get-options-overrides";

const require = createRequire(import.meta.url);

// these use globals during testing and are substituted by @rollup/plugin-replace during builds
const TS_VERSION_RANGE = (global as any)?.rpt2__TS_VERSION_RANGE || "$TS_VERSION_RANGE";
const ROLLUP_VERSION_RANGE = (global as any)?.rpt2__ROLLUP_VERSION_RANGE || "$ROLLUP_VERSION_RANGE";
Expand Down
4 changes: 3 additions & 1 deletion src/rollingcache.ts
@@ -1,9 +1,11 @@
import { existsSync, readdirSync, renameSync } from "fs";
import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra";
import fsExtra from "fs-extra"; // TODO: replace with fs-extra/esm once we support node16+ moduleResolution
import * as _ from "lodash";

import { ICache } from "./icache";

const { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } = fsExtra; // TODO: per above, replace as named exports once we support node16+ moduleResolution

/**
* Saves data in new cache folder or reads it from old one.
* Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed.
Expand Down
3 changes: 3 additions & 0 deletions src/tslib.ts
@@ -1,3 +1,4 @@
import { createRequire } from "module";
import { readFileSync } from "fs";

// The injected id for helpers.
Expand All @@ -6,6 +7,8 @@ export const TSLIB_VIRTUAL = "\0tslib.js";
export let tslibSource: string;
export let tslibVersion: string;

const require = createRequire(import.meta.url);

try
{
// tslint:disable-next-line:no-string-literal no-var-requires
Expand Down

0 comments on commit 5c04ca1

Please sign in to comment.