Created
November 2, 2017 09:26
-
-
Save begeeben/094a31a658e2d451233ea4ecd556a70b to your computer and use it in GitHub Desktop.
babel build without bundling
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
Show hidden characters
{ | |
"presets": [ | |
"stage-1", | |
[ | |
"env", { | |
"targets": { | |
"browsers": ["Chrome >= 61"], | |
"node": 6 | |
}, | |
"modules": "commonjs" | |
} | |
], | |
"react" | |
], | |
"plugins": [ | |
[ | |
"css-modules-transform",{ | |
"preprocessCss": "./preprocess-scss.js", | |
"extractCss": "./dev_build/style-bundle.css", | |
"extensions": [ | |
".css", | |
".scss" | |
] | |
} | |
] | |
], | |
"sourceMaps": true | |
} |
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
const fs = require('fs'); | |
const path = require('path'); | |
const mkdirp = require('mkdirp'); | |
const minimatch = require('minimatch'); | |
const babel = require('babel-core'); | |
// TODO: instead of working on paths, try to resolve dependencies | |
const EXTNAMES = ['.js', '.jsx', '.mjs']; | |
const INCLUDE_FILES = ['src/']; | |
const EXCLUDE_FILES = [ | |
'**/setupTests.js', | |
'**/*.test.js' | |
]; | |
const OUT_DIR = 'dev_build/'; | |
const BABEL_OPTIONS = { | |
}; | |
console.time('build'); | |
process.on('exit', (code) => { | |
console.timeEnd('build'); | |
}); | |
// TODO: support walk('path', () => {}) | |
function walk(filePath, options = {}, callback) { | |
// if (!filePath || !filePath.trim()) { | |
// throw new Error('Path is not specified.'); | |
// } | |
// if (!callback || typeof callback !== 'function') { | |
// throw new Error('Callback function is not specified.'); | |
// } | |
// filter | |
if (options.excludes) { | |
for (let i = 0; i < options.excludes.length; i++) { | |
if (minimatch(filePath, options.excludes[i], { nocase: true })) { | |
return; | |
} | |
} | |
} | |
// check the file path status | |
fs.stat(filePath, function (err, stats) { | |
if (err) { | |
throw err; | |
} | |
if (stats.isDirectory()) { | |
fs.readdir(filePath, function (err, files) { | |
if (err) { | |
throw err; | |
} | |
files.forEach(function (fileName) { | |
walk(path.join(filePath, fileName), options, callback); | |
}); | |
}); | |
} | |
if (stats.isFile()) { | |
// filter extname | |
if (options.extnames && options.extnames.indexOf(path.extname(filePath)) === -1) { | |
return; | |
} | |
callback(filePath); | |
} | |
}); | |
} | |
function transpile(filePath) { | |
console.log(`transpiling ${filePath}`); | |
const outputFileName = path.join(OUT_DIR, filePath); | |
// const serverBuildFileName = path.join('dev_sever_build', filePath.replace('.js', '.mjs')); | |
babel.transformFile(filePath, BABEL_OPTIONS, function (err, result) { | |
if (err) { | |
throw err; | |
} | |
// result; // => { code, map, ast } | |
mkdirp(path.dirname(outputFileName), function (err) { | |
if (err) { | |
throw err; | |
} | |
fs.writeFile(outputFileName, result.code, function (err) { | |
if (err) { | |
throw err; | |
} | |
console.log(`Finished ${outputFileName}`); | |
}); | |
}); | |
// mkdirp(path.dirname(serverBuildFileName), function (err) { | |
// if (err) { | |
// throw err; | |
// } | |
// fs.writeFile(serverBuildFileName, result.code, function (err) { | |
// if (err) { | |
// throw err; | |
// } | |
// console.log(`Finished ${serverBuildFileName}`); | |
// }); | |
// }); | |
}); | |
} | |
INCLUDE_FILES.forEach((dir) => { | |
walk(dir, { extnames: EXTNAMES, excludes: EXCLUDE_FILES }, transpile); | |
}); | |
walk('node_modules/', { excludes: ['**/*.js', '**/*.scss'] }, (filePath) => { | |
const outputFileName = path.join(OUT_DIR, filePath); | |
mkdirp(path.dirname(outputFileName), function (err) { | |
if (err) { | |
throw err; | |
} | |
// fs.copyFile(filePath, outputFileName, function (err) { | |
// if (err) { | |
// throw err; | |
// } | |
// console.log(`Copied ${outputFileName}`); | |
// }); | |
fs.createReadStream(filePath).pipe(fs.createWriteStream(outputFileName)); | |
}); | |
}); |
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
const sass = require('node-sass'); | |
module.exports = function processSass(data, filename) { | |
const result = sass.renderSync({ | |
data: data.replace('${theme}', 'xuse').replace('~', ''), | |
file: filename, | |
includePaths: ['src', 'node_modules'], | |
sourceMap: true, | |
sourceComments: true | |
}).css; | |
return result.toString('utf8'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment