Skip to content

Commit e2a49ed

Browse files
committed
Fixed missing dependancy in package.json and fixed build script errors.
1 parent 8241908 commit e2a49ed

File tree

4 files changed

+108
-28
lines changed

4 files changed

+108
-28
lines changed

buildLib/esbuild-plugin-swc.mjs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,31 @@ SOFTWARE.
3030

3131
import { transform, transformSync } from '@swc/core';
3232
import path from 'node:path';
33-
import process from 'node:process';
3433
import fs from 'node:fs/promises';
34+
import { existsSync } from 'node:fs';
35+
36+
function getNodeModulesDirectoryForPath(currentDir){
37+
let relativePath = "node_modules";
38+
let resolvedPath = path.resolve(currentDir, relativePath);
39+
40+
function countSeparators(str) {
41+
let count = 0;
42+
for(let i = 0; i < str.length; i++) {
43+
if(str[i] == path.sep)
44+
count++;
45+
}
46+
return count;
47+
}
48+
49+
while(!existsSync(resolvedPath)){
50+
if(countSeparators(resolvedPath) <= 1){
51+
throw new Error("Could not find node_modules directory");
52+
}
53+
relativePath = path.join("..", relativePath);
54+
resolvedPath = path.resolve(currentDir, relativePath);
55+
}
56+
return resolvedPath;
57+
}
3558

