Skip to content

Commit

Permalink
“import on demand” support in antd and next (alibaba#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
stkevintan authored and monkindey committed Jul 23, 2019
1 parent 71367f7 commit 369724c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ coverage
expected
website
gh-pages
weex
weex
build.js
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"remark-stringify": "^6.0.4",
"semver-regex": "^2.0.0",
"staged-git-files": "^1.1.2",
"ts-import-plugin": "^1.6.1",
"ts-jest": "^24.0.2",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"typescript": "^3.5.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/antd/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('../../scripts/build')({
libraryDirectory: 'lib',
libraryName: 'antd',
style: 'css'
})
2 changes: 1 addition & 1 deletion packages/antd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"npm": ">=3.0.0"
},
"scripts": {
"build": "tsc --declaration"
"build": "node ./build.js"
},
"devDependencies": {
"antd": "^3.14.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/next/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('../../scripts/build')({
libraryDirectory: 'lib',
libraryName: '@alifd/next',
style: importPath => `${importPath}/style`
})
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"npm": ">=3.0.0"
},
"scripts": {
"build": "tsc"
"build": "node ./build.js"
},
"peerDependencies": {
"@alifd/next": "^1.13.1",
Expand Down
77 changes: 77 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const tsImportPluginFactory = require('ts-import-plugin')
const ts = require('typescript')
const glob = require('glob')
const json5 = require('json5')
const fs = require('fs')

const getCompilerOptions = () => {
const tsconfig = json5.parse(
fs.readFileSync('./tsconfig.json', {
encoding: 'utf8'
})
)

const base = json5.parse(
fs.readFileSync('../../tsconfig.json', {
encoding: 'utf8'
})
)
const json = { ...base.compilerOptions, ...tsconfig.compilerOptions }

return ts.convertCompilerOptionsFromJson(json)
}

function compiler(fileNames, options, transformer) {
const program = ts.createProgram(fileNames, options)
const emitResult = transformer
? program.emit(undefined, undefined, undefined, false, {
before: [transformer]
})
: program.emit()

const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics)

allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start
)
let message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
'\n'
)
console.log(
`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
)
} else {
console.log(
`${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`
)
}
})

let exitCode = emitResult.emitSkipped ? 1 : 0
console.log(`Process exiting with code '${exitCode}'.`)
process.exit(exitCode)
}

module.exports = function build(transformerConfig) {
glob('./src/**/*.{js,ts,tsx}', (errs, fileNames) => {
if (errs) {
console.log(errs)
process.exit(1)
}
const parseRet = getCompilerOptions()
if (parseRet.errors.length > 0) {
console.log(parseRet.errors)
process.exit(1)
}
const transformer = transformerConfig
? tsImportPluginFactory(transformerConfig)
: undefined

compiler(fileNames, parseRet.options, transformer)
})
}

0 comments on commit 369724c

Please sign in to comment.