gulp in typescript esm project not working #2678
Unanswered
dimaslanjaka
asked this question in
Help
Replies: 1 comment 2 replies
-
i got an sollution: read from creating hybrid __dirname build.ts import child_process from 'child_process';
import gulp from 'gulp';
import path from 'upath';
import terser from 'gulp-terser';
import fs from 'fs';
import cleanCSS from 'gulp-clean-css';
import htmlmin from 'gulp-htmlmin';
import config from './src/config.js';
const { getDirname } = config;
const { spawn } = child_process;
const { existsSync, mkdirSync, rmSync } = fs;
const { src, dest, series } = gulp;
const { join } = path;
const __resolve = getDirname();
const __dirname = __resolve.__dirname;
const baseSrc = join(__dirname, 'src');
const baseDest = join(__dirname, 'dist');
//console.log(baseSrc, baseDest);
/**
* copy public files to dist
* @param done
* @returns
*/
const copy = (done?: any) => {
const ignore = ['!license/**', '!**/dev.js'];
const assets = () =>
src(['**/*', '!**/*.{ts,sqlite,css,html,ejs}', ...ignore], { cwd: baseSrc }).pipe(dest(baseDest));
const minjs = () =>
src(['**/*.js', ...ignore], { cwd: join(baseSrc, 'public', 'views') })
.pipe(terser())
.pipe(dest(join(baseDest, 'public', 'views')));
const mincss = () =>
src('**/*.css', { cwd: baseSrc })
.pipe(cleanCSS({ compatibility: 'ie8' }))
.pipe(dest(baseDest));
const minhtml = () =>
src('**/*.{html,ejs}', { cwd: baseSrc })
.pipe(
htmlmin({
minifyCSS: true,
minifyJS: true,
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true
})
)
.pipe(dest(baseDest));
return series(assets, minjs, mincss, minhtml)(done);
};
const tsc = (done?: any) => {
const cmd = spawn('tsc', ['-p', join(__dirname, 'tsconfig.prod.json')], {
stdio: 'inherit',
cwd: __dirname,
env: Object.assign(process.env, { NODE_ENV: 'production' })
});
cmd.stderr?.pipe(process.stderr);
cmd.stdout?.on('data', (data) => console.log(String(data)));
cmd.on('close', () => {
if (typeof done === 'function') done();
});
};
const clean = (done?: any) => {
// only run if not exist
if (existsSync(baseDest)) {
// remove dist
rmSync(baseDest, { recursive: true });
}
if (!existsSync(baseDest)) {
// recreate dist
mkdirSync(baseDest);
}
if (typeof done == 'function') done();
};
clean(() => tsc(copy)); run with |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
iam working in typescript project with ESNext.
package.json contain `"type": "module", when i removing this, all project will returning errors.
tsconfig.json
When i running it with nodemon like below is working:
![image](https://user-images.githubusercontent.com/12471057/166197636-5ab8b69e-9a2f-45c4-a16d-44e86594177f.png)
![image](https://user-images.githubusercontent.com/12471057/166197992-df8a247b-6f1a-44d3-9af2-7868ce985e91.png)
but, gulp returning error:
i've tried below commands, and still returning errors:
when i remove
"type": "module"
from package.json, gulp returning error:gulpfile.ts
Beta Was this translation helpful? Give feedback.
All reactions