This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from trunkclub/remove-lint-from-build
Remove lint from build-module
- Loading branch information
Showing
2 changed files
with
60 additions
and
59 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
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 |
---|---|---|
@@ -1,108 +1,109 @@ | ||
require('../utils/loadEnv'); | ||
|
||
var path = require('path'); | ||
var fs = require('fs-extra'); | ||
var chokidar = require('chokidar'); | ||
var transformFileSync = require('babel-core').transformFileSync; | ||
var spawn = require('cross-spawn'); | ||
var paths = require('../config/paths'); | ||
require('../utils/loadEnv') | ||
|
||
var path = require('path') | ||
var fs = require('fs-extra') | ||
var chokidar = require('chokidar') | ||
var transformFileSync = require('babel-core').transformFileSync | ||
var spawn = require('cross-spawn') | ||
var paths = require('../config/paths') | ||
const chalk = require('chalk') | ||
|
||
function lint () { | ||
return spawn.sync('node', [require.resolve('./lint')], {stdio: 'inherit'}); | ||
} | ||
|
||
function fancyLog (color, label, fileName, src, dest) { | ||
function fancyLog(color, label, fileName, src, dest) { | ||
console.log( | ||
color.bgBlack(label), | ||
'[' + chalk.green(fileName) + ']', | ||
chalk.gray(src + ' -> ' + dest) | ||
); | ||
) | ||
} | ||
|
||
function transformWithBabel (filePath) { | ||
const srcFile = path.join(paths.appSrc, filePath); | ||
function transformWithBabel(filePath) { | ||
const srcFile = path.join(paths.appSrc, filePath) | ||
const { code } = transformFileSync(srcFile, { | ||
ast: false, | ||
sourceMaps: 'inline', | ||
presets: [ | ||
require.resolve('babel-preset-trunkclub') | ||
], | ||
presets: [require.resolve('babel-preset-trunkclub')], | ||
plugins: [ | ||
[require.resolve('babel-plugin-module-resolver'), { | ||
root: [paths.appSrc] | ||
}] | ||
] | ||
}); | ||
const outputFilePath = | ||
path.join(paths.appBuild, | ||
path.join(path.dirname(filePath), | ||
path.basename(filePath, path.extname(filePath)) + '.js')) | ||
[ | ||
require.resolve('babel-plugin-module-resolver'), | ||
{ | ||
root: [paths.appSrc], | ||
}, | ||
], | ||
], | ||
}) | ||
const outputFilePath = path.join( | ||
paths.appBuild, | ||
path.join( | ||
path.dirname(filePath), | ||
path.basename(filePath, path.extname(filePath)) + '.js' | ||
) | ||
) | ||
// Write the transpiled file | ||
fs.writeFileSync(outputFilePath, code); | ||
fs.writeFileSync(outputFilePath, code) | ||
fancyLog(chalk.cyan, 'BABEL', filePath, srcFile, outputFilePath) | ||
// Copy the source file as a Flow declaration file | ||
const outputFlowFilePath = outputFilePath + '.flow' | ||
const outputFlowFilePath = outputFilePath + '.flow' | ||
fs.copySync(srcFile, outputFlowFilePath) | ||
fancyLog(chalk.yellow, 'FLOW', filePath + '.flow', srcFile, outputFlowFilePath) | ||
fancyLog( | ||
chalk.yellow, | ||
'FLOW', | ||
filePath + '.flow', | ||
srcFile, | ||
outputFlowFilePath | ||
) | ||
return outputFilePath | ||
} | ||
|
||
function copyAsset (filePath) { | ||
function copyAsset(filePath) { | ||
const srcFile = path.join(paths.appSrc, filePath) | ||
const destFile = path.join(paths.appBuild, filePath) | ||
fs.copySync(srcFile, destFile) | ||
fancyLog(chalk.magenta, 'COPY', filePath, srcFile, destFile) | ||
} | ||
|
||
function processFile (filePath) { | ||
function processFile(filePath) { | ||
if (/\.(es6|jsx?)$/.test(filePath)) { | ||
|
||
fs.mkdirpSync(path.parse(path.join(paths.appBuild, filePath)).dir); | ||
const outputPath = transformWithBabel(filePath); | ||
|
||
} else if (/\.(s?css|svg|json|ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/.test(filePath)) { | ||
|
||
fs.mkdirpSync(path.parse(path.join(paths.appBuild, filePath)).dir); | ||
fs.mkdirpSync(path.parse(path.join(paths.appBuild, filePath)).dir) | ||
const outputPath = transformWithBabel(filePath) | ||
} else if ( | ||
/\.(s?css|svg|json|ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$/.test( | ||
filePath | ||
) | ||
) { | ||
fs.mkdirpSync(path.parse(path.join(paths.appBuild, filePath)).dir) | ||
copyAsset(filePath) | ||
} | ||
} | ||
|
||
var args = process.argv.slice(2) | ||
|
||
var args = process.argv.slice(2); | ||
|
||
var outDirIdx = args.indexOf('-d') !== -1 | ||
? args.indexOf('-d') | ||
: args.indexOf('--out-dir') | ||
var outDirIdx = | ||
args.indexOf('-d') !== -1 ? args.indexOf('-d') : args.indexOf('--out-dir') | ||
if (outDirIdx !== -1) { | ||
paths.appBuild = path.resolve(paths.appBuild, '..', args[outDirIdx + 1]) | ||
} | ||
|
||
// Clear previous build artifacts before we start | ||
var shouldClean = args.indexOf('--clean') !== -1 | ||
if (shouldClean) { | ||
fs.removeSync(paths.appBuild); | ||
fs.removeSync(paths.appBuild) | ||
} | ||
|
||
var result = lint(); | ||
fs.walkSync(paths.appSrc).forEach(function (filePath) { | ||
processFile(path.relative(paths.appSrc, filePath)); | ||
}); | ||
fs.walkSync(paths.appSrc).forEach(function(filePath) { | ||
processFile(path.relative(paths.appSrc, filePath)) | ||
}) | ||
|
||
var isWatch = args.indexOf('-w') !== -1 || args.indexOf('--watch') !== -1 | ||
if (!isWatch) process.exit(result.status); | ||
if (!isWatch) process.exit(0) | ||
|
||
var watcher = chokidar.watch([ | ||
'**/*.*' | ||
], { | ||
var watcher = chokidar.watch(['**/*.*'], { | ||
cwd: paths.appSrc, | ||
persistent: true, | ||
ignoreInitial: true | ||
ignoreInitial: true, | ||
}) | ||
|
||
watcher.on('all', function (event, filePath) { | ||
watcher.on('all', function(event, filePath) { | ||
if (event === 'add' || event === 'change') { | ||
lint(); | ||
processFile(filePath); | ||
processFile(filePath) | ||
} | ||
}) |