Skip to content

Commit

Permalink
[cli] Use webpack to bundle all code to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Nov 21, 2017
1 parent 10a0245 commit 64b1418
Show file tree
Hide file tree
Showing 33 changed files with 368 additions and 256 deletions.
16 changes: 14 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ before_script:
- travis_wait yarn run bootstrap
- yarn run build
script:
# Build CLI to a single file
- cd packages/@sanity/cli
- npm run pack

# Remove source and dependencies from CLI to ensure it works standalone
- rm -rf packages/@sanity/cli/node_modules
- rm -rf packages/@sanity/cli/src && rm -rf packages/@sanity/cli/lib

# Test building the test studio with all customizations
- cd packages/test-studio
- cd $TRAVIS_BUILD_DIR/packages/test-studio
- $TRAVIS_BUILD_DIR/packages/@sanity/cli/bin/sanity build

# Test initializing a project in unattended mode and building it
# Test initializing a project in unattended mode
- sanity init -y --project=ppsg7ml5 --dataset=test --output-path=/tmp/test-project

# Use the newly commited changes instead of the latest dependencies from NPM
- cd /tmp/test-project
- $TRAVIS_BUILD_DIR/scripts/symlinkDependencies.js .

# Test building the project with latest dependencies
- $TRAVIS_BUILD_DIR/packages/@sanity/cli/bin/sanity build --skip-minify
11 changes: 10 additions & 1 deletion packages/@sanity/cli/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"presets": ["es2015-node4", "stage-2"]
"plugins": [
["transform-object-rest-spread", {"useBuiltIns": true}]
],
"presets": [
["env", {
"targets": {
"node": "6"
}
}]
]
}
3 changes: 3 additions & 0 deletions packages/@sanity/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ node_modules
# Built vendor files
/vendor
!/vendor/README.md

# Packed version of Sanity binary
/bin/sanity
9 changes: 8 additions & 1 deletion packages/@sanity/cli/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
src/
/src
/test
/.babelrc
/.editorconfig
/.eslintignore
/.eslintrc
/yarn.lock
/package-lock.json
File renamed without changes.
2 changes: 0 additions & 2 deletions packages/@sanity/cli/bin/sanity

This file was deleted.

10 changes: 5 additions & 5 deletions packages/@sanity/cli/bin/sanity.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env node
/* eslint-disable no-var, no-console, no-process-exit, prefer-template */

/**
* ┌────────────────┐
* │ │
* │ WEBPACK ENTRY │
* │ NO ES6 IN HERE │
* │ !NOT COMPILED! │
* │ │
* └────────────────┘
*/

var nodeVersion = Number(process.version.replace(/^v/i, '').split('.', 2)[0])

if (nodeVersion < 6) {
console.error('ERROR: Node.js version 6 or higher required. You are running ' + process.version)
process.exit(1)
} else {
require('../lib/cli')()
}

