js-build-tools 5.1.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,11 +4,11 @@ require("core-js/modules/esnext.weak-map.delete-all.js");
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.tsFor = void 0;
7
+ exports.tsFor = exports.loadTypescript = void 0;
8
8
  var _gulp = require("gulp");
9
9
  var _nodePath = _interopRequireDefault(require("node:path"));
10
10
  var gulpConfig = _interopRequireWildcard(require("../../gulp.config.js"));
11
- var _typescript = _interopRequireDefault(require("typescript"));
11
+ var _nodeModule = require("node:module");
12
12
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
13
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
14
14
  // gulp-ts-compile ships ESM-only (no CJS build - see its own package.json) since it's meant to be consumed by
@@ -27,6 +27,27 @@ const importGulpTsCompile = new Function('return import(\'gulp-ts-compile\')');
27
27
  * @param {import('node:stream').Stream} stream
28
28
  * @returns {Promise<void>}
29
29
  */
30
+ const installHint = 'Install it in your project with: npm install --save-dev typescript';
31
+
32
+ /**
33
+ * Load the project's own copy of TypeScript (an optional peer dependency, only needed when the typescript task is
34
+ * enabled) from the project being built, so projects which do not use TypeScript never need it installed.
35
+ * @memberOf module:partials
36
+ * @param {string} [projectPath=process.cwd()] The folder of the project (which has the package.json) being built.
37
+ * @returns {Object} The TypeScript compiler API module.
38
+ * @throws {Error} With install instructions when typescript is not installed.
39
+ */
40
+ const loadTypescript = (projectPath = process.cwd()) => {
41
+ try {
42
+ return (0, _nodeModule.createRequire)(_nodePath.default.join(projectPath, 'package.json'))('typescript');
43
+ } catch (error) {
44
+ if (error.code === 'MODULE_NOT_FOUND' && /Cannot find module 'typescript'/.test(error.message)) {
45
+ throw new Error(`The typescript task needs the optional peer dependency typescript, which is not installed. ${installHint}`);
46
+ }
47
+ throw error;
48
+ }
49
+ };
50
+ exports.loadTypescript = loadTypescript;
30
51
  const streamToPromise = stream => new Promise((resolve, reject) => {
31
52
  stream.on('finish', resolve);
32
53
  stream.on('error', reject);
@@ -41,9 +62,9 @@ const streamToPromise = stream => new Promise((resolve, reject) => {
41
62
  * @param {string} configPath - Path to a tsconfig.json file.
42
63
  * @returns {import('typescript').CompilerOptions}
43
64
  */
44
- const readCompilerOptions = configPath => {
45
- const configFile = _typescript.default.readConfigFile(configPath, _typescript.default.sys.readFile);
46
- const parsedConfig = _typescript.default.parseJsonConfigFileContent(configFile.config, _typescript.default.sys, _nodePath.default.dirname(configPath));
65
+ const readCompilerOptions = (ts, configPath) => {
66
+ const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
67
+ const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, _nodePath.default.dirname(configPath));
47
68
  return parsedConfig.options;
48
69
  };
49
70
 
@@ -66,16 +87,17 @@ const tsFor = (srcPath = gulpConfig.get('typescript.from'), distPath = gulpConfi
66
87
  if (gulpConfig.get('typescript.enabled') === false) {
67
88
  return () => {};
68
89
  }
90
+ const ts = loadTypescript();
69
91
  const configPath = gulpConfig.get('typescript.config');
70
92
  // When no tsconfig.json is configured, target/module are pinned explicitly rather than left for TypeScript's
71
93
  // own compiler defaults - those defaults have already shifted once across a TypeScript version bump in this
72
94
  // project's own history (silently changing whether output keeps `const`/`let` or downlevels to `var`), and
73
95
  // pinning them keeps this task's output stable across future TypeScript upgrades too. ES5/CommonJS matches
74
96
  // what the downstream babel/browserify bundling pipeline has always assumed it receives.
75
- const compilerOptions = configPath ? readCompilerOptions(configPath) : {
97
+ const compilerOptions = configPath ? readCompilerOptions(ts, configPath) : {
76
98
  declaration: true,
77
- target: _typescript.default.ScriptTarget.ES5,
78
- module: _typescript.default.ModuleKind.CommonJS,
99
+ target: ts.ScriptTarget.ES5,
100
+ module: ts.ModuleKind.CommonJS,
79
101
  esModuleInterop: false
80
102
  };
81
103
  // Accepts gulp's own callback-style task convention (an optional `done`, called once finished) rather than
@@ -1,7 +1,7 @@
1
1
  import { dest, src } from 'gulp'
2
2
  import path from 'node:path'
3
3
  import * as gulpConfig from '../../gulp.config.mjs'
4
- import ts from 'typescript'
4
+ import { createRequire } from 'node:module'
5
5
 
6
6
  // gulp-ts-compile ships ESM-only (no CJS build - see its own package.json) since it's meant to be consumed by
7
7
  // gulpfiles running under a real ESM loader, same as this project's own gulpfile.mjs does. This project also
@@ -19,6 +19,27 @@ const importGulpTsCompile = new Function('return import(\'gulp-ts-compile\')')
19
19
  * @param {import('node:stream').Stream} stream
20
20
  * @returns {Promise<void>}
21
21
  */
22
+ const installHint = 'Install it in your project with: npm install --save-dev typescript'
23
+
24
+ /**
25
+ * Load the project's own copy of TypeScript (an optional peer dependency, only needed when the typescript task is
26
+ * enabled) from the project being built, so projects which do not use TypeScript never need it installed.
27
+ * @memberOf module:partials
28
+ * @param {string} [projectPath=process.cwd()] The folder of the project (which has the package.json) being built.
29
+ * @returns {Object} The TypeScript compiler API module.
30
+ * @throws {Error} With install instructions when typescript is not installed.
31
+ */
32
+ export const loadTypescript = (projectPath = process.cwd()) => {
33
+ try {
34
+ return createRequire(path.join(projectPath, 'package.json'))('typescript')
35
+ } catch (error) {
36
+ if (error.code === 'MODULE_NOT_FOUND' && /Cannot find module 'typescript'/.test(error.message)) {
37
+ throw new Error(`The typescript task needs the optional peer dependency typescript, which is not installed. ${installHint}`)
38
+ }
39
+ throw error
40
+ }
41
+ }
42
+
22
43
  const streamToPromise = (stream) => new Promise((resolve, reject) => {
23
44
  stream.on('finish', resolve)
24
45
  stream.on('error', reject)
@@ -33,7 +54,7 @@ const streamToPromise = (stream) => new Promise((resolve, reject) => {
33
54
  * @param {string} configPath - Path to a tsconfig.json file.
34
55
  * @returns {import('typescript').CompilerOptions}
35
56
  */
36
- const readCompilerOptions = (configPath) => {
57
+ const readCompilerOptions = (ts, configPath) => {
37
58
  const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
38
59
  const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath))
39
60
  return parsedConfig.options
@@ -60,6 +81,7 @@ export const tsFor = (
60
81
  if (gulpConfig.get('typescript.enabled') === false) {
61
82
  return () => {}
62
83
  }
84
+ const ts = loadTypescript()
63
85
  const configPath = gulpConfig.get('typescript.config')
64
86
  // When no tsconfig.json is configured, target/module are pinned explicitly rather than left for TypeScript's
65
87
  // own compiler defaults - those defaults have already shifted once across a TypeScript version bump in this
@@ -67,7 +89,7 @@ export const tsFor = (
67
89
  // pinning them keeps this task's output stable across future TypeScript upgrades too. ES5/CommonJS matches
68
90
  // what the downstream babel/browserify bundling pipeline has always assumed it receives.
69
91
  const compilerOptions = configPath
70
- ? readCompilerOptions(configPath)
92
+ ? readCompilerOptions(ts, configPath)
71
93
  : { declaration: true, target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS, esModuleInterop: false }
72
94
  // Accepts gulp's own callback-style task convention (an optional `done`, called once finished) rather than
73
95
  // just returning a Promise, since callers may invoke the returned function directly with a callback instead
@@ -3,7 +3,7 @@ import { Transform, PassThrough } from 'node:stream'
3
3
  import ts from 'typescript'
4
4
  import File from 'vinyl'
5
5
  import * as setUp from '../test-helpers/setUp.mjs'
6
- import { tsFor } from './tsFor.mjs'
6
+ import { tsFor, loadTypescript } from './tsFor.mjs'
7
7
  import { countMatches } from 'test-filesystem'
8
8
 
9
9
  setUp.setDefaults('test-ts-for')
@@ -75,3 +75,13 @@ describe('tsFor', () => {
75
75
  expect(result).toBeInstanceOf(Function)
76
76
  })
77
77
  })
78
+
79
+ describe('loadTypescript', () => {
80
+ test('loads the typescript of the project being built', () => {
81
+ expect(loadTypescript().version).toBe(ts.version)
82
+ })
83
+
84
+ test('explains how to install typescript when the project does not have it', () => {
85
+ expect(() => loadTypescript('/')).toThrow(/optional peer dependency typescript.*npm install --save-dev typescript/)
86
+ })
87
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-build-tools",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "description": "Standardised build process with gulp.",
5
5
  "main": "gulpfile.base.js",
6
6
  "exports": {
@@ -77,11 +77,15 @@
77
77
  "vinyl-source-stream": "^2.0.0"
78
78
  },
79
79
  "peerDependencies": {
80
- "gulp-imagemin": ">=9.0.0"
80
+ "gulp-imagemin": ">=9.0.0",
81
+ "typescript": ">=5.0.0 <7.0.0"
81
82
  },
82
83
  "peerDependenciesMeta": {
83
84
  "gulp-imagemin": {
84
85
  "optional": true
86
+ },
87
+ "typescript": {
88
+ "optional": true
85
89
  }
86
90
  },
87
91
  "files": [