Skip to content

Commit 6c544d7

Browse files
committed
Fixed missing dependancy in package.json and fixed build script errors.
Fixed eslint errors with globalThis and self. Also fixed an oversight in testutil.mjs that was causing the tests to fail with the new changes.
1 parent 8241908 commit 6c544d7

File tree

7 files changed

+346
-119
lines changed

7 files changed

+346
-119
lines changed

buildLib/esbuild-plugin-swc.mjs

Lines changed: 40 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,14 +77,26 @@ 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
};
6195
});
6296
builder.onLoad({ filter: /\.(m?[tj]s)$/ }, async (args) => {
6397
const code = await fs.readFile(args.path, 'utf-8');
6498
const isTS = args.path.endsWith('.ts');
99+
const isCoreJs = args.path.indexOf('core-js') !== -1;
65100
const initialOptions = {
66101
jsc: {
67102
parser: {
@@ -73,6 +108,9 @@ export default function swcPlugin(options, isAsync) {
73108
sourceFileName: path.relative(options.root,args.path)
74109
};
75110
const finalOptions = assignDeep(assignDeep({}, initialOptions), options);
111+
if(isCoreJs){
112+
delete finalOptions.env.mode;
113+
}
76114
let result;
77115
if (isAsync) {
78116
result = await transform(code, finalOptions);

esbuild-runner.mjs

Lines changed: 54 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,36 @@ 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+
"node": "13.2.0"
258+
},
259+
"compat": compatTargets
260+
};
261+
236262
swcConfig.env = {
237263
"bugfixes": true,
238264
"mode": "usage",
239265
"coreJs": installedCoreJsVersion
240266
};
267+
swcConfig.env.targets = swcTargets[params.options.target??'es2015']||swcTargets["es2015"];
241268
if(params.options.target == "compat") {
242-
swcConfig.env.targets = compatTargets;
243269
swcConfig.jsc.minify.compress.ecma = 3;
244270
esbuildTarget = convertToEsbuildTarget(compatTargets);
245271
//esbuild does not officialy support es3 but outputs es3 compatable code when you specifiy es5 as the target.
246272
esbuildTarget.push("es5");
247273
}else{
248-
swcConfig.jsc.target = params.options.target??'es2015';
274+
const ecmaVersion = parseInt((params.options.target??'es2015').slice(2));
275+
if(isNaN(ecmaVersion)){
276+
ecmaVersion = 2015;
277+
}
278+
swcConfig.jsc.minify.compress.ecma = ecmaVersion;
249279
esbuildTarget = params.options.target??'es2015';
250280
}
251281
}

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default [
88
sourceType: "module",
99
globals: {
1010
console: "readonly",
11+
globalThis: "readonly", // this is to get eslint to stop complaining about globalThis, it's pollyfilled in the code.
12+
self: "readonly", // this is to get eslint to stop complaining about self, it's only used if globalThis is not available.
1113
// ugly platform-dependant classes and objects
1214
DecompressionStream: "readonly",
1315
Response: "readonly",

0 commit comments

Comments
 (0)