-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
234 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env node | ||
|
||
const {join} = require('node:path'); | ||
const esbuild = require('esbuild'); | ||
const {sassPlugin} = require('esbuild-sass-plugin'); | ||
const {sparsedBuildPlugin} = require('./sparsed-build-plugin'); | ||
const {useFromSourcePlugin} = require('./use-from-source-plugin'); | ||
const autoprefixer = require('autoprefixer'); | ||
const postcssPresetEnv = require('postcss-preset-env'); | ||
const postcss = require('postcss'); | ||
const tsconfigJson = require('../tsconfig.json'); | ||
|
||
const {compilerOptions: {target}} = tsconfigJson; | ||
|
||
function build({path, format}) { | ||
return esbuild.build({ | ||
bundle: true, | ||
sourcemap: true, | ||
target: target, | ||
tsconfig: './tsconfig.json', | ||
entryPoints: [join('./src', path)], | ||
outbase: './src', | ||
outdir: `./build${format ? '/' + format : ''}`, | ||
format: format, | ||
plugins: [ | ||
useFromSourcePlugin(/\.svg$/), | ||
sparsedBuildPlugin({ | ||
extension: { | ||
'.scss': '.css', | ||
} | ||
}), | ||
sassPlugin({ | ||
async transform(source) { | ||
const {css} = await postcss([ | ||
autoprefixer({cascade: false}), | ||
postcssPresetEnv({stage: 0}), | ||
]).process(source, {from: undefined}); | ||
|
||
return css; | ||
}, | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
build({path: 'index.ts', format: 'cjs'}); | ||
build({path: 'index.ts', format: 'esm'}); | ||
build({path: 'index.scss'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
const assert = require('node:assert'); | ||
const {join, resolve} = require('node:path'); | ||
const {statSync, existsSync} = require('node:fs'); | ||
|
||
function resolveFile(path, exts, strict) { | ||
const isLikeFile = exts.some((ext) => path.endsWith(ext)); | ||
|
||
if (!existsSync(path)) { | ||
if (!isLikeFile) { | ||
for (const ext of exts) { | ||
const file = resolveFile(path + ext, exts); | ||
if (file) { | ||
return file; | ||
} | ||
} | ||
} | ||
} else { | ||
const stat = statSync(path); | ||
|
||
if (stat.isFile()) { | ||
return path; | ||
} else if (!isLikeFile && stat.isDirectory()) { | ||
for (const ext of exts) { | ||
const file = resolveFile(join(path, 'index' + ext), exts); | ||
if (file) { | ||
return file; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (strict) { | ||
// throw ENOENT | ||
return statSync(path); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function replaceExt(path, extensions) { | ||
for (const [from, to] of Object.entries(extensions)) { | ||
if (path.endsWith(from)) { | ||
const parts = path.split(from).slice(0, -1); | ||
|
||
return parts | ||
.concat(parts.pop() + to) | ||
.join(from); | ||
} | ||
} | ||
|
||
return path; | ||
} | ||
|
||
function sparsedBuildPlugin({extension = {}, processed = new Set()} = {}) { | ||
extension = Object.assign({ | ||
'.ts': '.js', | ||
'.tsx': '.js', | ||
'.json': '.js', | ||
}, extension); | ||
|
||
return { | ||
name: 'sparsedBuild', | ||
setup(build) { | ||
const {esbuild, initialOptions} = build; | ||
const { | ||
bundle, | ||
resolveExtensions = ['.tsx', '.ts', '.jsx', '.js', '.css', '.json'] | ||
} = initialOptions; | ||
|
||
assert(bundle === true, `Option 'bundle' should be 'true' for sparsed build`); | ||
|
||
build.onResolve({filter: /.*/}, async ({path, resolveDir, kind}) => { | ||
if (kind === 'entry-point') { | ||
return; | ||
} | ||
|
||
if (!path.match(/^\.{1,2}/)) { | ||
return {external: true}; | ||
} | ||
|
||
const fullpath = resolveFile(resolve(resolveDir, path), resolveExtensions, true); | ||
|
||
if (!processed.has(fullpath)) { | ||
processed.add(fullpath); | ||
|
||
await esbuild.build({ | ||
...initialOptions, | ||
entryPoints: [fullpath], | ||
}); | ||
} | ||
|
||
return { | ||
path: replaceExt(path, extension), | ||
external: true | ||
}; | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
sparsedBuildPlugin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const {dirname, relative, resolve} = require('node:path'); | ||
|
||
function useFromSourcePlugin(match) { | ||
return { | ||
name: 'useFromSource', | ||
setup(build) { | ||
let {outdir, outfile, outbase = '.'} = build.initialOptions; | ||
outdir = resolve(outdir || dirname(outfile)); | ||
outbase = resolve(outbase); | ||
|
||
build.onResolve({filter: match}, ({path, resolveDir, importer}) => { | ||
if (!path.match(/^\.{1,2}/)) { | ||
return {external: true}; | ||
} | ||
|
||
const outpath = resolve(outdir, relative(outbase, dirname(importer))); | ||
|
||
return { | ||
external: true, | ||
path: relative(outpath, resolve(resolveDir, path)) | ||
}; | ||
}); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = { | ||
useFromSourcePlugin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.