Closed codewithkyle closed 5 years ago
The current .gitignore setup doesn't work with the server-side compiled JavaScript and CSS. Update the following files to fix:
.gitignore
webpack.config.js
const path = require('path'); const glob = require("glob"); const fs = require('fs'); const rimraf = require("rimraf"); const chalk = require('chalk'); const TerserPlugin = require('terser-webpack-plugin'); // Create a timestamp for cache busting const timestamp = Date.now().toString(); const entries = {}; // Get the entries const entryFiles = glob.sync('./_compiled/**/*.js'); if(entryFiles.length){ for(let i = 0; i < entryFiles.length; i++){ const name = entryFiles[i].match(/[ \w-]+?(?=\.)/)[0].toLowerCase(); entries[name] = entryFiles[i]; } }else{ console.log('Missing JS files'); return; } // Bundle modules module.exports = { mode: (process.env.NODE_ENV === 'production') ? 'production' : 'none', entry: entries, output: { path: path.resolve(__dirname, './public/automation'), filename: `modules-${ timestamp }/[name].js` }, resolve:{ modules:[ './utils/scripts', './node_modules' ] }, optimization: { runtimeChunk: 'single', splitChunks: { chunks: 'async', name: 'globals', maxInitialRequests: Infinity, minSize: 0, cacheGroups: { node_vendor: { test: /[\\/]node_modules[\\/]/, chunks: 'all', priority: 1, name(module) { let packageName = ''; let rawPackageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|.*$)/)[0]; let trimmedPackageName = rawPackageName.replace(/\/node_modules\//, ''); if(trimmedPackageName.match(/^\@/)){ let namespaceRemoved = trimmedPackageName.replace(/(.*?\/)(?=\w)/, ''); packageName = namespaceRemoved.replace(/\/.*/, ''); }else{ packageName = trimmedPackageName.replace(/\/.*/, ''); } return `../packages-${ timestamp }/npm.${ packageName.toLowerCase() }`; }, }, }, }, minimizer: [new TerserPlugin({ terserOptions:{ ecma: 5, mangle: false, output: { beautify: false, comments: false } } })] }, plugins: [ function(){ this.plugin("done", function(stats){ if (stats.compilation.errors && stats.compilation.errors.length){ console.log(stats.compilation.errors); process.exit(1); }else{ cleanup(); } }); } ], }; function cleanup(){ console.log(chalk.white('Removing stale JavaScript builds')); const path = `public/automation`; const allDirs = fs.readdirSync(path); const moduleDirs = []; for(let i = 0; i < allDirs.length; i++){ if(allDirs[i].match(/modules-.*/)){ moduleDirs.push(allDirs[i]); } } for(i = 0; i < moduleDirs.length; i++){ const modulesTimestamp = moduleDirs[i].match(/[^modules-].*/)[0]; if(parseInt(modulesTimestamp) < parseInt(timestamp)){ rimraf(`public/automation/${ moduleDirs[i] }`, (err)=>{ if(err){ console.log(`Failed to remove ${ moduleDirs[i] }`); throw err; } }); } } const packageDirs = []; for(let i = 0; i < allDirs.length; i++){ if(allDirs[i].match(/packages-.*/)){ packageDirs.push(allDirs[i]); } } for(i = 0; i < packageDirs.length; i++){ const packageTimestamp = packageDirs[i].match(/[^packages-].*/)[0]; if(parseInt(packageTimestamp) < parseInt(timestamp)){ rimraf(`public/automation/${ packageDirs[i] }`, (err)=>{ if(err){ console.log(`Failed to remove ${ packageDirs[i] }`); throw err; } }); } } // Write the timestamp to local general config file var data = fs.readFileSync('./config/papertrain/automation.php', 'utf-8'); var newValue = data.replace(/'jsCacheBustTimestamp'.*/g, "'jsCacheBustTimestamp' => '"+ timestamp +"',"); fs.writeFileSync('./config/papertrain/automation.php', newValue, 'utf-8'); }
sass.config.js
const sass = require('node-sass'); const glob = require("glob"); const fs = require('fs'); const rimraf = require("rimraf"); const chalk = require('chalk'); // Create a timestamp for cache busting const timestamp = Date.now().toString(); // Get all the base SCSS files from the base global scss directory const globalFiles = glob.sync('./utils/styles/*.scss'); // Get all the SCSS files from the templates lib directory const templateFiles = glob.sync('./templates/**/*.scss'); // Concat the arrays const files = [...globalFiles, ...templateFiles]; let success = true; compileSASS(); function compileSASS(){ console.log(chalk.white('Compiling SASS')); // Make the new CSS directory fs.mkdirSync(`./public/automation/styles-${ timestamp }`); // Generate the files for(let i = 0; i < files.length; i++){ const file = files[i]; sass.render( { file: file, outputStyle: 'compressed', includePaths: ['utils/styles/settings', 'utils/styles/tools'] }, function(error, result){ if (error) { console.log(chalk.hex('#f57b7b').bold(`SASS Compile Error:`), chalk.white(`${ error.message } at line`), chalk.yellow.bold(error.line), chalk.hex('#ffffff').bold(error.file)); }else{ const fileName = result.stats.entry.match(/[ \w-]+?(?=\.)/gi)[0].toLowerCase(); if(fileName){ const newFile = `./public/automation/styles-${ timestamp }/` + fileName + '.css'; console.log(chalk.hex('#ffffff').bold(file), chalk.hex('#8cf57b').bold(' [compiled]')); cleanup(files.indexOf(file)); fs.writeFile(newFile, result.css.toString(), function (err) { if(err){ success = false; }; }); }else{ console.log('Something went wrong with the file name of ' + result.stats.entry); success = false; } } } ); } } function cleanup(index){ if(index !== files.length - 1){ return; } if(!success){ console.log(chalk.white('Something went wrong, the previous CSS will not be removed')); return; } console.log(chalk.white('Removing stale CSS')); const path = `public/automation`; const allDirs = fs.readdirSync(path); const styleDirs = []; for(let i = 0; i < allDirs.length; i++){ if(allDirs[i].match(/styles-.*/)){ styleDirs.push(allDirs[i]); } } for(i = 0; i < styleDirs.length; i++){ const directoryTimestamp = styleDirs[i].match(/[^styles-].*/)[0]; if(parseInt(directoryTimestamp) < parseInt(timestamp)){ rimraf(`public/automation/${ styleDirs[i] }`, (err)=>{ if(err){ console.log(`Failed to remove ${ styleDirs[i] }`); throw err; } }); } } // Write the timestamp to Crafts general config file var data = fs.readFileSync('./config/papertrain/automation.php', 'utf-8'); var newValue = data.replace(/'cssCacheBustTimestamp'.*/g, "'cssCacheBustTimestamp' => '"+ timestamp +"',"); fs.writeFileSync('./config/papertrain/automation.php', newValue, 'utf-8'); }
# Assets /public/automation/* !/public/automation/.gitkeep
Create the public/automation/ directory and add a .gitkeep file.
public/automation/
.gitkeep
Find and replace assets with automation in the build-tools directory.
assets
automation
build-tools
Replace the /assets/ with /automation/ in the PapertrainModuleService.php file.
/assets/
/automation/
PapertrainModuleService.php
The current
.gitignore
setup doesn't work with the server-side compiled JavaScript and CSS. Update the following files to fix:webpack.config.js
sass.config.js
.gitignore
Create the
public/automation/
directory and add a.gitkeep
file.Find and replace
assets
withautomation
in thebuild-tools
directory.Replace the
/assets/
with/automation/
in thePapertrainModuleService.php
file.