3659
function assignDeep(target, source) {
3760
for (const key in source) {
@@ -54,7 +77,18 @@ export default function swcPlugin(options, isAsync) {
5477
name: 'esbuild:swc',
5578
setup(builder) {
5679
builder.onResolve({ filter: /\.(m?[tj]s)$/ }, (args) => {
57-
const fullPath = path.resolve(args.resolveDir, args.path);
80+
let fullPath;
81+
let defaultPath = path.resolve(args.resolveDir, args.path);
82+
if(args.kind == 'import-statement'){
83+
if(existsSync(defaultPath)){
84+
fullPath = defaultPath;
85+
}else{
86+
let nodeModulesPath = getNodeModulesDirectoryForPath(args.resolveDir);
87+
fullPath = path.join(nodeModulesPath, args.path);
88+
}
89+
}else{
90+
fullPath = defaultPath;
91+
}
5892
return {
5993
path: fullPath,
6094
};

esbuild-runner.mjs

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as esbuild from 'esbuild'
22
import Path from 'node:path';
33
import { createRequire } from 'node:module';
44
import { existsSync, readFileSync } from 'node:fs';
5+
import { fileURLToPath } from 'node:url';
56

67
import semver from 'semver';
78

@@ -14,47 +15,59 @@ import swcPlugin from './buildLib/esbuild-plugin-swc.mjs';
1415

1516
function getScriptPath(){
1617
try{
17-
return import.meta.url;
18+
return fileURLToPath(import.meta.url);
1819
}catch(e){
1920
return __filename||null;
2021
}
2122
}
2223

2324
function getScriptDirectory(){
2425
try{
25-
return Path.dirname(import.meta.url);
26+
return Path.dirname(fileURLToPath(import.meta.url));
2627
}catch(e){
2728
return __dirname||null;
2829
}
2930
}
3031

3132
const resolveFileFromPackage = (function(){
32-
return ((new Function("try{return import.meta.resolve;}catch(e){return null}"))()||createRequire(getScriptPath()).resolve);
33+
const rawResolve = ((function(){try{return import.meta.resolve;}catch(e){return null}})()||createRequire(getScriptPath()).resolve);
34+
return function(fileToResolve){
35+
const result = rawResolve(fileToResolve);
36+
if(result.startsWith("file:")){
37+
return fileURLToPath(result);
38+
}
39+
return result;
40+
};
3341
})();
3442

35-
function readKeyFromPackageJson(key,pkg) {
36-
let resolvedPath;
37-
if(typeof pkg == "undefined" || pkg == null){
38-
let currentDir = getScriptDirectory;
39-
let relativePath = "package.json";
40-
resolvedPath = Path.resolve(currentDir, relativePath);
43+
function getOwnPackageJsonPath(){
44+
let currentDir = getScriptDirectory();
45+
let relativePath = "package.json";
46+
let resolvedPath = Path.resolve(currentDir, relativePath);
4147

42-
function countSeparators(str) {
43-
let count = 0;
44-
for(let i = 0; i < str.length; i++) {
45-
if(str[i] == Path.sep)
46-
count++;
47-
}
48-
return count;
48+
function countSeparators(str) {
49+
let count = 0;
50+
for(let i = 0; i < str.length; i++) {
51+
if(str[i] == Path.sep)
52+
count++;
4953
}
54+
return count;
55+
}
5056

51-
while(!existsSync(resolvedPath)){
52-
if(countSeparators(resolvedPath) <= 1){
53-
throw new Error("Could not find package.json file");
54-
}
55-
relativePath = Path.join("..", relativePath);
56-
resolvedPath = Path.resolve(currentDir, relativePath);
57+
while(!existsSync(resolvedPath)){
58+
if(countSeparators(resolvedPath) <= 1){
59+
throw new Error("Could not find package.json file");
5760
}
61+
relativePath = Path.join("..", relativePath);
62+
resolvedPath = Path.resolve(currentDir, relativePath);
63+
}
64+
return resolvedPath;
65+
}
66+
67+
function readKeyFromPackageJson(key,pkg) {
68+
let resolvedPath;
69+
if(typeof pkg == "undefined" || pkg == null){
70+
resolvedPath = getOwnPackageJsonPath();
5871
}else{
5972
resolvedPath = resolveFileFromPackage(Path.join(pkg,"package.json"));
6073
if(!resolvedPath||!existsSync(resolvedPath)){
@@ -233,19 +246,37 @@ async function build(params) {
233246
if(!semver.satisfies(installedCoreJsVersion, readKeyFromPackageJson("devDependencies.core-js"))){
234247
throw new Error("Please update your dependencies, the core-js version installed does not match the version specified in the package.json file.");
235248
}
249+
250+
const swcTargets = {
251+
"es2015": {
252+
"chrome": "49",
253+
"firefox": "44",
254+
"safari": "10",
255+
"edge": "13",
256+
"opera": "36",
257+
"android": "125",
258+
"ios": "10"
259+
},
260+
"compat": compatTargets
261+
};
262+
236263
swcConfig.env = {
237264
"bugfixes": true,
238265
"mode": "usage",
239266
"coreJs": installedCoreJsVersion
240267
};
268+
swcConfig.env.targets = swcTargets[params.options.target??'es2015']||swcTargets["es2015"];
241269
if(params.options.target == "compat") {
242-
swcConfig.env.targets = compatTargets;
243270
swcConfig.jsc.minify.compress.ecma = 3;
244271
esbuildTarget = convertToEsbuildTarget(compatTargets);
245272
//esbuild does not officialy support es3 but outputs es3 compatable code when you specifiy es5 as the target.
246273
esbuildTarget.push("es5");
247274
}else{
248-
swcConfig.jsc.target = params.options.target??'es2015';
275+
const ecmaVersion = parseInt((params.options.target??'es2015').slice(2));
276+
if(isNaN(ecmaVersion)){
277+
ecmaVersion = 2015;
278+
}
279+
swcConfig.jsc.minify.compress.ecma = ecmaVersion;
249280
esbuildTarget = params.options.target??'es2015';
250281
}
251282
}

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848
"opera": "12.1",
4949
"android": "122",
5050
"ios": "8",
51-
"node": "6.17.1"
51+
"node": "13.2.0"
5252
},
5353
"devDependencies": {
5454
"mocha": "^10.4.0",
5555
"esbuild": "^0.21.5",
5656
"eslint": "^9.4.0",
57+
"semver": "^7.6.2",
5758
"core-js": "^3.37.1",
5859
"@swc/core": "^1.5.28",
5960
"@swc/helpers": "^0.5.11"

0 commit comments

Comments
 (0)