Skip to content

Commit

Permalink
Add basic linter
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Feb 29, 2024
1 parent 3c9aa70 commit fd92532
Show file tree
Hide file tree
Showing 5 changed files with 5,893 additions and 1,180 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
- name: Setup environment
run: ./script/gh-action-prepare

- name: Lint assets
run: npm run lint

- name: Build assets
run: bundle exec rails assets:precompile

Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/application_classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
//= require jquery/dist/jquery
//= require timeago/jquery.timeago

jQuery.noConflict()
/* global jQuery */
jQuery.noConflict();
58 changes: 29 additions & 29 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env node

import babel from '@babel/core'
import { createHash } from 'crypto'
import esbuild from 'esbuild'
import coffeeScriptPlugin from 'esbuild-coffeescript'
import { lessLoader } from 'esbuild-plugin-less'
import fsPromises from 'fs/promises'
import glob from 'glob'
import babel from '@babel/core';
import { createHash } from 'crypto';
import esbuild from 'esbuild';
import coffeeScriptPlugin from 'esbuild-coffeescript';
import { lessLoader } from 'esbuild-plugin-less';
import fsPromises from 'fs/promises';
import glob from 'glob';

const outdir = 'app/assets/builds'
const outdir = 'app/assets/builds';

const plugins = [
coffeeScriptPlugin({
Expand All @@ -25,62 +25,62 @@ const plugins = [
name: 'babel',
setup (build) {
build.onEnd(async () => {
const esbuildFilepath = `${outdir}/application.js`
const inputSourceMap = JSON.parse(await fsPromises.readFile(`${esbuildFilepath}.map`))
const esbuildFilepath = `${outdir}/application.js`;
const inputSourceMap = JSON.parse(await fsPromises.readFile(`${esbuildFilepath}.map`));
const options = {
inputSourceMap,
minified: true,
presets: [
['@babel/preset-env']
],
sourceMaps: true
}
const esbuildOutput = await fsPromises.readFile(esbuildFilepath)
const result = await babel.transformAsync(esbuildOutput, options)
const filename = 'application.jsout'
const outfileBabel = `${outdir}/${filename}`
};
const esbuildOutput = await fsPromises.readFile(esbuildFilepath);
const result = await babel.transformAsync(esbuildOutput, options);
const filename = 'application.jsout';
const outfileBabel = `${outdir}/${filename}`;
result.map.sources = result.map.sources
// CoffeeScript sourcemap and Esbuild sourcemap combined generates duplicated source paths
.map((path) => path.replace(/\.\.\/\.\.\/javascript(\/.+)?\/app\/javascript\//, '../app/javascript/'))
const resultMap = JSON.stringify(result.map)
const resultMapHash = createHash('sha256').update(resultMap).digest('hex')
.map((path) => path.replace(/\.\.\/\.\.\/javascript(\/.+)?\/app\/javascript\//, '../app/javascript/'));
const resultMap = JSON.stringify(result.map);
const resultMapHash = createHash('sha256').update(resultMap).digest('hex');

return Promise.all([
// add hash so it matches sprocket output
fsPromises.writeFile(outfileBabel, `${result.code}\n//# sourceMappingURL=${filename}-${resultMapHash}.map`),
fsPromises.writeFile(`${outfileBabel}.map`, JSON.stringify(result.map))
])
})
]);
});
}
},
{
name: 'analyze',
setup (build) {
build.onEnd(async (result) => {
if (options.analyze) {
const analyzeResult = await esbuild.analyzeMetafile(result.metafile)
const analyzeResult = await esbuild.analyzeMetafile(result.metafile);

console.log(analyzeResult)
console.log(analyzeResult);
}
})
});
}
}
]
];

const args = process.argv.slice(2)
const args = process.argv.slice(2);
const options = {
watch: args.includes('--watch'),
analyze: args.includes('--analyze')
}
};
const watch = options.watch
? {
onRebuild (error) {
if (error == null) {
console.log('Rebuild succeeded')
console.log('Rebuild succeeded');
}
}
}
: false
: false;

esbuild.build({
bundle: true,
Expand All @@ -94,4 +94,4 @@ esbuild.build({
sourcemap: 'external',
watch
}).then(() => console.log('Build succeeded'))
.catch((e) => console.debug(e))
.catch((e) => console.debug(e));
Loading

0 comments on commit fd92532

Please sign in to comment.