var path = require('path')
var runCli = require('../lib/cli')
runCli(path.join(__dirname, '..'))
39 changes: 16 additions & 23 deletions packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"description": "Sanity CLI tool for managing Sanity installations, managing plugins, schemas and datasets",
"main": "lib/cli.js",
"bin": {
"sanity": "./bin/sanity.js"
"sanity": "./bin/sanity"
},
"scripts": {
"build": "npm test && npm run package-yarn",
"pack": "node scripts/pack.js",
"package-yarn": "node -r babel-register src/scripts/package-yarn.js",
"test": "mocha test/**/*.test.js",
"clean": "rimraf lib",
Expand All @@ -21,66 +22,58 @@
"type": "git",
"url": "git+https://github.com/sanity-io/sanity.git"
},
"keywords": [
"sanity",
"cms",
"headless",
"realtime",
"content",
"cli",
"tool"
],
"keywords": ["sanity", "cms", "headless", "realtime", "content", "cli", "tool"],
"author": "Sanity.io <hello@sanity.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/sanity-io/sanity/issues"
},
"homepage": "https://www.sanity.io/",
"dependencies": {
"dependencies": {},
"devDependencies": {
"@sanity/client": "^0.121.0",
"@sanity/resolver": "^0.120.0",
"@sanity/util": "^0.120.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.1",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"chalk": "^1.1.3",
"configstore": "^3.0.0",
"debug": "^2.6.3",
"decompress": "^4.2.0",
"deep-sort-object": "^1.0.1",
"eslint": "^4.9.0",
"eslint-config-sanity": "^3.1.0",
"execa": "^0.6.0",
"fs-extra": "^4.0.2",
"git-user-info": "^1.0.1",
"gitconfiglocal": "^2.0.1",
"inquirer": "^2.0.0",
"klaw-sync": "^3.0.2",
"latest-version": "^3.1.0",
"leven": "^2.1.0",
"lodash": "^4.17.4",
"minimist": "^1.2.0",
"mocha": "^3.2.0",
"opn": "^4.0.2",
"ora": "^1.3.0",
"osenv": "^0.1.4",
"package-json": "^4.0.1",
"progrescii": "^0.1.1",
"promise-props-recursive": "^1.0.0",
"resolve-from": "^4.0.0",
"rimraf": "^2.6.2",
"semver": "^5.3.0",
"semver-compare": "^1.0.0",
"simple-get": "^2.7.0",
"split2": "^2.1.1",
"thenify": "^3.3.0",
"update-notifier": "^1.0.3",
"validate-npm-package-name": "^3.0.0",
"webpack": "^3.8.1",
"xdg-basedir": "^3.0.0"
},
"devDependencies": {
"babel-plugin-lodash": "^3.2.11",
"babel-preset-es2015-node4": "^2.1.1",
"babel-preset-stage-2": "^6.22.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"eslint": "^4.9.0",
"eslint-config-sanity": "^3.1.0",
"mocha": "^3.2.0",
"rimraf": "^2.6.2",
"source-map-support": "^0.4.11"
}
}
13 changes: 13 additions & 0 deletions packages/@sanity/cli/scripts/lazy-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable space-before-function-paren */
module.exports = function(content) {
if (this.cacheable) {
this.cacheable()
}

this.value = content

const nonLazy = content.replace(/ = lazyRequire\(/g, ' = () => require(')
const withoutLazy = nonLazy.replace(/\n.*?lazyRequire\d* = .*?\n/g, '')

return withoutLazy
}
89 changes: 89 additions & 0 deletions packages/@sanity/cli/scripts/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env node
/* eslint-disable no-console, no-process-exit, no-sync */
const fse = require('fs-extra')
const path = require('path')
const webpack = require('webpack')
const klawSync = require('klaw-sync')

const lazyLoader = require.resolve('./lazy-loader')
const shebangLoader = require.resolve('./shebang-loader')
const basedir = path.join(__dirname, '..')

// Make sure there are no native modules
const isBinding = file => path.basename(file.path) === 'binding.gyp'
const bindings = klawSync(basedir, {nodir: true, filter: isBinding})

if (bindings.length > 0) {
console.error('Eek! Found native module at:')
bindings.forEach(file => console.error(file.path))
process.exit(1)
}

const babelRc = JSON.parse(fse.readFileSync(path.join(basedir, '.babelrc'), 'utf8'))

// Use the real node __dirname and __filename in order to get Yarn's source
// files on the user's system. See constants.js
const nodeOptions = {
__filename: false,
__dirname: false
}

const compiler = webpack({
entry: {
sanity: path.join(basedir, 'bin/sanity.js')
},
output: {
filename: 'sanity',
path: path.join(basedir, 'bin'),
libraryTarget: 'commonjs2'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [{loader: 'babel-loader', options: babelRc}]
},
{
test: /node_modules[/\\]rc[/\\]/,
use: [{loader: shebangLoader}]
},
{
test: /node_modules[/\\]update-notifier[/\\].*\.js$/,
use: [{loader: lazyLoader}]
}
]
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),
new webpack.BannerPlugin({
banner: '#!/usr/bin/env node',
raw: true
}),
new webpack.DefinePlugin({
__SANITY_IS_BUNDLED__: JSON.stringify(true),

// Workaround for rc module console.logging if module.parent does not exist
'module.parent': JSON.stringify({})
})
],
target: 'node',
node: nodeOptions
})

compiler.run((err, stats) => {
if (err) {
throw err
}

if (stats.compilation.errors.length > 0) {
console.error(stats.compilation.errors)
process.exit(1)
}

// Make the file executable
const outputPath = path.join(basedir, 'bin', 'sanity')
fse.chmodSync(outputPath, 0o755)

console.log('Done packing.')
})
15 changes: 15 additions & 0 deletions packages/@sanity/cli/scripts/shebang-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable space-before-function-paren */
module.exports = function(content) {
if (this.cacheable) {
this.cacheable()
}

this.value = content
let source = content

if (typeof source === 'string' && /^\s*#!/.test(source)) {
source = source.replace(/^\s*#![^\n\r]*[\r\n]/, '')
}

return source
}

0 comments on commit 64b1418

Please sign in to comment.