@cara/porter
Advanced tools
Comparing version 4.3.0 to 4.3.1
{ | ||
"name": "@cara/porter", | ||
"description": "A middleware for web modules", | ||
"version": "4.3.0", | ||
"version": "4.3.1", | ||
"main": "src/porter.js", | ||
@@ -46,3 +46,3 @@ "repository": { | ||
"license": "BSD-3-Clause", | ||
"gitHead": "69a3fbeb6532552ad13c915a325329463e94a966" | ||
"gitHead": "187c69c37d33a535623ba0efe29ea94e8d8efdb5" | ||
} |
@@ -15,5 +15,2 @@ 'use strict'; | ||
const DEPTH_FIRST = 0; | ||
const BREATH_FIRST = 1; | ||
function getEntry(packet, entries) { | ||
@@ -146,6 +143,2 @@ return entries && (entries.length === 1 || !packet.parent) ? entries[0] : packet.main; | ||
* [Symbol.iterator]() { | ||
return yield* this.iterate({ mode: DEPTH_FIRST }); | ||
} | ||
* iterate({ mode = DEPTH_FIRST } = {}) { | ||
const { entries, packet, scope, format } = this; | ||
@@ -158,5 +151,3 @@ const extensions = EXTENSION_MAP[format]; | ||
if (!entry.children) return; | ||
const children = [ ...entry.children ]; | ||
if (mode === BREATH_FIRST) children.reverse(); | ||
for (const mod of children) { | ||
for (const mod of entry.children) { | ||
if (done[mod.id]) continue; | ||
@@ -178,3 +169,3 @@ if (entry.dynamicChildren?.includes(mod)) continue; | ||
done[entry.id] = true; | ||
if (mode === DEPTH_FIRST) yield* iterate(entry, preload); | ||
yield* iterate(entry, preload); | ||
if (extensions.includes(path.extname(entry.file))) { | ||
@@ -187,3 +178,2 @@ if (!(entry.packet.isolated && preload && entry.packet !== preload?.packet)) { | ||
} | ||
if (mode === BREATH_FIRST) yield* iterate(entry, preload); | ||
} | ||
@@ -402,3 +392,3 @@ | ||
for (const mod of this.iterate({ mode: BREATH_FIRST })) { | ||
for (const mod of this) { | ||
const { code, map } = minify ? await mod.minify() : await mod.obtain(); | ||
@@ -411,3 +401,3 @@ const subnode = await this.createSourceNode({ | ||
}); | ||
node.prepend(subnode); | ||
node.add(subnode); | ||
} | ||
@@ -455,5 +445,5 @@ | ||
for (const mod of this.iterate({ mode: BREATH_FIRST })) { | ||
const { code } = minify ? await mod.minify() : await mod.obtain(); | ||
chunks.unshift(code); | ||
for (const mod of this) { | ||
const result = minify ? await mod.minify() : await mod.obtain(); | ||
chunks.push(result.code); | ||
} | ||
@@ -460,0 +450,0 @@ |
@@ -151,9 +151,2 @@ 'use strict'; | ||
this.imports.push(dep); | ||
} else { | ||
// import './foo.d.ts'; | ||
// import './bar.ts'; | ||
const mod = await this.parseImport(dep); | ||
for (let i = this.children.length - 1; i >= 0; i--) { | ||
if (this.children[i] === mod) this.children.splice(i, 1); | ||
} | ||
} | ||
@@ -160,0 +153,0 @@ } |
@@ -7,3 +7,35 @@ 'use strict'; | ||
module.exports = class TsModule extends JsModule { | ||
async _transpile({ code, }) { | ||
async load() { | ||
const { packet } = this; | ||
const { code } = await super.load(); | ||
const ts = packet.tryRequire('typescript'); | ||
if (!ts) return { code }; | ||
this.matchImport(code); | ||
// remove imports of type definitions in advance, such as | ||
// import { IModel } from './foo.d.ts'; | ||
// import { IOptions } from './bar.ts'; | ||
const result = await this._transpile({ code }, { | ||
target: ts.ScriptTarget.ES2022, | ||
sourceMap: false, | ||
}); | ||
// remove imports that are transformed from dynamic imports, such as | ||
// import('./utils/math') | ||
this.matchImport(result.code); | ||
return result; | ||
} | ||
matchImport(code) { | ||
const { dynamicImports = [] } = this; | ||
super.matchImport(code); | ||
if (dynamicImports.length > 0) { | ||
this.dynamicImports = dynamicImports; | ||
this.imports = this.imports.filter(specifier => { | ||
return !dynamicImports.includes(specifier); | ||
}); | ||
} | ||
} | ||
async _transpile({ code }, compilerOptions) { | ||
const { fpath, packet } = this; | ||
@@ -20,7 +52,6 @@ const ts = packet.tryRequire('typescript'); | ||
// - https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function | ||
const { compilerOptions } = tsconfig; | ||
const { outputText, diagnostics, sourceMapText } = ts.transpileModule(code, { | ||
fileName, | ||
compilerOptions: { | ||
...compilerOptions, | ||
...tsconfig.compilerOptions, | ||
module: ts.ModuleKind.CommonJS, | ||
@@ -30,2 +61,3 @@ sourceMap: true, | ||
inlineSourceMap: false, | ||
...compilerOptions, | ||
}, | ||
@@ -32,0 +64,0 @@ }); |
138654
3533